Gestion des etudiants - Java Swing avec NetBeans et MySQL
Structure de la base de donnees MySQL
CREATE DATABASE ecole;
USE ecole;
CREATE TABLE etudiants (
id INT PRIMARY KEY AUTO_INCREMENT,
nom VARCHAR(100),
age INT
);
INSERT INTO etudiants (nom, age) VALUES ('Ali', 22), ('Sami', 24), ('Lina', 21);
Connexion a la base de donnees
public Connection getConnection() throws SQLException {
return DriverManager.getConnection("jdbc:mysql://localhost:3306/ecole", "root", "");
}
Chargement de tous les etudiants
public void chargerEtudiants() {
try {
con = getConnection();
pst = con.prepareStatement("SELECT * FROM etudiants");
rs = pst.executeQuery();
model.setRowCount(0);
while (rs.next()) {
model.addRow(new Object[]{
rs.getInt("id"),
rs.getString("nom"),
rs.getInt("age")
});
}
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
Ajouter un etudiant
private void btnAjouterActionPerformed(java.awt.event.ActionEvent evt) {
try {
con = getConnection();
pst = con.prepareStatement("INSERT INTO etudiants(nom, age) VALUES(?, ?)");
pst.setString(1, txtNom.getText());
pst.setInt(2, Integer.parseInt(txtAge.getText()));
pst.executeUpdate();
JOptionPane.showMessageDialog(this, "Etudiant ajoute !");
chargerEtudiants();
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
Modifier un etudiant
private void btnModifierActionPerformed(java.awt.event.ActionEvent evt) {
int selectedRow = tableDonnees.getSelectedRow();
if (selectedRow >= 0) {
int id = Integer.parseInt(model.getValueAt(selectedRow, 0).toString());
try {
con = getConnection();
pst = con.prepareStatement("UPDATE etudiants SET nom = ?, age = ? WHERE id = ?");
pst.setString(1, txtNom.getText());
pst.setInt(2, Integer.parseInt(txtAge.getText()));
pst.setInt(3, id);
pst.executeUpdate();
JOptionPane.showMessageDialog(this, "Etudiant modifie !");
chargerEtudiants();
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Supprimer un etudiant
private void btnSupprimerActionPerformed(java.awt.event.ActionEvent evt) {
int selectedRow = tableDonnees.getSelectedRow();
if (selectedRow >= 0) {
int id = Integer.parseInt(model.getValueAt(selectedRow, 0).toString());
try {
con = getConnection();
pst = con.prepareStatement("DELETE FROM etudiants WHERE id = ?");
pst.setInt(1, id);
pst.executeUpdate();
JOptionPane.showMessageDialog(this, "Etudiant supprime !");
chargerEtudiants();
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Recherche dun etudiant
private void btnRechercherActionPerformed(java.awt.event.ActionEvent evt) {
String mot = txtRecherche.getText();
try {
con = getConnection();
pst = con.prepareStatement("SELECT * FROM etudiants WHERE nom LIKE ?");
pst.setString(1, "%" + mot + "%");
rs = pst.executeQuery();
model.setRowCount(0);
while (rs.next()) {
model.addRow(new Object[]{
rs.getInt("id"),
rs.getString("nom"),
rs.getInt("age")
});
}
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
Remplir les champs en cliquant sur une ligne
private void tableDonneesMouseClicked(java.awt.event.MouseEvent evt) {
int i = tableDonnees.getSelectedRow();
txtNom.setText(model.getValueAt(i, 1).toString());
txtAge.setText(model.getValueAt(i, 2).toString());
}