在jsp

时间:2015-07-30 13:19:17

标签: mysql sql jdbc

我遇到了一些错误,我试图将一些数据插入到mysql db中的表中,并且还希望同时从另一个表中删除一些数据。我正在使用jsp,jdbc。我无法理解为什么我会收到此错误,这是我的jsp页面的代码..

否则{                String sql1;

        try{


            con=DriverManager.getConnection(url,usr,passwd);

            String s1=request.getParameter("myText");
            String s2=request.getParameter("myText1");
            String s3=request.getParameter("myText2");
            String s4=request.getParameter("myText3");
            String s5=request.getParameter("myText4");
            String s6=request.getParameter("myText5");
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date issueDate = sdf.parse(s4);
            Date expiryDate=sdf.parse(s5);
            sql="insert into library.issue values (?,?,?,?,?,?)";

            ps=con.prepareStatement(sql);
            con.setAutoCommit(false);
            ps.setString(1,s1);
            ps.setString(2,s2);
            ps.setString(3,s3);
            ps.setDate(4, new java.sql.Date(issueDate.getTime()));
            ps.setDate(5, new java.sql.Date(expiryDate.getTime()));
            ps.setString(6,s6);
            ps.addBatch(sql);
            sql1="delete from library.books where book_id=?";
            ps.setString(1,s3);
            ps.addBatch(sql1);
            int[] count=ps.executeBatch();
            con.commit();
            response.sendRedirect("complete.jsp");



        }catch(SQLException e){
            e.printStackTrace();
            try{
                con.rollback();
            }catch(SQLException e1){
                e1.printStackTrace();
            }
        }finally{
            try{
                if(ps!=null ){
                    ps.close();

                }
            }catch(SQLException e1){
                e1.printStackTrace();
            }
            try{
                if(con!=null ){
                    con.close();

                }
            }catch(SQLException e2){
                e2.printStackTrace();
            }
        }
        }
    %>

我收到以下错误..

java.sql.SQLException:您的SQL语法中有错误;检查与您的MySQL服务器版本相对应的手册,以获得正确的语法,以便在'?,?,?,?,?,?)附近使用?'在第1行     在com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2975)     在com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1600)     在com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1695)     在com.mysql.jdbc.Connection.execSQL(Connection.java:3020)     在com.mysql.jdbc.Statement.executeUpdate(Statement.java:1074)     在com.mysql.jdbc.Statement.executeUpdate(Statement.java:1008)     在com.mysql.jdbc.ServerPreparedStatement.executeBatch(ServerPreparedStatement.java:582)     在org.apache.jsp.reaction2_jsp._jspService(reaction2_jsp.java:201)     在org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)     在javax.servlet.http.HttpServlet.service(HttpServlet.java:729)     在org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:438)     在org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)     在org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)     在javax.servlet.http.HttpServlet.service(HttpServlet.java:729)     在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)     在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)     在org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)     在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)     在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)     在org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)     在org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)     在org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)     在org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)     at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)     在org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:617)     在org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)     在org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)     在org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)     at org.apache.coyote.AbstractProtocol $ AbstractConnectionHandler.process(AbstractProtocol.java:668)     在org.apache.tomcat.util.net.AprEndpoint $ SocketProcessor.doRun(AprEndpoint.java:2463)     在org.apache.tomcat.util.net.AprEndpoint $ SocketProcessor.run(AprEndpoint.java:2452)     在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)     at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:615)     at org.apache.tomcat.util.threads.TaskThread $ WrappingRunnable.run(TaskThread.java:61)     在java.lang.Thread.run(Thread.java:744)

library是我的db名称,问题是我的tablename

1 个答案:

答案 0 :(得分:0)

您无法在PreparedStatement批处理中组合不同的SQL语句。因此,要么在一次交易中使用普通Statement或两个PreparedStatement