Insertion des data dans une table mysql via un jbutton

Fermé
barakouda - 20 juin 2013 à 12:06
bouchrot Messages postés 351 Date d'inscription mercredi 12 octobre 2011 Statut Membre Dernière intervention 2 janvier 2023 - 21 juin 2013 à 17:58
Salut,

je ne parviens pas à inseré les données à partir d'un formulaire avec les champs (matricule, nom, postnom) de type (int, string et string) dans table de la base de données mysql à l'aide du bouton jbutton (insertion) de mon formulaire. Le probleme est qu'il ya qu'une seule insertion quand l'on click sur le bouton insertion et ensuite un message d'erreur de JOptionpaneshowMessageDialog s'affiche.

Pourriez vous m'aidez?

Voici mon code :

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package gestionstudent;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.JOptionPane;

/**
*
* @author Richard
*/
public class StudentFormulaire extends javax.swing.JFrame {
Connection con = null;
Statement st = null;
ResultSet rs = null;
String DB = "jdbc:mysql://localhost/gestionetudiant";
int a;
String b;
String c;
/**
* Creates new form StudentFormulaire
*/

public StudentFormulaire() {
initComponents();
}

/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
jLabel6 = new javax.swing.JLabel();
jLabel7 = new javax.swing.JLabel();
jLabel8 = new javax.swing.JLabel();
Matricule = new javax.swing.JTextField();
Nom = new javax.swing.JTextField();
Postnom = new javax.swing.JTextField();
Inserer = new javax.swing.JButton();
Quitter = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jLabel1.setText("FORMULAIRE DE GESTION DE L'ETUDIANT");

jLabel2.setText("---------------------------------------------------------------");

jLabel3.setText("Matricule");

jLabel4.setText("Nom");

jLabel5.setText("Postnom");

jLabel6.setText(":");

jLabel7.setText(":");

jLabel8.setText(":");

Inserer.setText("INSERER");
Inserer.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
InsererActionPerformed(evt);
}
});

Quitter.setText("QUITTER");
Quitter.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
QuitterActionPerformed(evt);
}
});

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel3)
.addComponent(jLabel4)
.addComponent(jLabel5))
.addGap(27, 27, 27)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel8)
.addComponent(jLabel7)
.addComponent(jLabel6))
.addGap(63, 63, 63)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel2)
.addComponent(jLabel1)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(Postnom, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 224, Short.MAX_VALUE)
.addComponent(Nom, javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(Matricule, javax.swing.GroupLayout.Alignment.LEADING)))
.addContainerGap(97, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addGap(67, 67, 67)
.addComponent(Inserer)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(Quitter)
.addGap(147, 147, 147))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jLabel2)
.addGap(38, 38, 38)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3)
.addComponent(jLabel6)
.addComponent(Matricule, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(29, 29, 29)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel4)
.addComponent(jLabel7)
.addComponent(Nom, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(27, 27, 27)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel5)
.addComponent(jLabel8)
.addComponent(Postnom, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 67, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(Inserer)
.addComponent(Quitter))
.addGap(52, 52, 52))
);

pack();
}// </editor-fold>

private void QuitterActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
try{
st.close();
con.close();
System.exit(0);
}
catch (Exception e)
{JOptionPane.showMessageDialog(null, "Unable to close connection");}
}

private void InsererActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection(DB,"root","barakouda");
st= con.createStatement();
int nb = st.executeUpdate(
"INSERT INTO etudiant"
+"(matricule,nom,postnom)"
+"VALUES ('"+a+"','"+b+"','"+c+"')");

a = Integer.parseInt(Matricule.getText());
b = Nom.getText();
c = Postnom.getText();
}
catch (Exception e)
{JOptionPane.showMessageDialog(null, "Error in connection");}
}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(StudentFormulaire.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(StudentFormulaire.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(StudentFormulaire.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(StudentFormulaire.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>

/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new StudentFormulaire().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton Inserer;
private javax.swing.JTextField Matricule;
private javax.swing.JTextField Nom;
private javax.swing.JTextField Postnom;
private javax.swing.JButton Quitter;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JLabel jLabel7;
private javax.swing.JLabel jLabel8;
// End of variables declaration
}
A voir également:

7 réponses

bouchrot Messages postés 351 Date d'inscription mercredi 12 octobre 2011 Statut Membre Dernière intervention 2 janvier 2023 6
21 juin 2013 à 17:50
je ne vois pas d"erreur dans ton code ,
1
bouchrot Messages postés 351 Date d'inscription mercredi 12 octobre 2011 Statut Membre Dernière intervention 2 janvier 2023 6
20 juin 2013 à 12:21
salut
donc ce code est difficile a lire car c'est long ,mais c pas grave ,alors la 1er chose
est ce que la matricule dans ta base de donnée est autoincriment ou non
ensuite met ça
a = Integer.parseInt(Matricule.getText());
b = Nom.getText();
c = Postnom.getText();

aprés le try
comme ça

try {
a = Integer.parseInt(Matricule.getText());
b = Nom.getText();
c = Postnom.getText();
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection(DB,"root","barakouda");
st= con.createStatement();
int nb = st.executeUpdate(
"INSERT INTO etudiant"
+"(matricule,nom,postnom)"
+"VALUES ('"+a+"','"+b+"','"+c+"')");
}
catch (Exception e)
{JOptionPane.showMessageDialog(null, "Error in connection");}
}
0
Thanks.

Mai mon probleme est de faire une insertion repetitive donc apres la saisie du premier enregistrement, j'insere et ensuite que je puisse saisir le second, le troisieme puis le nieme enregistrament dans la table.
0
bouchrot Messages postés 351 Date d'inscription mercredi 12 octobre 2011 Statut Membre Dernière intervention 2 janvier 2023 6
20 juin 2013 à 13:42
oui je sais mais répond au moins a la question que je t'ai posé , est ce que ton matricule est auto-incriment dans la base ou non
0

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

Posez votre question
non le matricule est à saisir par l'utilisateur.
0
bouchrot Messages postés 351 Date d'inscription mercredi 12 octobre 2011 Statut Membre Dernière intervention 2 janvier 2023 6
21 juin 2013 à 17:51
je te conseille d'essayer l'ajout sur une autre table pour voir , s'il ya le même problème
0
bouchrot Messages postés 351 Date d'inscription mercredi 12 octobre 2011 Statut Membre Dernière intervention 2 janvier 2023 6
21 juin 2013 à 17:58
une autre est ce que tu peux m'envoyer la base de donnée et le projet ,
je veux voir par moi même
0