Interface graphique en java pour remplir une table de base de données

ayadijj Messages postés 9 Date d'inscription jeudi 22 mars 2012 Statut Membre Dernière intervention 13 mai 2012 - 26 avril 2012 à 13:47
 Utilisateur anonyme - 26 avril 2012 à 21:53
Bonsoir,je veut ajouter des utilisateurs dans une base de données,je travailles sous Netbeans,voici mon code,pas d'erreurs,sauf que je le compile,il m'affiche:erreur sql.
svp,qui peut m'aider et merci d'avance.

package jouda;
import java.sql.*;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

import java.util.HashSet;
import java.util.Set;

/**
*
* @author Administrateur
*/
public class GestionUtilisateur extends javax.swing.JFrame {

/**
* Creates new form GestionUtilisateur
*/
public GestionUtilisateur() {
this.setTitle("Gestion des utilisateurs");

initComponents();
String req="Select * from utilisateur";
remplir(req);
}

/**
* 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() {

jPanel1 = new javax.swing.JPanel();
jScrollPane2 = new javax.swing.JScrollPane();
tab = new javax.swing.JTable();
jPanel2 = new javax.swing.JPanel();
b1 = new javax.swing.JButton();
b2 = new javax.swing.JButton();
b3 = new javax.swing.JButton();
b4 = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowActivated(java.awt.event.WindowEvent evt) {
formWindowActivated(evt);
}
});

javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 100, Short.MAX_VALUE)
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 100, Short.MAX_VALUE)
);

tab.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null, null, null, null, null},
{null, null, null, null, null, null},
{null, null, null, null, null, null},
{null, null, null, null, null, null}
},
new String [] {
"CIN", "Nom", "Prénom", "Num_téléphone", "Adresse", "Pw_admin"
}
) {
Class[] types = new Class [] {
java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
};

public Class getColumnClass(int columnIndex) {
return types [columnIndex];
}
});
jScrollPane2.setViewportView(tab);

b1.setText("Ajouter ");
b1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
b1ActionPerformed(evt);
}
});

b2.setText("Modifier");
b2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
b2ActionPerformed(evt);
}
});

b3.setText("Supprimer");
b3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
b3ActionPerformed(evt);
}
});

b4.setText("Retour");
b4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
b4ActionPerformed(evt);
}
});

javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addGap(68, 68, 68)
.addComponent(b1, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(b2, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(26, 26, 26)
.addComponent(b3, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(27, 27, 27)
.addComponent(b4)
.addGap(0, 156, Short.MAX_VALUE))
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
.addContainerGap(43, Short.MAX_VALUE)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(b1)
.addComponent(b2)
.addComponent(b3)
.addComponent(b4))
.addContainerGap())
);

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addGap(76, 76, 76)
.addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 461, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
.addGroup(layout.createSequentialGroup()
.addGap(0, 0, Short.MAX_VALUE)
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(22, 22, 22)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(57, 57, 57)
.addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 42, Short.MAX_VALUE)
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);

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


private void b4ActionPerformed(java.awt.event.ActionEvent evt) {
new Administrateur().setVisible(true); // TODO add your handling code here:
}

private void b1ActionPerformed(java.awt.event.ActionEvent evt) {
this.setVisible(false); // TODO add your handling code here:
new AjoutUtilisateur().setVisible(true);// TODO add your handling code here:
}

private void b2ActionPerformed(java.awt.event.ActionEvent evt) {
new ModifierUtilisateur().setVisible(true); // TODO add your handling code here:
}

private void b3ActionPerformed(java.awt.event.ActionEvent evt) {
int j=tab.getSelectedRow();
if(j==-1){
JOptionPane.showMessageDialog(this, "Aucun elément n'a eté selectionné");
}
else{
int a=JOptionPane.showConfirmDialog(this, "voulez vous supprimer cet utilisateur");
if(a==0){
supprimer(tab.getValueAt(tab.getSelectedRow(), 0).toString());
String t="select * from utilisateur";
remplir(t);// TODO add your handling code here:
}
}
}
public void remplir( String req){
int d=tab.getRowCount();
for(int compt=0;compt<d;compt++){
((DefaultTableModel)tab.getModel()).removeRow(0);
}
try {
Class.forName("com.mysql.jdbc.Driver");
try{
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/gestion_vsat","root","041119864");
Statement st=con.createStatement();
ResultSet res=st.executeQuery(req);
int i=0;
while(res.next()){
((DefaultTableModel)tab.getModel()).addRow(new Object[]{null,null,null});
tab.setValueAt(res.getString("CIN"),i,0);
tab.setValueAt(res.getString("nom"),i,1);
tab.setValueAt(res.getString("prénom"),i,2);
tab.setValueAt(res.getString("num_téléphone"),i,3);
tab.setValueAt(res.getString("adresse"),i,4);
tab.setValueAt(res.getString("pw_admin"),i,5);

i++;

}

} catch(SQLException ex){
System.out.println("Erreur requete sql");

}

}
catch(ClassNotFoundException ex){
System.out.println("Erreur de chargement com.mysql.jdbc"+ex.toString());

}

// TODO add your handling code here:
}

private void formWindowActivated(java.awt.event.WindowEvent evt) {
String req="Select * from utilisateur";
remplir(req);// TODO add your handling code here:
}

public static void supprimer(String CIN){
try {
Class.forName("com.mysql.jdbc.Driver");
try{
String req="delete from utilisateur where CIN='"+CIN+"'";
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/gestion_vsat","root","041119864");
Statement st=con.createStatement();
st.executeUpdate(req);




} catch(SQLException e){
System.out.println("Erreur requete sql");

}

}
catch(ClassNotFoundException ex){
System.out.println("Erreur de chargement com.mysql.jdvc"+ex.toString());

}
}



/**
* @param args the command line arguments
*/
public static void main(String args[]) {
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(accueil.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(accueil.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(accueil.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(accueil.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
/*
* 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(GestionUtilisateur.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(GestionUtilisateur.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(GestionUtilisateur.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(GestionUtilisateur.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 GestionUtilisateur().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton b1;
private javax.swing.JButton b2;
private javax.swing.JButton b3;
private javax.swing.JButton b4;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JTable tab;
// End of variables declaration
}

3 réponses

Utilisateur anonyme
26 avril 2012 à 21:53
Et ca ne sert à rien de poster un énorme code ici, je ne lis jamais ce qui dépasse les 20 lignes

D'autant plus que ça pue le pompage de code à plein nez. Et y'a aucun effort pour expliquer le code, ou même pour bien le présenter visuellement.

On ressort les questions classiques à se poser :
--> bons identifiants de connexion à la base de données ?
--> requêtes SQL bien construites ?


--
Pylouq
(Lire le Réglement n'a jamais tué personne, au pire ça a instruit des gens.)
2
Rejoignez-nous