[Microsoft][SQLServer 2000 Driver for JDBC]Invalid parameter binding(s) 错误求解决~~代码都复制出来

Stringname=request.getParameter("name");Stringtitle=request.getParameter("title");Str... String name = request.getParameter("name");
String title = request.getParameter("title");
String querySql = "insert into tb_guestbook(name,title) values(?,?)";
String [] parms = new String[] {name,title};
Context context;
try {
context = new InitialContext();
DataSource ds = (DataSource)context.lookup("java:/comp/env/jdbc/TTRMSDB");
Connection conn = ds.getConnection();

PreparedStatement pstmt = conn.prepareStatement(querySql);
System.out.println(parms[1]);
for(int i=0; i<parms.length; i++){
pstmt.setString((i+1), parms[i]);
pstmt.executeUpdate();
}

// pstmt.setString(1, name);
// pstmt.setString(2, title);

pstmt.executeUpdate();
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

错误提示:
java.sql.SQLException:[Microsoft][SQLServer 2000 Driver for JDBC]Invalid parameter binding(s).
at com.microsoft.jdbc.base.BaseExceptions.createException(Unknown Source)
at com.microsoft.jdbc.base.BaseExceptions.getException(Unknown Source)
at com.microsoft.jdbc.base.BasePreparedStatement.validateParameters(Unknown Source)
at com.microsoft.jdbc.base.BasePreparedStatement.validateParameters(Unknown Source)
at com.microsoft.jdbc.base.BasePreparedStatement.preImplExecute(Unknown Source)
at com.microsoft.jdbc.base.BaseStatement.commonExecute(Unknown Source)
at com.microsoft.jdbc.base.BaseStatement.executeUpdateInternal(Unknown Source)
at com.microsoft.jdbc.base.BasePreparedStatement.executeUpdate(Unknown Source)
at org.apache.tomcat.dbcp.dbcp.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:102)
at com.tiantong.servlet.GuestbookServlet.doPost(GuestbookServlet.java:43)
at com.tiantong.servlet.BusinessServlet.doGet(BusinessServlet.java:58)
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:128)
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:286)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:845)
展开
 我来答
百度网友96b20a22d
2011-11-16
知道答主
回答量:19
采纳率:0%
帮助的人:23.6万
展开全部
首先肯定的是错误提示是你的参数有错误Invalid parameter binding(s).

请看下面两句代码 for(int i=0; i<parms.length; i++){
pstmt.setString((i+1), parms[i]);
pstmt.executeUpdate();//这句话出问题了
}
第一次for循环进来后,你通过pstmt.setString((i+1), parms[i]);只往进设置了一个参数,就执行了
pstmt.executeUpdate();由于String querySql = "insert into tb_guestbook(name,title) values(?,?)";中需要两个参数,所以执行pstmt.executeUpdate()这句话就会出问题了。

解决办法:for(int i=0; i<parms.length; i++){
pstmt.setString((i+1), parms[i]);
}
pstmt.executeUpdate();//将这句话放在for循环外即可
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式