誰能幫我看看查詢語句條件有什么問題?
我寫的sql語句:
Connection conn = DBUtil.getConnection();
?? ??? ?String sql = ""+
?? ??? ??? ??? ?" select * from imooc_goddess where id = ?";
?? ??? ?PreparedStatement ptmt = conn.prepareStatement(sql);
?? ??? ?ptmt.setInt(1, id);
好像沒什么問題呀,直接在mysql中可以查出數(shù)據(jù),但執(zhí)行時報錯:
Exception in thread "main" com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?' at line 1
?? ?at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
?? ?at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
這個”?“有什么問題?
2016-03-03
http://blog.csdn.net/vanezuo/article/details/6686089 看看這個?
2016-03-03
不好意思,提交了問題自己就發(fā)現(xiàn)犯了個低級錯誤,現(xiàn)加上一句:
ResultSet rs = ptmt.executeQuery(sql);
就是這里,執(zhí)行時又加了“sql”變量,相當于語句連續(xù)執(zhí)行了兩次,而且中間沒有分割,所以出現(xiàn)了語法錯誤。
謝謝各位答復(fù),只能采納一個,就按答復(fù)順序吧,多包涵。
2016-03-03
ptmt.execute(); 少一句執(zhí)行吧