我写的sql语句:
Connection conn = DBUtil.getConnection();
String sql = ""+
" select * from imooc_goddess where id = ?";
PreparedStatement ptmt = conn.prepareStatement(sql);
ptmt.setInt(1, id);
好像没什么问题呀,直接在mysql中可以查出数据,但执行时报错:
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)
这个”?“有什么问题?
不好意思,提交了问题自己就发现犯了个低级错误,现加上一句:
ResultSet rs = ptmt.executeQuery(sql);
就是这里,执行时又加了“sql”变量,相当于语句连续执行了两次,而且中间没有分割,所以出现了语法错误。
谢谢各位答复,只能采纳一个,就按答复顺序吧,多包涵。
ptmt.execute(); 少一句执行吧