SQL 准备语句在 where 子句的两边都不'?'

  • 本文关键字:子句 语句 where SQL 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;

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

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

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

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

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

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;

相关内容

  • 没有找到相关文章

最新更新