jsp在查询的时候报错了

tangkai007 2012-09-28 11:42:48
ps = conn.prepareStatement("select * from customer");在执行这条语句的时候一切正常输出
//select top ? customercode, customername, phone, address, relationman,other from customer where customercode not in(select top ? customercode from customer) 但是执行这条语句却报出下面的错误
//ps.setInt(1,page.getBeginIndex() ); page.getBeginIndex()=0;
//ps.setInt(2,page.getEveryPage() ); page.getEveryPage()=5
rs= ps.executeQuery();


com.microsoft.sqlserver.jdbc.SQLServerException: '@P0' 附近有语法错误。
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:197)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.getNextResult(SQLServerStatement.java:1493)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.doExecutePreparedStatement(SQLServerPreparedStatement.java:390)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement$PrepStmtExecCmd.doExecute(SQLServerPreparedStatement.java:340)
at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:4575)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:1400)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(SQLServerStatement.java:179)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(SQLServerStatement.java:154)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.executeQuery(SQLServerPreparedStatement.java:283)
at com.BeifengQuote.DaoImpl.CustomerDaoImpl.findCustomerAll(CustomerDaoImpl.java:215)
at com.BeifengQuote.Action.CustomerFindAllAction.execute(CustomerFindAllAction.java:23)
at com.BeifengQuote.Servlet.WebContral.doPost(WebContral.java:57)
at com.BeifengQuote.Servlet.WebContral.doGet(WebContral.java:40)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
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.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:857)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Thread.java:619)

这是什么错误呀??
...全文
148 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
Dazer007 2013-11-05
  • 打赏
  • 举报
回复
我也遇到同样的问题 com.microsoft.sqlserver.jdbc.SQLServerException: '@P0' 附近有语法错误 解决: 在含有top的sql语句中把?变成原始的字符串拼接就好了
  • 打赏
  • 举报
回复
select top '"+要设置的条数 +"'
  • 打赏
  • 举报
回复
你那?是什么意思?
select '"+要设置的条数 +"'
awusoft 2012-09-28
  • 打赏
  • 举报
回复
selec top ?这里好像不能使用参数形式
tangkai007 2012-09-28
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

select top 0 可以么?
[/Quote]
select top 0 不报错,
select top 10 能正常显示
菖蒲老先生 2012-09-28
  • 打赏
  • 举报
回复
select top 0 可以么?
tangkai007 2012-09-28
  • 打赏
  • 举报
回复
String sql = "select top ? * from foodinfo where foodId not in(select top ? foodId from foodinfo order by foodId) order by foodId";

conn = this.getConn();
try {
pst = conn.prepareStatement(sql);
pst.setInt(1, 5);
pst.setInt(2, 5);
rs = pst.executeQuery()





不能这样写
在这里?占位符不起作用 你只能用连接字符串(报错)
String sql = "select top "+value+" * from foodinfo where foodId not in(select top"+value+" foodId from foodinfo order by foodId) order by foodId";



谢谢了我知道了
tangkai007 2012-09-28
  • 打赏
  • 举报
回复
用的式整形呀!没有错
ps.setInt(1, 4);
小小都不懂 2012-09-28
  • 打赏
  • 举报
回复
预处理吧
那是不是类型不匹配呢?
tangkai007 2012-09-28
  • 打赏
  • 举报
回复
我用的是PreparedStatement
用法是正确的呀!

62,614

社区成员

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

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