JDBC 使用正确的语句返回 MySQLSyntaxError 异常

发布于 2024-12-07 03:23:36 字数 4364 浏览 0 评论 0原文

我正在用 java 编写一个与 MySQL 数据库通信的应用程序,当我测试它时,我注意到在表中插入新行的代码抛出了 MySQLSyntaxError 异常,因此我尝试使用 MySQL Workbench 执行相同的 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)

I'm writing an application in java which communicates with a MySQL db, and while I was testing it I noticed that the code to insert new rows in a table threw a MySQLSyntaxError Exception, so I tried to execute the same INSERT using MySQL Workbench, and it worked. The code in question is this:

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;
}

EDIT: sorry, the stack trace is:

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

战皆罪 2024-12-14 03:23:36

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,了解在 '? 附近使用的正确语法。 , ? , DEFAULT , NULL )' 在第 1 行

这些占位符 ? 根本不应该出现在 MySQL 端。

看这里,

sInserim.executeUpdate(sqlCommandInserim);

您将原始 SQL 字符串传递到 executeUpdate() 中,而不是使用设置的值执行 PreparedStatement

将其替换为

sInserim.executeUpdate();

executeUpdate(sqlString) 只能在 Statement 上使用。


与具体问题无关,您应该关闭finally 块中的PreparedStatement,以防止出现异常时资源泄漏。顺便说一句,这同样适用于 ConnectionStatementResultSet

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

Those placeholders ? should not appear in the MySQL side at all.

Look here,

sInserim.executeUpdate(sqlCommandInserim);

you're passing the raw SQL string into executeUpdate() instead of executing the PreparedStatement with the set values.

Replace it by

sInserim.executeUpdate();

The executeUpdate(sqlString) should be used on Statement only.


Unrelated to the concrete problem, you should be closing the PreparedStatement in the finally block to prevent resource leaking in case of exceptions. The same applies to Connection, Statement and ResultSet by the way.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文