开发者社区> 问答> 正文

jsp实现数据库查询prepareStatement()参数设置问题

我想传递一个参数,用该参数实现一个数据库的查询,但是报错: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)

展开
收起
蛮大人123 2016-03-11 15:24:02 3435 0
1 条回答
写回答
取消 提交回答
  • 我说我不帅他们就打我,还说我虚伪

    `PreparedStatement ps = conn.prepareStatement(sql);
    ps.setInt(1, b);
    CallableStatement cs=conn.prepareCall(sql);`
    这里错了,cs执行query的时候,实际上没有指定参数。
    而且你实际上直接用ps去执行query就行了。不用创建那个cs。

    2019-07-17 18:59:23
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
DTCC 2022大会集锦《云原生一站式数据库技术与实践》 立即下载
阿里云瑶池数据库精要2022版 立即下载
2022 DTCC-阿里云一站式数据库上云最佳实践 立即下载