SQL编写的声明没有采取'?'在where子句的两边

时间:2012-09-03 10:49:33

标签: jdbc jdb

我的sql查询中有类似的东西

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

但是以下不起作用..

    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;

这有什么特别的原因吗? 提前谢谢。

2 个答案:

答案 0 :(得分:2)

?用于传递参数,而不是字段名称。

如果必须这样做,请将SQL构建为

"SELECT * FROM PHYSICIAN_INFO WHERE " + "BADGEID" + " = ?"

答案 1 :(得分:0)

你可以试试这样的东西,因为?只能用于传递参数,或者您可以使用数据库函数,然后将值传递给函数。

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;