2011-05-01 6 views
1

je la chaîne suivante:Exceptions à préparer la déclaration en java pour une mise à jour MySQL

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

J'utilise ensuite le code suivant pour préparer et utiliser cette déclaration:

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(); 

Quand je lance mon Test Junit Je reçois l'erreur suivante:

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Vous avez une erreur dans votre syntaxe SQL; consultez le manuel qui correspond à votre version du serveur MySQL pour la bonne syntaxe à utiliser près de 'time = 240, trackNum = 11, année =' 2007 ', numlikes = 0, numdislikes = 0, où s' à la ligne 1

Il semble que l'instruction préparée ajoute un, avant la clause where. Je pense que c'est la cause du problème, mais je ne suis pas sûr de savoir pourquoi cela se présente. Quelqu'un a-t-il un aperçu de ce problème?

Modifier: J'ai corrigé le problème de la virgule manquante après le genre =? et j'ai toujours le problème. Merci d'avoir signalé ce problème.

L'erreur que je reçois après avoir ajouté la nouvelle virgule est:

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) 
+0

J'ai ajouté la virgule après genre =? et j'ai toujours le problème. – PFranchise

+0

Est-ce qu'il affiche le même message? '... near 'time = 240 ...' indiquait la virgule manquante. – Thomas

+0

@Thomas Oui monsieur, l'erreur est restée la même après avoir ajouté la virgule. – PFranchise

Répondre

3

"name = ?, artist = ?, albumName = ?, genre = ? " la virgule est manquante.

Votre SQL serait comme ... , albumName = ?, genre = ? time = ?, trackNum = ?, ...

+0

Merci! En fait, je viens de le remarquer, mais j'ai toujours le problème. – PFranchise

2

Vous avez oublié une virgule après genre = ? et avant time = ?

0

Il vous manque une virgule après la mise genre.

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