BTN Add Action Performed
BTN Add Action Performed
ActionEvent evt) {
// TODO add your handling code here:
Connection conn = null;
PreparedStatement statement = null;
String maSv = txtMasv.getText();
String hoTen = txtHoten.getText();
String email = txtEmail.getText();
String diaChi = txtDiachi.getText();
String lop = txtLop.getText();
try {
// Mo ket noi den CSDL
conn = (Connection)
DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "");
//Them du lieu vao CSDL
String sql = "INSERT INTO ctt24_student(masv, hoten,email, diachi, lopsv)" +
"VALUES(?,?,?,?,?)";
statement = (PreparedStatement) conn.prepareStatement(sql);
statement.setString(1, maSv);
statement.setString(2, hoTen);
statement.setString(3, email);
statement.setString(4, diaChi);
statement.setString(5, lop);
statement.execute();
} catch (SQLException ex) {
Logger.getLogger(StudentFrame.class.getName()).log(Level.SEVERE, null, ex);
} finally {
//Dong ket noi
if (conn != null){
try {
conn.close();
} catch (SQLException ex) {
Logger.getLogger(StudentFrame.class.getName()).log(Level.SEVERE, null, ex);
}
}
if (statement != null){
try {
statement.close();
} catch (SQLException ex) {
Logger.getLogger(StudentFrame.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}