我想传递一个参数,用该参数实现一个数据库的查询,但是报错:No value specified for parameter 1
java代码:
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)
`PreparedStatement ps = conn.prepareStatement(sql);
ps.setInt(1, b);
CallableStatement cs=conn.prepareCall(sql);`
这里错了,cs执行query的时候,实际上没有指定参数。
而且你实际上直接用ps去执行query就行了。不用创建那个cs。
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。