JDBC返回MySQLSyntaxError用正确的说法异常JDBC返回MySQLSyntaxErr

2019-05-12 14:52发布

我用Java编写与一个MySQL数据库通信的应用程序,虽然我是测试它,我注意到代码中插入表中的新行扔了MySQLSyntaxError异常,所以我试图执行使用MySQL工作台相同的INSERT,和它的工作。 有问题的代码是这样的:

public static boolean aggiungiElem(String nome, GrafoGenerico g){
    if(connessioneAperta()){
        try{
            String sqlCommandUser="SELECT USER()";
            String sqlCommandInserim="INSERT INTO salvataggi VALUES ( ? , ? , DEFAULT , NULL );";
            PreparedStatement sUser=conn.prepareStatement(sqlCommandUser);
            ResultSet risultatiUtente=sUser.executeQuery();
            String utente = null;
            while(risultatiUtente.next()){
                utente=risultatiUtente.getString(1);
            }
            sUser.close();
            PreparedStatement sInserim=conn.prepareStatement(sqlCommandInserim);
            sInserim.setString(1, utente);
            sInserim.setString(2, nome);
            //sInserim.setObject(3,g);
            System.out.println(sInserim.toString());
            sInserim.executeUpdate(sqlCommandInserim);
            sInserim.close();
            return true;
        }
        catch(SQLException e){
            e.printStackTrace();
            return false;
        }
    }
    else
        return false;
}

编辑:对不起,堆栈跟踪是:

com.mysql.jdbc.exceptions.jdbc4.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 '? , ? , DEFAULT , NULL )' at line 1
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:407)
at com.mysql.jdbc.Util.getInstance(Util.java:382)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1052)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3603)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3535)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1989)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2150)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2620)
at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1664)
at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1583)
at se.diag.control.clientspec.UtilsClientSQL.aggiungiElem(UtilsClientSQL.java:67)
at se.diag.control.clientspec.UtilsClientSQLTest.testAggiungiElem(UtilsClientSQLTest.java:67)
at se.diag.control.clientspec.UtilsClientSQLTest.testCaricaElem(UtilsClientSQLTest.java:99)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.internal.runners.TestMethodRunner.executeMethodBody(TestMethodRunner.java:99)
at org.junit.internal.runners.TestMethodRunner.runUnprotected(TestMethodRunner.java:81)
at org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:34)
at org.junit.internal.runners.TestMethodRunner.runMethod(TestMethodRunner.java:75)
at org.junit.internal.runners.TestMethodRunner.run(TestMethodRunner.java:45)
at org.junit.internal.runners.TestClassMethodsRunner.invokeTestMethod(TestClassMethodsRunner.java:66)
at org.junit.internal.runners.TestClassMethodsRunner.run(TestClassMethodsRunner.java:35)
at org.junit.internal.runners.TestClassRunner$1.runUnprotected(TestClassRunner.java:42)
at org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:34)
at org.junit.internal.runners.TestClassRunner.run(TestClassRunner.java:52)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:45)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)

Answer 1:

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:您的SQL语法错误; 检查对应于你的MySQL服务器版本使用附近的“正确的语法手册? ,? ,DEFAULT,NULL)”在第1行

这些占位符? 不应该出现在MySQL的侧面都没有。

看这里,

sInserim.executeUpdate(sqlCommandInserim);

你传递的原始SQL字符串转换成executeUpdate()而不是执行PreparedStatement与设定值。

通过替换它

sInserim.executeUpdate();

executeUpdate(sqlString)应使用Statement而已。


无关的具体问题,您应该关闭PreparedStatementfinally阻止,以避免资源在例外的情况下,泄漏。 这同样适用于ConnectionStatementResultSet的方式。



文章来源: JDBC returning MySQLSyntaxError Exception with correct statement
标签: java mysql jdbc