Use? PLACEHOLDER write MySQL query statement, execute error
1 Com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:You has an error in your SQL syntax; Check the manual, corresponds to your MySQL server version for the right syntax to use near '?,?,?,?) ' At line 1 2 @ sun.reflect.NativeConstructorAccessorImpl.newInstance0 (Native Method) 3 at Sun.reflect.NativeConstr Uctoraccessorimpl.newinstance (nativeconstructoraccessorimpl.java:57) 4 at Sun.reflect.DelegatingConstructorAccessorImpl.newInstance (delegatingconstructoraccessorimpl.java:45) 5 at Java.lang.reflect.Constructor.newInstance (constructor.java:526) 6 at Com.mysql.jdbc.Util.handleNewInstance ( util.java:411) 7 at Com.mysql.jdbc.Util.getInstance (util.java:386) 8 at Com.mysql.jdbc.SQLError.createSQLException (sqlerror.java:1054) 9 at Com.mysql.jdbc.MysqlIO.checkErrorPacket (mysqlio.java:4190) at COM.MYSQL.JDBC.MYSQLIO.C Heckerrorpacket (mysqlio.java:4122) at Com.mysql.jdbc.MysqlIO.sendCommand (mysqlio.java:2570) at Com.mysql.jdbc. MysqliO.sqlquerydirect (mysqlio.java:2731) at Com.mysql.jdbc.ConnectionImpl.execSQL (connectionimpl.java:2812) in COM. Mysql.jdbc.ConnectionImpl.execSQL (connectionimpl.java:2761) at Com.mysql.jdbc.StatementImpl.execute ( statementimpl.java:894) at Com.mysql.jdbc.StatementImpl.execute (statementimpl.java:732), at com.eloancn.test.ru N.conntecttest.insert (conntecttest.java:67) at Com.eloancn.test.run.ConntectTest.main (conntecttest.java:44)
Two methods involved in testing:
Method One:
1 Public Static voidMain (string[] args) {2 Try {3Class.forName ("Com.mysql.jdbc.Driver");4SYSTEM.OUT.PRINTLN ("Load succeeded");5}Catch(ClassNotFoundException e) {6 System.out.println (e);7 e.printstacktrace ();8 }9InputStream in =NULL;Ten Try {
Load local Database link data Onein =NewFileInputStream ("Src/main/resources/jdbc.properties"); A}Catch(FileNotFoundException e) { - e.printstacktrace (); - } theProperties pp =NewProperties (); - Try { - pp.load (in); -}Catch(IOException e) { + e.printstacktrace (); - } +Object obj =NULL; AarraylistNewArraylist(); at Try { -Connection conn = drivermanager.getconnection (Pp.getproperty ("Jdbc.url"), Pp.getproperty ("Jdbc.username"), Pp.getproperty ("Jdbc.password")); -Conn.setautocommit (false); - String sql = "INSERT into D_important_loandatum_score (tenderid,photoid,datumid,showclient)" - + "VALUES (?,?,?,?)"; - INSERT (conn, SQL, new object[] {1, "3", 2, 3 }); in conn.commit (); -}Catch(Exception e) { to e.printstacktrace (); + } - the}
Method Two: Perform an insert
1 Public Static BooleanInsert (Connection conn, String SQL, object[] paras)throwsException {2PreparedStatement statement =NULL;3String str = "";4statement =conn.preparestatement (SQL);5 if(Paras! =NULL&& Paras.length! = 0) {6 for(inti = 0; i < paras.length; i++) {7 if(Paras[i].getclass (). Getsimplename (). Equals ("Integer")) {8Statement.setint (i + 1), (Integer) paras[i]);9str+=Paras[i];Ten}Else { OneStatement.setstring (i + 1), (String) paras[i]); Astr+=Paras[i]; - } - } the } - Boolean result = Statement.execute (sql); - //logger.info ("sql:" +sql+ "paras:" +str); - returnresult; +}
If the previous two methods are put into the class, perform an error. After multiple lookups, the final confirmation is that the insertion method has a red part written error, modified to:boolean result = Statement.execute ();
Save, re-execute, normal.
PS: Also use preparement precompiled SQL, query, modify and other call methods, the final execution can not compile SQL again, is the MySQL internal encoding setup problem, there will be an exception.
You have a error in your SQL syntax; check the manual, corresponds to your MySQL server version for the right Synt Ax to use near '?,?,?,?) '