0% found this document useful (0 votes)
28 views3 pages

1

This Java code defines a class called "user" that connects to a MySQL database and allows viewing and adding user records to a table. It imports necessary SQL and Swing classes, defines database connection and table loading methods, and includes an action listener for a button to insert new users into the database table. The main method sets up the Swing user interface and loads existing data.

Uploaded by

kpegohe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views3 pages

1

This Java code defines a class called "user" that connects to a MySQL database and allows viewing and adding user records to a table. It imports necessary SQL and Swing classes, defines database connection and table loading methods, and includes an action listener for a button to insert new users into the database table. The main method sets up the Swing user interface and loads existing data.

Uploaded by

kpegohe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

import java.sql.

Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

public class user extends javax.swing.JFrame {

Connection con;
PreparedStatement pst;
ResultSet rs;
DefaultTableModel d;

public user() {
initComponents();
Connect(); Appel à la méthode Connect() dans le constructeur
}

public void Connect() {


try {
Class.forName(com.mysql.cj.jdbc.Driver);
con = DriverManager.getConnection(jdbcmysqllocalhostschoolmanagement, root, );
System.out.println(Connexion réussie !);
} catch (ClassNotFoundException SQLException ex) {
Logger.getLogger(user.class.getName()).log(Level.SEVERE, null, ex);
System.out.println(Echec de la connexion !);
}
}

public void User_load() {


int c;
try {
pst = con.prepareStatement(selectfrom user);
rs = pst.executeQuery();
java.sql.ResultSetMetaData rsd = rs.getMetaData();
c = rsd.getColumnCount();
d = (DefaultTableModel) jTable1.getModel();
d.setRowCount(0);
while (rs.next()) {
Vector v2 = new Vector();
for (int i = 1; i = c; i++) {
v2.add(rs.getString(id));
v2.add(rs.getString(name));
v2.add(rs.getString(phone));
v2.add(rs.getString(address));
v2.add(rs.getString(uname));
v2.add(rs.getString(utype));
}
d.addRow(v2);
}
} catch (SQLException ex) {
Logger.getLogger(user.class.getName()).log(Level.SEVERE, null, ex);
}
}
private void initComponents() {
Votre code pour l'initialisation des composants Swing
}

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {


try {
String name = txtname.getText();
String phone = txtphone.getText();
String address = txtaddress.getText();
String uname = txtuname.getText();
String password = txtpass.getText();
String utype = txtutype.getSelectedItem().toString();
pst = con.prepareStatement(insert into user(name,phone,address,uname,password,utype)Values(,,,,,));
pst.setString(1, name);
pst.setString(2, phone);
pst.setString(3, address);
pst.setString(4, uname);
pst.setString(5, password);
pst.setString(6, utype);
pst.executeUpdate();
JOptionPane.showMessageDialog(this, User Added);
txtname.setText();
txtphone.setText();
txtaddress.setText();
txtuname.setText();
txtpass.setText();
txtutype.setSelectedIndex(-1);
txtname.requestFocus();
User_load(); Recharger les données après l'ajout
} catch (SQLException ex) {
Logger.getLogger(user.class.getName()).log(Level.SEVERE, null, ex);
}
}

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 httpdownload.oracle.comjavasetutorialuiswinglookandfeelplaf.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 InstantiationException IllegalAccessException
javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(user.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
editor-fold
java.awt.EventQueue.invokeLater(() - {
new user().setVisible(true);
});
}

Variables declaration - do not modify


private javax.swing.JButton jButton1;
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable jTable1;
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.JTextField txtname;
private javax.swing.JTextField txtphone;
private javax.swing.JTextField txtaddress;
private javax.swing.JTextField txtuname;
private javax.swing.JPasswordField txtpass;
private javax.swing.JComboBoxString txtutype;
End of variables declaration
}

You might also like