0

I have something like this in my sql query

private static final String QUERY_PHYSICIAN_INFO= "SELECT * FROM PHYSICIAN_INFO WHERE ? = ?";

but following is not working..

    Connection conn = null;
    PreparedStatement stmt = null;  
     String logintype;
       if(isInteger(id))
       {
           logintype="BADGEID";

       }else{
           logintype="ID";
       }
        stmt=conn.prepareStatement(QUERY_PHYSICIAN_INFO);
        stmt.setString(1, logintype);
        stmt.setString(2, id);
        ResultSet rs = stmt.executeQuery();
        Physician phs = null;

Is there any special reason for this? Thanks in advance.

user1065490
  • 305
  • 6
  • 16

2 Answers2

2

? is for passing parameters, not field names.

If you must do this, build the SQL as

"SELECT * FROM PHYSICIAN_INFO WHERE " + "BADGEID" + " = ?"
podiluska
  • 50,950
  • 7
  • 98
  • 104
  • Thank you very much for ur concern, I tried this way, but it is updating null for static sql query. String logintype; String query ="SELECT * FROM PHYSICIAN_INFO WHERE " + logintype + " = ?" now in some method i update logintype to some other value and the finally the generated query is SELECT * FROM PHYSICIAN_INFO WHERE null = ? – user1065490 Sep 03 '12 at 11:17
  • You need to change the SQL string once you have determined what logintype is – podiluska Sep 03 '12 at 11:19
0

You could try something like this because ? can be used only for passing parameters or you could use a database function and then pass values to the function.

private static final String QUERY_PHYSICIAN_INFO_BADGE= "SELECT * FROM PHYSICIAN_INFO WHERE BADGEID = ?";

private static final String QUERY_PHYSICIAN_INFO_ID= "SELECT * FROM PHYSICIAN_INFO WHERE ID = ?";

Connection conn = null;
    PreparedStatement stmt = null;  
     String logintype;
       if(isInteger(id))
       {               
           stmt=conn.prepareStatement(QUERY_PHYSICIAN_INFO_BADGE);

       }else{               
           stmt=conn.prepareStatement(QUERY_PHYSICIAN_INFO_ID);
       }
        stmt.setString(1, id);
        ResultSet rs = stmt.executeQuery();
        Physician phs = null;
Jacob
  • 14,463
  • 65
  • 207
  • 320