Insertion en java / mysql
Résolu
aliouneman2
-
KX Messages postés 16761 Date d'inscription Statut Modérateur Dernière intervention -
KX Messages postés 16761 Date d'inscription Statut Modérateur Dernière intervention -
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????
Cette classe s'appelle Base
Voici la liste des erreurs qui ont été affiches :
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:
- Mysql insert java
- Waptrick java football - Télécharger - Jeux vidéo
- Jeux java itel - Télécharger - Jeux vidéo
- Touche insert - Guide
- Eclipse java - Télécharger - Langages
- Java apk - Télécharger - Langages
1 réponse
Bonjour,
Comme indiqué par l'erreur :
La ligne 95 correspond à cette requête :
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 :
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.
Par contre ton id avec la valeur c'est un peu bizarre...