简体   繁体   English

PrepareStatement在选择查询Java中不起作用

[英]PrepareStatement not working in select query java

String checkq="select * from "+Dbc.TB_COMPANY+" where cmp_name=? and cmp_id!=?";
        pst=con.prepareStatement(checkq);
                pst.setString(1,"test"); pst.setString(2,"2")
        rs=st.executeQuery(checkq);

shows You have an error in your SQL syntax; 显示您的SQL语法有错误; check the manual that corresponds to your MySQL server version for the right syntax to use near ''13a'' and cmp_id!=3' at line 1 检查与您的MySQL服务器版本相对应的手册以获取正确的语法,以在第1行的“ 13a”和cmp_id!= 3'附近使用

You're calling st.executeQuery and passing in checkq , which makes me think st is a Statement instance. 您正在调用st.executeQuery并传入checkq ,这使我认为stStatement实例。 The string in checkq is not, of course, a valid SQL statement — it has PreparedStatement placeholders in it. 当然, checkq的字符串不是有效的SQL语句-它具有PreparedStatement占位符。 You should be calling executeQuery on the PreparedStatement instead (and not using a simple Statement at all): 您应该改为在PreparedStatement上调用executeQuery (并且完全不要使用简单的Statement ):

String checkq="select * from "+Dbc.TB_COMPANY+" where cmp_name=? and cmp_id!=?";
pst=con.prepareStatement(checkq);
pst.setString(1,"test");
pst.setString(2,"2")
// Not: rs=st.executeQuery(checkq);
rs = pst.executeQuery();
String checkq="select * from "+Dbc.TB_COMPANY+" where cmp_name=? and cmp_id!=?";
        pst=con.prepareStatement(checkq);
                pst.setString(1,"test"); pst.setString(2,"2")
        rs=pst.executeQuery(checkq);

you are using both statement and prepareStatement 您同时使用statement和prepareStatement

make sure to use prepareStatement 确保使用prepareStatement

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM