"select top ? ..."语句无法使用preparedstatement预编译的困惑

xfga 2007-03-27 05:09:23
下面这段代码是完全正常的:

public static Vector[] getArticleList(int start, int length) {
Vector[] vt = new Vector[2];
Connection conn = null;
PreparedStatement pst = null;
Statement st=null;
ResultSet rs = null;
String sql = "";
try {
conn = DB.getConnection();
// 首先获得所有留言
vt[0] = new Vector();
sql="SELECT TOP "+length+" * FROM articlelist WHERE articleid not in (SELECT TOP "+start+" articleid FROM articlelist)";
//pst = conn.prepareStatement(sql);
//pst.setInt(1, length);
//pst.setInt(2, start);
//rs = pst.executeQuery();
//###########这里一切正常#####################
st=conn.createStatement();
rs=st.executeQuery(sql);
while (rs.next()) {
ArticleInfo at = new ArticleInfo();
at.setArticleid(rs.getInt("articleid"));
at.setContent(rs.getString("content"));
at.setHostip(rs.getString("hostid"));
at.setNickname(rs.getString("nickname"));
at.setPubdate(rs.getTimestamp("pubdate"));
at.setTitle(rs.getString("title"));
vt[0].add(at);
}
rs.close();
// 获得所有回复
vt[1] = new Vector();
sql = "SELECT TOP ? * FROM replylist " + "WHERE articleid not in "
+ "(SELECT TOP ? articleid FROM replylist) ";
pst = conn.prepareStatement(sql);
pst.setInt(1, start);
pst.setInt(2, length);
rs = pst.executeQuery();
while (rs.next()) {
ArticleInfo at = new ArticleInfo();
at.setArticleid(rs.getInt("articleid"));
at.setContent(rs.getString("content"));
at.setFarticleid(rs.getInt("farticleid"));
at.setHostip(rs.getString("hostip"));
at.setNickname(rs.getString("nickname"));
at.setPubdate(rs.getTimestamp("pubdate"));
at.setTitle(rs.getString("title"));
at.setFarticleid(rs.getInt("farticleid"));
vt[1].add(at);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
DB.close(rs, pst, conn);
}
return vt;
}

下面这段代码是错误的:
public static Vector[] getArticleList(int start, int length) {
Vector[] vt = new Vector[2];
Connection conn = null;
PreparedStatement pst = null;
Statement st=null;
ResultSet rs = null;
String sql = "";
try {
conn = DB.getConnection();
// 首先获得所有留言
vt[0] = new Vector();
sql="SELECT TOP ? * FROM articlelist WHERE articleid not in (SELECT TOP ? articleid FROM articlelist)";
pst = conn.prepareStatement(sql);
pst.setInt(1, length);
pst.setInt(2, start);
rs = pst.executeQuery();//提示这里有错误!
//st=conn.createStatement();
//rs=st.executeQuery(sql);
while (rs.next()) {
ArticleInfo at = new ArticleInfo();
at.setArticleid(rs.getInt("articleid"));
at.setContent(rs.getString("content"));
at.setHostip(rs.getString("hostid"));
at.setNickname(rs.getString("nickname"));
at.setPubdate(rs.getTimestamp("pubdate"));
at.setTitle(rs.getString("title"));
vt[0].add(at);
}
rs.close();
// 获得所有回复
vt[1] = new Vector();
sql = "SELECT TOP ? * FROM replylist " + "WHERE articleid not in "
+ "(SELECT TOP ? articleid FROM replylist) ";
pst = conn.prepareStatement(sql);
pst.setInt(1, start);
pst.setInt(2, length);
rs = pst.executeQuery();
while (rs.next()) {
ArticleInfo at = new ArticleInfo();
at.setArticleid(rs.getInt("articleid"));
at.setContent(rs.getString("content"));
at.setFarticleid(rs.getInt("farticleid"));
at.setHostip(rs.getString("hostip"));
at.setNickname(rs.getString("nickname"));
at.setPubdate(rs.getTimestamp("pubdate"));
at.setTitle(rs.getString("title"));
at.setFarticleid(rs.getInt("farticleid"));
vt[1].add(at);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
DB.close(rs, pst, conn);
}
return vt;
}


提示的具体错误:

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 cn.burt.gbook.beans.ArticleBean.getArticleList(ArticleBean.java:25)
at cn.burt.gbook.user.Article.doGet(Article.java:41)
at cn.burt.gbook.user.Article.doPost(Article.java:52)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:687)
at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:469)
at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:403)
at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:301)
at cn.burt.gbook.user.Login.doPost(Login.java:44)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at cn.burt.gbook.user.AuthorityFilter.doFilter(AuthorityFilter.java:43)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:228)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:216)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:634)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:445)
at java.lang.Thread.run(Unknown Source)


...全文
328 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
凉凉二点凉 2007-03-27
  • 打赏
  • 举报
回复
引用时要不要写成这样啊'"+something here+"'
oracs 2007-03-27
  • 打赏
  • 举报
回复
你不用prepareStatement,就用普通的Statement,然后构造一个select sql ,你运行一下看报错么?
craig_wolfdon 2007-03-27
  • 打赏
  • 举报
回复
sql语句错误:


换行的时候用"+"连接


81,092

社区成员

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

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