Problème de requête
Résolu/Fermé2 réponses
NHenry
Messages postés
15047
Date d'inscription
vendredi 14 mars 2003
Statut
Modérateur
Dernière intervention
11 mars 2023
331
20 avril 2016 à 22:43
20 avril 2016 à 22:43
Je pense qu'il faut que tu concatène ta chaine ('Select ... ') avec ta variable pour faire ce que tu veux.
KX
Messages postés
16668
Date d'inscription
samedi 31 mai 2008
Statut
Modérateur
Dernière intervention
17 mars 2023
3 005
21 avril 2016 à 13:43
21 avril 2016 à 13:43
Bonjour,
Il te faut un Prepared Statement.
https://docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html
Il te faut un Prepared Statement.
https://docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html
String sql = "INSERT INTO javadb (personne) VALUES (?)"; PreparedStatement ps = con.prepareStatement(sql); ps.setString(1, nom); // "nom" est une variable de type String ps.executeUpdate();
Bonjour,
Merci pour votre réponse, j'obtiens une erreur par contre :/ :
com.mysql.jdbc.exceptions.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 '?)' at line 1
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:936)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2941)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1623)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1715)
at com.mysql.jdbc.Connection.execSQL(Connection.java:3243)
at com.mysql.jdbc.Statement.executeUpdate(Statement.java:1343)
at com.mysql.jdbc.Statement.executeUpdate(Statement.java:1260)
at com.objis.demojdbc.DemoJdbc.sauverEnBase(DemoJdbc.java:34)
at com.objis.demojdbc.DemoJdbc.main(DemoJdbc.java:12)
Merci pour votre réponse, j'obtiens une erreur par contre :/ :
com.mysql.jdbc.exceptions.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 '?)' at line 1
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:936)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2941)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1623)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1715)
at com.mysql.jdbc.Connection.execSQL(Connection.java:3243)
at com.mysql.jdbc.Statement.executeUpdate(Statement.java:1343)
at com.mysql.jdbc.Statement.executeUpdate(Statement.java:1260)
at com.objis.demojdbc.DemoJdbc.sauverEnBase(DemoJdbc.java:34)
at com.objis.demojdbc.DemoJdbc.main(DemoJdbc.java:12)
KX
Messages postés
16668
Date d'inscription
samedi 31 mai 2008
Statut
Modérateur
Dernière intervention
17 mars 2023
3 005
22 avril 2016 à 17:09
22 avril 2016 à 17:09
Tu as :
Si tu avais utilisé le code que j'ai mis, en cas d'erreur tu aurais du avoir :
De plus ton import doit pas être bon, tu as du importer com.mysql.jdbc.* mais c'est java.sql.* qu'il faut importer !
at com.mysql.jdbc.Statement.executeUpdatec'est à dire que tu as directement exécuté la requête avec "?" sans remplacer la valeur.
Si tu avais utilisé le code que j'ai mis, en cas d'erreur tu aurais du avoir :
at java.sql.PreparedStatement.executeUpdate()
De plus ton import doit pas être bon, tu as du importer com.mysql.jdbc.* mais c'est java.sql.* qu'il faut importer !
21 avril 2016 à 13:36
21 avril 2016 à 13:43
Modifié par KX le 21/04/2016 à 14:01
Exemple :
Si je concatène comme ceci :
String sql = "INSERT INTO javadb (personne) VALUES ('"+nom+"')";Cela donne ceci :
INSERT INTO javadb (personne) VALUES (''); DROP javadb; --')Et bim ! Ça supprime toute la table !