1

I'm trying to retreive some data from data base. I executed the following Mysql query in Mysql workbench and it works perfectly :

SELECT DISTINCT  orders.orderer, orders.start_date, order_type.name, concept_name.name
FROM orders, order_type, concept_name
WHERE orders.patient_id="4" AND orders.order_type_id=order_type.order_type_id AND concept_name.concept_name_id=orders.concept_id;

Now trying to write this code in java as it shown in the following code :

public class ConnectionMysql {
    static Connection cnx;
    static Statement st;
    static ResultSet rst;

    public static void main(String[] args){
        // Connection cnx= connecterDB();
        rechercheOrdre(4);
    }

    public static Connection connecterDB(){
        try{
            Class.forName("com.mysql.jdbc.Driver");

            String url="jdbc:mysql://localhost:3306/openmrs";
            String user="root";
            String password="root123";

            Connection cnx=DriverManager.getConnection(url, user, password);

            return cnx;
        } catch(Exception e){
            e.printStackTrace();
            return null;
        }

    }
    public static void rechercheOrdre(int PatientId){
        try{
            Connection cnx= connecterDB();
            st=cnx.createStatement();
            String query=" SELECT DISTINCT  orders.orderer, orders.start_date, order_type.name, concept_name.name"
                + "FROM orders, order_type, concept_name"
                + "WHERE orders.order_type_id = order_type.order_type_id AND concept_name.concept_name_id = orders.concept_id AND orders.patient_id ='"+PatientId+"'";
            rst=st.executeQuery(query);

        } catch(SQLException e){
            System.out.println(e.getMessage());
        }
    }

}

Unfortunately, I'm getting an error that I couldn't understand:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '.order_type_id == order_type.order_type_id AND concept_name.concept_name_id == o' at line 1

2
  • returning from inside the try and catch blocks is ill advised. Commented Aug 16, 2017 at 12:35
  • remember about sql injection. Commented Aug 16, 2017 at 13:10

2 Answers 2

2

You are missing white spaces at end of lines when concated at runtime you will have concept_nameFROM

String query=" SELECT DISTINCT  orders.orderer, orders.start_date, order_type.name, concept_name.name "
            + "FROM orders, order_type, concept_name "
            + "WHERE orders.order_type_id = order_type.order_type_id AND concept_name.concept_name_id = orders.concept_id AND orders.patient_id ='"+PatientId+"'";
Sign up to request clarification or add additional context in comments.

Comments

0

You query

String query=" SELECT DISTINCT  orders.orderer, orders.start_date, order_type.name, concept_name.name"
+ "FROM orders, order_type, concept_name"
 "WHERE orders.order_type_id = order_type.order_type_id 
AND concept_name.concept_name_id = orders.concept_id 
AND orders.patient_id ='"+PatientId+"'";

Turns out to be this :

SELECT DISTINCT  orders.orderer, orders.start_date, 
order_type.name, concept_name.nameFROM orders, 
order_type, concept_nameWHERE orders.order_type_id = order_type.order_type_id 
AND concept_name.concept_name_id = orders.concept_id 
AND orders.patient_id ='your patientid';

You are missing a whitespace before from and where

So do this :

String query = " SELECT DISTINCT  orders.orderer, orders.start_date, ";
query += " order_type.name, concept_name.name ";
query += " FROM orders, order_type, concept_name ";
query += " WHERE orders.order_type_id = order_type.order_type_id AND " ;
query += " concept_name.concept_name_id = orders.concept_id AND ";
query += " orders.patient_id ='"+PatientId+"'";

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.