Insertion en java / mysql

Résolu/Fermé
aliouneman2 - 25 déc. 2014 à 12:48
KX Messages postés 16734 Date d'inscription samedi 31 mai 2008 Statut Modérateur Dernière intervention 24 avril 2024 - 25 déc. 2014 à 23:49
Bonjour j'ai un problème d'insertion en base à chaque fois que j'insére il m'affiche un message d'erreur de requettes SQL

Help me????

import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;


public class InterfaceConnexion extends JFrame implements ActionListener{
	
	JLabel nom;
	JTextField nom1;
	JLabel prenom;
	JTextField prenom1;
	JLabel lieuNaiss;
	JTextField lieuNaiss1;
	JButton valider;
	
	public InterfaceConnexion(){
		
		this.setTitle(" Insertion dans une Base de données ");
		this.setSize( new Dimension(350,275));
		
		this.setLocationRelativeTo(null);
		Container contenu = this.getContentPane();
		contenu.setLayout(null);
		
		 nom = new JLabel(" Nom ");
		 nom1 = new JTextField(20);
		contenu.add(nom);
		nom.setBounds(10, 10, 110, 25);
		contenu.add(nom1);
		nom1.setBounds(135, 10, 110, 25);
		
		
		prenom = new JLabel(" Prénom ");
		contenu.add(prenom);
		prenom.setBounds(10, 55, 110, 25);
		
		 prenom1 = new JTextField(20);
		contenu.add(prenom1);
		prenom1.setBounds(135, 55, 110, 25);
		
		 lieuNaiss = new JLabel(" Lieu de Naissance ");
		contenu.add(lieuNaiss);
		lieuNaiss.setBounds(10, 110, 110, 25);
		
		 lieuNaiss1 = new JTextField(20);
		contenu.add(lieuNaiss1);
		lieuNaiss1.setBounds(135, 110, 110, 25);
		
	    valider = new JButton("Valider");
		contenu.add(valider);
		valider.setBounds(175, 155,110 , 25);
		
		valider.addActionListener(this);
		
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setVisible(true);
		
		
	}

	

	@SuppressWarnings("null")
	public void actionPerformed(ActionEvent e) {
		
		Base b = new Base();
		Connection con = null;
		Statement statement;
		ResultSet resultat;
		
		if (e.getSource() == valider) {
			
			String nom2 = nom1.getText();
			String prenom2 = prenom1.getText();
			String lieuNaiss2 = lieuNaiss1.getText();
			
			b.connexionDB();
			
			con = b.getConnect();
			
			try {
				
				statement = con.createStatement();
				String sql = "INSERT INTO personne (id,nom,prenom,lieuNaiss) VALUES('',"+nom2+"','"+prenom2+"','"+lieuNaiss2+"')'";
				statement.executeUpdate(sql);
				
			} catch (SQLException e1) {

				e1.printStackTrace();
			}
			
			
			
		}
		
	}

}







Cette classe s'appelle Base


import java.sql.Connection;
import java.sql.DriverManager;


public class Base {
	
	public Connection con;
	
	public void connexionDB(){
		
		try {
				
			   Class.forName("com.mysql.jdbc.Driver");
				
			} catch (ClassNotFoundException e) {

				e.printStackTrace();
			}	
			
		try {
			
			String url ="jdbc:mysql://localhost/test";
			String user = "root";
			String pass = "";
			con = DriverManager.getConnection(url, user, pass);
			
		} catch (Exception e) {
			
			e.printStackTrace();
			throw new IllegalStateException(" Connexion is available ",e);
			
		}
		
	}
	
	public Connection getConnect(){
		
		return con;
	}

	
}



Voici la liste des erreurs qui ont été affiches :


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 '','aa','aaa')'' 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 InterfaceConnexion.actionPerformed(InterfaceConnexion.java:95)
	at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
	at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
	at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
	at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
	at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
	at java.awt.Component.processMouseEvent(Unknown Source)
	at javax.swing.JComponent.processMouseEvent(Unknown Source)
	at java.awt.Component.processEvent(Unknown Source)
	at java.awt.Container.processEvent(Unknown Source)
	at java.awt.Component.dispatchEventImpl(Unknown Source)
	at java.awt.Container.dispatchEventImpl(Unknown Source)
	at java.awt.Component.dispatchEvent(Unknown Source)
	at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
	at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
	at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
	at java.awt.Container.dispatchEventImpl(Unknown Source)
	at java.awt.Window.dispatchEventImpl(Unknown Source)
	at java.awt.Component.dispatchEvent(Unknown Source)
	at java.awt.EventQueue.dispatchEvent(Unknown Source)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.run(Unknown Source)

A voir également:

1 réponse

KX Messages postés 16734 Date d'inscription samedi 31 mai 2008 Statut Modérateur Dernière intervention 24 avril 2024 3 015
25 déc. 2014 à 16:53
Bonjour,

Comme indiqué par l'erreur :

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 '','aa','aaa')'' at line 1
at InterfaceConnexion.actionPerformed(InterfaceConnexion.java:95)

La ligne 95 correspond à cette requête :
"INSERT INTO personne (id,nom,prenom,lieuNaiss) VALUES('',"+nom2+"','"+prenom2+"','"+lieuNaiss2+"')'"

Il te manque un guillemet avant nom2...

Remarque : il y a des risques de sécurité à utiliser la concaténation de String dans une requête SQL, on appelle ça de l'injection SQL. Imagines par exemple que je donnes la valeur suivante à lieuNaiss2 :
'); DROP TABLE personne; --
tu vas alors avoir deux requêtes qui s'exécutent dont l'une va te supprimer toute ta table... il faut utiliser un PreparedStatement pour passer des arguments variables à une requête.
0
d'aprés ce que j'ai compris je peux faire ceci ...




String sql = "INSERT INTO personne (id,nom,prenom,lieuNaiss) VALUES(''','"+nom2+"','"+prenom2+"','"+lieuNaiss2+"')'";
PreparedStatement statement = con.prepareStatement(sql);

statement.setString(1,nom2);
statement.setString(2,prenom2);
statement.setString(3,lieuNaiss2);

statement.execute();

0
KX Messages postés 16734 Date d'inscription samedi 31 mai 2008 Statut Modérateur Dernière intervention 24 avril 2024 3 015
25 déc. 2014 à 18:49
C'est un peu ça, à part qu'il faut remplacer les concaténation de String, et les remplacer par des "?"

String sql = "INSERT INTO personne (id,nom,prenom,lieuNaiss) VALUES('',?,?,?)";

Par contre ton id avec la valeur
''
c'est un peu bizarre...
0
aliouneman2 > KX Messages postés 16734 Date d'inscription samedi 31 mai 2008 Statut Modérateur Dernière intervention 24 avril 2024
25 déc. 2014 à 23:31
le id est de auto incrément c'est la raison pour laquelle j'ai mis ' '
0
KX Messages postés 16734 Date d'inscription samedi 31 mai 2008 Statut Modérateur Dernière intervention 24 avril 2024 3 015 > aliouneman2
25 déc. 2014 à 23:49
Dans ce cas tu ne mets rien du tout...

INSERT INTO personne (nom,prenom,lieuNaiss) VALUES (?,?,?)
0