0

我的 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;

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

4

2 回答 2

2

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

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

"SELECT * FROM PHYSICIAN_INFO WHERE " + "BADGEID" + " = ?"
于 2012-09-03T10:50:56.260 回答
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;
于 2012-09-03T10:57:34.257 回答