我有以下字符串:
public static final String UPDATESONG = "update songs SET " +
"name = ?, artist = ?, albumName = ?, genre = ?, " +
"time = ?, trackNum = ?, year = ?, numlikes = ?, numdislikes = ? where song_id = ?";
然后我使用以下代码来准备和使用此语句:
ps = conn.prepareStatement(AppConstants.Queries.UPDATESONG);
ps.setString(1, name);
ps.setString(2, artist);
ps.setString(3, albumName);
ps.setString(4, genre);
ps.setInt(5, time);
ps.setInt(6, trackNumber);
ps.setString(7, year);
ps.setInt(8, numberOfLikes);
ps.setInt(9, numberOfDislikes);
ps.setInt(10, id);
ps.executeUpdate();
当我运行 Junit 测试时,出现以下错误:
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:你的 SQL 语法有错误;查看与您的 MySQL 服务器版本对应的手册,了解在 'time = 240, trackNum = 11, year = '2007', numlikes = 0, numdislikes = 0, where s' at line 1 附近使用的正确语法
准备好的语句似乎在 where 子句之前添加了一个 , 。我认为这是问题的原因,但我不确定它为什么会出现。有人对这个问题有任何见解吗?
编辑:我更正了 genre=? 后缺少逗号的问题。我仍然有问题。感谢您指出该问题。
添加新逗号后出现的错误是:
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 'time = 240, trackNum = 11, year = '2007', numlikes = 0, numdislikes = 0, where s' at line 1
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
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:2626)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2119)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2415)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2333)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2318)
at SongDAO.updateSong(SongDAO.java:155)
at SongTest.testUpdateSong(SongTest.java:58)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at junit.framework.TestCase.runTest(TestCase.java:168)
at junit.framework.TestCase.runBare(TestCase.java:134)
at junit.framework.TestResult$1.protect(TestResult.java:110)
at junit.framework.TestResult.runProtected(TestResult.java:128)
at junit.framework.TestResult.run(TestResult.java:113)
at junit.framework.TestCase.run(TestCase.java:124)
at junit.framework.TestSuite.runTest(TestSuite.java:243)
at junit.framework.TestSuite.run(TestSuite.java:238)
at junit.framework.TestSuite.runTest(TestSuite.java:243)
at junit.framework.TestSuite.run(TestSuite.java:238)
at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:83)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
最佳答案
“name = ?, artist = ?, albumName = ?, genre = ?”
缺少最后一个逗号。
您的 SQL 会像 ... , albumName = ?, genre = ?时间 = ?, trackNum = ?, ...
关于java - java 中用于 mysql 更新的 prepare 语句异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5851469/