为了账号安全,请及时绑定邮箱和手机立即绑定

jsp实现数据库查询,prepareStatement()参数设置;

jsp实现数据库查询,prepareStatement()参数设置;

慕村9548890 2019-02-16 09:11:38
我想传递一个参数,用该参数实现一个数据库的查询,但是报错:No value specified for parameter 1java代码 public List<StudentInfo>findAll(int b) { List<StudentInfo>l=new ArrayList<StudentInfo>(); try{ Connection conn =getConnection(); String sql ="select * from studentinfo where studentid=?"; PreparedStatement ps = conn.prepareStatement(sql); ps.setInt(1, b); CallableStatement cs=conn.prepareCall(sql); ResultSet rs=cs.executeQuery(); while(rs.next()){ StudentInfo s=new StudentInfo(); s.setId(rs.getInt("studentid")); s.setName(rs.getString("studentname")); s.setClassid(rs.getInt("classid")); s.setPassword(rs.getString("password")); s.setSex(rs.getInt("sex")); s.setAddress(rs.getString("address")); s.setTel(rs.getDouble("tel")); l.add(s); } }catch(Exception e){ e.printStackTrace(); } return l; } jsp代码 <jsp:useBean id="c" class="com.Bean.F_stu_info"></jsp:useBean> <% int a=2016010101; List<StudentInfo>l=c.findAll(a); if(l==null||l.size()<1){ out.print("没有数据"); }else{ for(StudentInfo b:l){ %> 报错信息 java.sql.SQLException: No value specified for parameter 1 at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:957) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:896) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:885) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:860) at com.mysql.jdbc.PreparedStatement.checkAllParametersSet(PreparedStatement.java:2205) at com.mysql.jdbc.PreparedStatement.fillSendPacket(PreparedStatement.java:2185) at com.mysql.jdbc.PreparedStatement.fillSendPacket(PreparedStatement.java:2115) at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1936) at com.mysql.jdbc.CallableStatement.executeQuery(CallableStatement.java:858) at com.Bean.F_stu_info.findAll(F_stu_info.java:46) at org.apache.jsp.deal_005f1_005f2_jsp._jspService(deal_005f1_005f2_jsp.java:113) at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) at javax.servlet.http.HttpServlet.service(HttpServlet.java:731) at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:439) at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395) at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339) at javax.servlet.http.HttpServlet.service(HttpServlet.java:731) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103) at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:956) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:436) at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1078) at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:625) at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:318) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.lang.Thread.run(Thread.java:745)
查看完整描述

2 回答

?
梵蒂冈之花

TA贡献1900条经验 获得超5个赞

PreparedStatement ps = conn.prepareStatement(sql);
ps.setInt(1, b);
CallableStatement cs=conn.prepareCall(sql);

这里错了,cs执行query的时候,实际上没有指定参数。
而且你实际上直接用ps去执行query就行了。不用创建那个cs。

查看完整回答
反对 回复 2019-03-01
  • 2 回答
  • 0 关注
  • 1124 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信