PrepareStatement不在select query java中工作

时间:2015-04-30 11:55:45

标签: java prepared-statement where

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);

:显示 您的SQL语法有错误;查看与您的MySQL服务器版本对应的手册,以便在#13;' 13a''''''''和cmp_id!= 3'在第1行

2 个答案:

答案 0 :(得分:6)

您正在呼叫st.executeQuery并传入checkq,这让我觉得stStatement个实例。当然,checkq中的字符串不是有效的SQL语句 - 它中包含PreparedStatement个占位符。您应该在 executeQuery 上致电PreparedStatement(而不是简单地使用简单的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();

答案 1 :(得分:0)

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);

您正在使用statement和prepareStatement

确保使用prepareStatement