尝试使用java数据库进行更新时的MySQLSyntaxErrorException

时间:2016-12-03 06:48:25

标签: java mysql jdbc

我在更新数据库时不断获得MySQLSyntaxErrorException

以下是错误的堆栈跟踪:

  

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:您的SQL语法中有错误;检查与您的MySQL服务器版本相对应的手册,以获得正确的语法,以便在'?,?,?,?,?,?,?)附近使用?'在第1行       at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)       at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)       at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)       at java.lang.reflect.Constructor.newInstance(Constructor.java:423)       在com.mysql.jdbc.Util.handleNewInstance(Util.java:411)       在com.mysql.jdbc.Util.getInstance(Util.java:386)       在com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1053)       在com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4120)       在com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4052)       在com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2503)       在com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2664)       在com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2788)       在com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2738)       在com.mysql.jdbc.StatementImpl.execute(StatementImpl.java:899)       在com.mysql.jdbc.StatementImpl.execute(StatementImpl.java:737)       在Lab6.StudentDB.writeToSQL(StudentDB.java:166)       在Lab6.StudentDB.add(StudentDB.java:83)       在Lab6.LabWeek6GUI.addJButtonActionPerformed(LabWeek6GUI.java:204)       在Lab6.LabWeek6GUI.access $ 000(LabWeek6GUI.java:16)       在Lab6.LabWeek6GUI $ 2.actionPerformed(LabWeek6GUI.java:68)       在javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)       在javax.swing.AbstractButton $ Handler.actionPerformed(AbstractButton.java:2348)       在javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)       在javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)       在javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)       at java.awt.Component.processMouseEvent(Component.java:6533)       在javax.swing.JComponent.processMouseEvent(JComponent.java:3324)       at java.awt.Component.processEvent(Component.java:6298)       at java.awt.Container.processEvent(Container.java:2236)       at java.awt.Component.dispatchEventImpl(Component.java:4889)       at java.awt.Container.dispatchEventImpl(Container.java:2294)       at java.awt.Component.dispatchEvent(Component.java:4711)       at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)       at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)       at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)       at java.awt.Container.dispatchEventImpl(Container.java:2280)       at java.awt.Window.dispatchEventImpl(Window.java:2746)       at java.awt.Component.dispatchEvent(Component.java:4711)       at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)       at java.awt.EventQueue.access $ 500(EventQueue.java:97)       at java.awt.EventQueue $ 3.run(EventQueue.java:709)       at java.awt.EventQueue $ 3.run(EventQueue.java:703)       at java.security.AccessController.doPrivileged(Native Method)       at java.security.ProtectionDomain $ JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)       at java.security.ProtectionDomain $ JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:90)       at java.awt.EventQueue $ 4.run(EventQueue.java:731)       at java.awt.EventQueue $ 4.run(EventQueue.java:729)       at java.security.AccessController.doPrivileged(Native Method)       at java.security.ProtectionDomain $ JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)       at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)       at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)       at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)       at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)       at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)       at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)       在java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

这是我的代码。

String sql = "INSERT INTO studentDB (id, name, grade, avg, test1, test2, test3) "
                    + "VALUES (?, ?, ?, ?, ?, ?, ?)";

stmt.execute(sql);

PreparedStatement preparedStatement = conn.prepareStatement(sql);
preparedStatement.setInt(1, id);
preparedStatement.setString(2, name);
preparedStatement.setString(3, grade);
preparedStatement.setDouble(4, avg);
preparedStatement.setDouble(5, test1);
preparedStatement.setDouble(6, test2);
preparedStatement.setDouble(7, test3);
preparedStatement.executeUpdate();

1 个答案:

答案 0 :(得分:1)

您的例外来自stmt.execute(sql);,请将其删除。您之后似乎正确使用了PreparedStatement(但由于前面提到的stmt.execute(sql)试图在未设置绑定参数的情况下运行查询,因此您的代码尚未到达。