PreparedStatement 语法错误
PreparedStatement 给Int变量赋值后执行总是出错.
String sql = "select top ? " + "* from Book where " + key + "= ' " + value +
" ' " + " and where BookID not in ( " + "select top ? BookID " + "from Book) ";
con = DriverManager.getConnection(url, user, password);
psta = con.prepareStatement(sql);
psta.setInt(1, 5);
psta.setInt(1, 1);
res = psta.executeQuery();
执行后总是报错:
java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]Line 1: Incorrect syntax near '@P1 '.
at com.microsoft.jdbc.base.BaseExceptions.createException(Unknown Source)
at com.microsoft.jdbc.base.BaseExceptions.getException(Unknown Source)
at com.microsoft.jdbc.sqlserver.tds.TDSRequest.processErrorToken(Unknown Source)
at com.microsoft.jdbc.sqlserver.tds.TDSRequest.processReplyToken(Unknown Source)
------解决方案--------------------psta.setInt(1, 5);
psta.setInt(1, 1);
赋错了吧,两个?啊
改
psta.setInt(1, 5);
psta.setInt(2, 1);
试试
------解决方案--------------------setint()用法
将指定参数设置为给定 Java int 值。在将该对象发送到数据库时,驱动程序将它转换成一个 SQL INTEGER 值。
------解决方案--------------------sql语句出错了吧,String sql 中有两个 where 。