JDBC的prepareStatement使用疑惑

fxfeiyi 2006-10-21 02:48:22
改写分页程序,遇到一个SQL语句执行的问题,弄了一晚上也没解决。

public ArrayList findAllBooks(int offset, int length, Connection conn) throws SQLException {
PreparedStatement ps = null;
ResultSet rs = null;
ArrayList books = new ArrayList();
BookDAO empvo = null;
BookVO bookVO=new BookVO();

// String strSql = "select * from book where rowid not in (select rowid from book where rownum >= ?) and rownum <= ?";//原来oracle的,现在不能用到sql server上

String strSql = "select top ? * "
+"from book "
+"where bookid not in "
+"(select top ? bookid "
+" from book )";

try {
ps = conn.prepareStatement(strSql);
ps.setByte(2, Integer.valueOf(length).byteValue()); //起始记录的位置
ps.setByte(1, Integer.valueOf(offset).byteValue()); //需要读取的记录数
rs = ps.executeQuery();

while (rs.next()) {

bookVO.setBookId(Integer.toString(rs.getInt ("BookID"))) ;
bookVO.setBookName (rs.getString ("BookName")) ;
bookVO.setAuthor(rs.getString ("Author")) ;
books.add(empvo);
}
}
catch (SQLException ex) {
ex.printStackTrace();
throw ex;
}
return books;
}


java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]第 1
行: '@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)
at com.microsoft.jdbc.sqlserver.tds.TDSRPCRequest.processReplyToken(Unknown Source)
at com.microsoft.jdbc.sqlserver.tds.TDSRequest.processReply(Unknown Source)
at com.microsoft.jdbc.sqlserver.SQLServerImplStatement.getNextResultType(Unknown Source)
at com.microsoft.jdbc.base.BaseStatement.commonTransitionToState(Unknown Source)
at com.microsoft.jdbc.base.BaseStatement.postImplExecute(Unknown Source)
at com.microsoft.jdbc.base.BasePreparedStatement.postImplExecute(Unknown Source)
at com.microsoft.jdbc.base.BaseStatement.commonExecute(Unknown Source)
at com.microsoft.jdbc.base.BaseStatement.executeQueryInternal(Unknown Source)
at com.microsoft.jdbc.base.BasePreparedStatement.executeQuery(Unknown Source)
at com.fxfeiyi.itbook.dao.book.BookDAO.findAllBooks(BookDAO.java:47)
at com.fxfeiyi.itbook.bo.book.BookBO.findAllBooks(BookBO.java:33)
...全文
251 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
fxfeiyi 2006-10-21
  • 打赏
  • 举报
回复
注意不是ps.setByte(2, Integer.valueOf(length).byteValue()); //起始记录的位置
ps.setByte(1, Integer.valueOf(offset).byteValue()); //需要读取的记录数
这里的问题,使用setInt,setString 都测试过

81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧