使用preparedstatement从Servlet插入时出现Mysql语法错误
我正在尝试使用servlet将行插入表中,同时尝试使用Statement插入(使用insertQuery1&insertQuery2),它正在执行正常但在使用preparedstatment(使用insertPrepQuery)执行时抛出SQL语法错误。 码:
public void printout(Assetdetail assdet)抛出SQLException,ClassNotFoundException {String driver =“com.mysql.jdbc.Driver”; String url =“jdbc:mysql:// localhost / assetmgnt”; ResultSet结果= 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)”+“值(?,?,?,?,?,?,?)”; 的Class.forName(驱动器); Connection conn = DriverManager.getConnection(url,User,Password); // -------------使用statment / * Statement stmt = conn.createStatement(); int insertStmtCount = stmt.executeUpdate(insertQuery2); System.out.println(“插入的行数为:”+ insertStmtCount); stmt.close(); * / // -------------使用Prepared statment System.out.println(“Preparation 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(“插入值”); // int insertPrpCount = prpStmt.executeUpdate(insertPrepQuery); //System.out.println("No of Rows inserted is:“+ insertPrpCount); boolean rs = prpStmt.execute(insertPrepQuery); System.out.println(“行插入:”+ rs); prpStmt.close(); conn.close(); }}
错误:
com.mysql.jdbc.exceptions.MySQLSyntaxErrorException:您的SQL语法中有错误; 查看与您的MySQL服务器版本相对应的手册,以便在第1行“?,?,?,?,?,?,?)'附近使用正确的语法 在com.mysql.jdbc.SQLError.createSQLException(SQLError.java:936) 在com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2985) 在com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1631) 在com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1723) 在com.mysql.jdbc.Connection.execSQL(Connection.java:3277) 在com.mysql.jdbc.Connection.execSQL(Connection.java:3206) 在com.mysql.jdbc.Statement.execute(Statement.java:727) 在org.gk.assetmgment.servlet.AssetAddAuth.printout(AssetAddAuth.java:121) 在org.gk.assetmgment.servlet.AssetAddAuth.doPost(AssetAddAuth.java:58) 在javax.servlet.http.HttpServlet.service(HttpServlet.java:646) 在javax.servlet.http.HttpServlet.service(HttpServlet.java:727) 在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303) 在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) 在org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) 在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241) 在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) 在org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220) 在org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122) 在org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:503) 在org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170) 在org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103) 在org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950) 在org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116) 在org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:421) 在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) 在java.lang.Thread.run(未知来源)
删除prpStmt.execute(insertPrepQuery);
中的语句prpStmt.execute(insertPrepQuery);
它必须是prpStmt.execute();
该消息显示准备好的语句作为常规语句执行。 那是因为你用过
prpStmt.execute(insertPrepQuery);
代替
prpStmt.execute();
或更好,
prpStmt.executeUpdate();
您在准备语句时已经指定了查询。 执行时无需再次指定。