【问题标题】:Exceptions with prepare statement in java for a mysql updatejava中用于mysql更新的prepare语句异常
【发布时间】:2011-05-01 21:16:27
【问题描述】:

我有以下字符串:

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)

【问题讨论】:

  • 我在genre=?后面加了逗号我仍然有这个问题。
  • 它是否显示相同的信息? ... near 'time = 240 ... 表示缺少逗号。
  • @Thomas 是的先生,我添加逗号后错误仍然存​​在。
  • 是同一个异常还是同一个消息?请再次发布它,然后再检查您是否真的重新编译了代码(我认为代码没有被重新编译的问题太频繁了)。
  • @Thomas 我重新编译了代码并更新了我的问题。我包括了完整的跟踪,但我不确定它是否可以告诉我们更多信息。感谢您一直以来的帮助。

标签: java mysql prepared-statement


【解决方案1】:

"name = ?, artist = ?, albumName = ?, genre = ? " 缺少最后一个逗号。

你的 SQL 应该是 ... , albumName = ?, genre = ? time = ?, trackNum = ?, ...

【讨论】:

  • 谢谢!实际上我也注意到了这一点,但我仍然遇到问题。
【解决方案2】:

您在genre = ?time = ? 之前忘记了逗号

【讨论】:

    【解决方案3】:

    您在设置流派后缺少逗号。

      public static final String UPDATESONG = "update songs SET " +
        "name = ?, artist = ?, albumName = ?, genre = ?, " +
        "time = ?, trackNum = ?, year = ?, numlikes = ?, numdislikes = ? where song_id = ?";
    

    【讨论】:

      猜你喜欢
      • 2013-11-17
      • 2017-09-28
      • 1970-01-01
      • 1970-01-01
      • 2011-08-19
      • 1970-01-01
      • 2016-01-17
      • 1970-01-01
      • 2013-05-28
      相关资源
      最近更新 更多