首页 文章

使用preparedstatement从Servlet插入时出现Mysql语法错误

提问于
浏览
3

我试图使用servlet将行插入表中,同时尝试使用Statement插入(使用insertQuery1&insertQuery2),它正在执行正常但在使用preparedstatment(使用insertPrepQuery)执行时抛出SQL语法错误 . 码:

public void printout( Assetdetail assdet) throws  SQLException, ClassNotFoundException{
        String driver = "com.mysql.jdbc.Driver";
        String url = "jdbc:mysql://localhost/assetmgnt";
        ResultSet result = null;
        String User = "root";
        String Password = "";
//      String insertQuery1 =
//              "INSERT INTO assetdetails (ASSETNAME,ASSETST,ASSETUNIT,ASSETADD1,ASSETADD2,ASSETPINCODE,ASSETDOORNUM) VALUES " + 
//                                                              "( \'MYASSET2\',\'STREET3\',34,\'ASST2ADD1\',\'ASST2ADD2\',600029,\'#34\')"; 
        String insertQuery2 =
                "INSERT INTO assetdetails (ASSETNAME,ASSETST,ASSETUNIT,ASSETADD1,ASSETADD2,ASSETPINCODE,ASSETDOORNUM) VALUES " + 
                                                                "("                                      +
                                                                "\'" + assdet.getAssetname() + "\'"                   +
                                                                 ",\'" + assdet.getAssetstreetname() + "\'"              +
                                                                 "," + Integer.parseInt(assdet.getAssetunitnumber()) +
                                                                 ",\'" + assdet.getAssetaddln1()+ "\'"                  +
                                                                 ",\'" + assdet.getAssetaddln2()+ "\'"                  +
                                                                 "," + Integer.parseInt(assdet.getAssetpincode()) + 
                                                                 ",\'" + assdet.getAssetdoornum()+ "\'" + 
                                                                ")";


        String insertPrepQuery =
                "insert into assetdetails (ASSETNAME,ASSETST,ASSETUNIT,ASSETADD1,ASSETADD2,ASSETPINCODE,ASSETDOORNUM)" + 
                "values (?,?,?,?,?,?,?)";

        Class.forName(driver);
        Connection conn = DriverManager.getConnection(url,User,Password);


        //-------------Using statment

        /*Statement stmt = conn.createStatement();
        int insertStmtCount = stmt.executeUpdate(insertQuery2);
        System.out.println("No of Rows inserted is : " + insertStmtCount);
        stmt.close();*/

        //-------------Using Prepared statment 

        System.out.println(" Preparing statment");
        java.sql.PreparedStatement prpStmt = conn.prepareStatement(insertPrepQuery);
        prpStmt.setString(1, assdet.getAssetname());
        prpStmt.setString(2, assdet.getAssetstreetname());
        prpStmt.setInt(3,14);
        prpStmt.setString(4, assdet.getAssetaddln1());
        prpStmt.setString(5, assdet.getAssetaddln2());
        prpStmt.setInt(6,500029);
        prpStmt.setString(7,assdet.getAssetdoornum());


        System.out.println(" Inserting Values");
        //int insertPrpCount = prpStmt.executeUpdate(insertPrepQuery);
        //System.out.println("No of Rows inserted is : " + insertPrpCount);
        boolean rs = prpStmt.execute(insertPrepQuery);
        System.out.println("Rows inserted : " + rs);
        prpStmt.close();


        conn.close();
       }
}

错误:

com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?,?,?,?,?,?,?)' at line 1
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:936)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2985)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1631)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1723)
    at com.mysql.jdbc.Connection.execSQL(Connection.java:3277)
    at com.mysql.jdbc.Connection.execSQL(Connection.java:3206)
    at com.mysql.jdbc.Statement.execute(Statement.java:727)
    at org.gk.assetmgment.servlet.AssetAddAuth.printout(AssetAddAuth.java:121)
    at org.gk.assetmgment.servlet.AssetAddAuth.doPost(AssetAddAuth.java:58)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
    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:503)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:421)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:314)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Unknown Source)

2 回答

  • 1

    删除 prpStmt.execute(insertPrepQuery); 中的语句

    它一定是 prpStmt.execute();

  • 0

    该消息显示准备好的语句作为常规语句执行 . 那是因为你用过

    prpStmt.execute(insertPrepQuery);
    

    代替

    prpStmt.execute();
    

    或更好,

    prpStmt.executeUpdate();
    

    您在准备语句时已经指定了查询 . 执行时无需再次指定 .

相关问题