Mysql 使用 preparedstatement 从 Servlet 插入时出现语法错误
Mysql Syntax error while inserting from Servlet using preparedstatement
我正在尝试使用 servlet 将行插入 table,同时尝试使用 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)
删除prpStmt.execute(insertPrepQuery);
中的语句
必须是prpStmt.execute();
该消息表明准备好的语句作为常规语句执行。那是因为你用了
prpStmt.execute(insertPrepQuery);
而不是
prpStmt.execute();
或者更好,
prpStmt.executeUpdate();
您在准备语句时已经指定了查询。执行时无需再次指定
我正在尝试使用 servlet 将行插入 table,同时尝试使用 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)
删除prpStmt.execute(insertPrepQuery);
必须是prpStmt.execute();
该消息表明准备好的语句作为常规语句执行。那是因为你用了
prpStmt.execute(insertPrepQuery);
而不是
prpStmt.execute();
或者更好,
prpStmt.executeUpdate();
您在准备语句时已经指定了查询。执行时无需再次指定