Insertion des données dans une base MySQL

Résolu/Fermé
maroun_ba Messages postés 11 Date d'inscription samedi 9 février 2013 Statut Membre Dernière intervention 5 avril 2013 - 15 févr. 2013 à 11:48
arth Messages postés 9374 Date d'inscription mardi 27 septembre 2005 Statut Contributeur Dernière intervention 16 décembre 2016 - 15 févr. 2013 à 21:34
Bonjour,
je veux ajouter des données à ma base MySQL en utilisant le langage java mais le code ne marche plus.
voici le code
package com.controle.test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class connection {
public static void main(String[] args) {
String url = "com.mysql.jdbc.Driver";
Connection con = null ;
Statement s = null;
try {

System.out.println("Connection au driver JDBC");
//moyPaiement=txtmoy.getSelectedText();
Class.forName(url).newInstance();
System.out.println("Chargement du pilote Mysql réussi");

con = DriverManager.getConnection("jdbc:mysql://localhost:3306/base1","aidi","aidi");
//insertion
s = con.createStatement();
String str = "INSERT INTO utilisateur(id,email,mot_de_passe,nom,date_inscription) VALUES (3,'user3@ensi.com', MD5('password3'), 'user3', NOW());)" ;
s.executeUpdate(str);
//System.out.println(str+" ligne insérée");
}
catch(Exception e) {System.out.println("Exception");}
finally {
if ( s != null ) {
try {
con.close();
} catch ( SQLException ignore ) {
}
if ( con != null ) {
try {
con.close();
} catch ( SQLException ignore ) {
}
}}
}}}
a console affiche :
Connection au driver JDBC
Chargement du pilote Mysql réussi
Exception

merci de m'aider.

6 réponses

Chocobo_tofu1 Messages postés 162 Date d'inscription lundi 1 mars 2010 Statut Membre Dernière intervention 10 juillet 2014 362
15 févr. 2013 à 12:26
bonjour,

Commence par remplacé le

catch(Exception e) {System.out.println("Exception");} 

par
catch(Exception e) {e.printStackTrace( );} 


et les
catch ( SQLException ignore ) { }

par
catch(SQLException sqle) {sqle.printStackTrace( );} 


et poste ce que t'affiches la console java
0
maroun_ba Messages postés 11 Date d'inscription samedi 9 février 2013 Statut Membre Dernière intervention 5 avril 2013 1
15 févr. 2013 à 14:07
voilà l'erreur
Connection au driver JDBC
Chargement du pilote Mysql réussi
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 ')' at line 1
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.Util.getInstance(Util.java:386)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1053)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4096)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4028)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2490)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2651)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2728)
at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1811)
at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1725)
at com.controle.test.connection.main(connection.java:24)
et merci de m'aider
0
Chocobo_tofu1 Messages postés 162 Date d'inscription lundi 1 mars 2010 Statut Membre Dernière intervention 10 juillet 2014 362
Modifié par Chocobo_tofu1 le 15/02/2013 à 14:35
essaie
"INSERT INTO utilisateur(id,email,mot_de_passe,nom,date_inscription) VALUES (3,'user3@ensi.com', MD5('password3'), 'user3', NOW())" 

au lieu de
"INSERT INTO utilisateur(id,email,mot_de_passe,nom,date_inscription) VALUES (3,'user3@ensi.com', MD5('password3'), 'user3', NOW());)"
0
maroun_ba Messages postés 11 Date d'inscription samedi 9 février 2013 Statut Membre Dernière intervention 5 avril 2013 1
15 févr. 2013 à 15:00
l'erreur persiste :/ merci de m'aider
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
Chocobo_tofu1 Messages postés 162 Date d'inscription lundi 1 mars 2010 Statut Membre Dernière intervention 10 juillet 2014 362
15 févr. 2013 à 16:56
t'as quoi comme erreur dans ta console?
0
arth Messages postés 9374 Date d'inscription mardi 27 septembre 2005 Statut Contributeur Dernière intervention 16 décembre 2016 1 291
Modifié par arth le 15/02/2013 à 21:37
Bonjour,

D'où ça sort les ";" dans des parenthèses?

Au lieu de :

"INSERT INTO utilisateur(id,email,mot_de_passe,nom,date_inscription) VALUES (3,'user3@ensi.com', MD5('password3'), 'user3', NOW());)"

Mettre :

"INSERT INTO utilisateur(id,email,mot_de_passe,nom,date_inscription) VALUES (3,'user3@ensi.com', MD5('password3'), 'user3', NOW())"

EDIT : Oups c'est déjà ce qui est donné par Chocobo.
Je suis du même avis et ça ne devrait pas planter. SInon ce n'est pas la même erreur..


Le loup, solitaire et mystérieux.
0