0% found this document useful (0 votes)
17 views8 pages

Codes

The document contains Java code for connecting to a MySQL database and performing CRUD (create, read, update, delete) operations on an "employees" table. It includes code to get a database connection, login a user, save a new employee record, clear form fields, update an existing record, delete a record, and retrieve data on row selection from the table. The code handles form validation, error handling, and updating the UI.

Uploaded by

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

Codes

The document contains Java code for connecting to a MySQL database and performing CRUD (create, read, update, delete) operations on an "employees" table. It includes code to get a database connection, login a user, save a new employee record, clear form fields, update an existing record, delete a record, and retrieve data on row selection from the table. The code handles form validation, error handling, and updating the UI.

Uploaded by

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

 Connection codes

Connection conn = null;

private Connection getConnection(){

String url = "jdbc:mysql://localhost:3306/ employee_info";

String user = "root";

String pass ="Cyprian_2020";

try {

conn = DriverManager.getConnection(url, user, pass);

catch (SQLException e) {

// TODO Auto-generated catch block

e.getLocalizedMessage();

return conn;

 LoginBtn codes
private void LoginBtnActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:

if(userNameTxt.getText().isEmpty()){
JOptionPane.showMessageDialog(this, "Enter userName", "Error",
JOptionPane.ERROR_MESSAGE);
return;
}
if(passwordTxt.getText().isEmpty()){
JOptionPane.showMessageDialog(this, "Enter password", "Error",
JOptionPane.ERROR_MESSAGE);

return;
}

try {
PreparedStatement stmt = getConnection().prepareStatement("SELECT
* FROM USERS WHERE userName=? AND password=?");
stmt.setString(1, userNameTxt.getText());
stmt.setString(2, passwordTxt.getText());
ResultSet rs = stmt.executeQuery();

while(rs.next()) {

if((rs.getString(1).equals(userNameTxt.getText()))
&&(rs.getString(2).equals(passwordTxt.getText()))) {
MainUi EmFrame = new MainUi();
EmFrame.show();
dispose();
}
else {
JOptionPane.showMessageDialog(this, "Inncorrect userName or Password",
"Login Error", JOptionPane.ERROR_MESSAGE);
}
}

}
catch(SQLException e) {
e.getLocalizedMessage();
}
}
 Save Code
private void saveBtnActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
if(empIDtxt.getText().isEmpty()){
JOptionPane.showMessageDialog(this, "Enter EmpID", "Error",
JOptionPane.ERROR_MESSAGE);
return;
}
if(surNametxt.getText().isEmpty()){
JOptionPane.showMessageDialog(this, "Enter SurName", "Error",
JOptionPane.ERROR_MESSAGE);
return;
}
if(givenNametxt.getText().isEmpty()){
JOptionPane.showMessageDialog(this, "Enter GivenName", "Error",
JOptionPane.ERROR_MESSAGE);
return;
}
if(dobtxt.getText().isEmpty()){
JOptionPane.showMessageDialog(this, "Enter Date of birth", "Error",
JOptionPane.ERROR_MESSAGE);
return;
}
if(gendertxt.getSelectedItem() == null){
JOptionPane.showMessageDialog(this, "Select Gender", "Error",
JOptionPane.ERROR_MESSAGE);
return;
}
if(departtxt.getSelectedItem() == null){
JOptionPane.showMessageDialog(this, "Select Department", "Error",
JOptionPane.ERROR_MESSAGE);
return;

if(jobtxt.getText().isEmpty()){
JOptionPane.showMessageDialog(this, "Enter Job", "Error",
JOptionPane.ERROR_MESSAGE);
return;
}

if(salarytxt.getText().isEmpty()){
JOptionPane.showMessageDialog(this, "Enter Salary", "Error",
JOptionPane.ERROR_MESSAGE);
return;
}

try{
PreparedStatement stmt = getConnection().prepareStatement("INSERT INTO employees
(EmpID,surName,givenName,otherName,DOB,gender,department,jobTitle,salary)
VALUES(?,?,?,?,?,?,?,?,?)");
stmt.setString(1, empIDtxt.getText());
stmt.setString(2, surNametxt.getText());
stmt.setString(3, givenNametxt.getText());
stmt.setString(4,otherNametxt.getText());
stmt.setString(5, dobtxt.getText());
stmt.setString(6, gendertxt.getSelectedItem().toString());
stmt.setString(7, departtxt.getSelectedItem().toString());
stmt.setString(8, jobtxt.getText());
stmt.setDouble(9, Double.parseDouble(salarytxt.getText()));
stmt.execute();
JOptionPane.showMessageDialog(this, "Record Save");

loadTable();

catch(SQLException e){
e.getLocalizedMessage();

}
 ClearBtn code
private void clearBtnActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
empIDtxt.setText("");
surNametxt.setText("");
givenNametxt.setText("");
otherNametxt.setText("");
dobtxt.setText("");
gendertxt.setSelectedIndex(0);
departtxt.setSelectedItem(null);
jobtxt.setText("");
salarytxt.setText("");

}
 UpdateCode
private void updateBtnActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:

if(empIDtxt.getText().isEmpty()){
JOptionPane.showMessageDialog(this, "Enter EmpID", "Error",
JOptionPane.ERROR_MESSAGE);
return;
}
if(surNametxt.getText().isEmpty()){
JOptionPane.showMessageDialog(this, "Enter SurName", "Error",
JOptionPane.ERROR_MESSAGE);
return;
}
if(givenNametxt.getText().isEmpty()){
JOptionPane.showMessageDialog(this, "Enter GivenName", "Error",
JOptionPane.ERROR_MESSAGE);
return;
}
if(dobtxt.getText().isEmpty()){
JOptionPane.showMessageDialog(this, "Enter Date of birth", "Error",
JOptionPane.ERROR_MESSAGE);
return;
}
if(gendertxt.getSelectedItem() == null){
JOptionPane.showMessageDialog(this, "Select Gender", "Error",
JOptionPane.ERROR_MESSAGE);
return;
}
if(departtxt.getSelectedItem() == null){
JOptionPane.showMessageDialog(this, "Select Department", "Error",
JOptionPane.ERROR_MESSAGE);
return;

if(jobtxt.getText().isEmpty()){
JOptionPane.showMessageDialog(this, "Enter Job", "Error",
JOptionPane.ERROR_MESSAGE);
return;
}

if(salarytxt.getText().isEmpty()){
JOptionPane.showMessageDialog(this, "Enter Salary", "Error",
JOptionPane.ERROR_MESSAGE);
return;
}
try{
PreparedStatement stmt = getConnection().prepareStatement("UPDATE employees SET
surName=?,givenName=?,otherName=?,DOB=?,Gender=?,department=?,jobTitle=?,Salary=?
WHERE EmpID =?");

stmt.setString(1,surNametxt.getText());
stmt.setString(2,givenNametxt.getText());
stmt.setString(3, otherNametxt.getText());
stmt.setString(4, dobtxt.getText());
stmt.setString(5,
gendertxt.getSelectedItem().toString());
stmt.setString(6,
departtxt.getSelectedItem().toString());
stmt.setString(7, jobtxt.getText());
stmt.setDouble(8,
Double.parseDouble(salarytxt.getText()));
stmt.setString(9, empIDtxt.getText());

stmt.executeUpdate();
JOptionPane.showMessageDialog(this, "Record
Updated");
loadTable();

}
catch(SQLException e){
e.getLocalizedMessage();
}

 DeleteCodes
private void deleteBtnActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
if(empIDtxt.getText().isEmpty()){
JOptionPane.showMessageDialog(this, "Select emplyee to delete", "Error",
JOptionPane.ERROR_MESSAGE);
return;
}

try {
PreparedStatement stmt =
getConnection().prepareStatement("DELETE FROM employees WHERE EmpID =?");
stmt.setString(1, empIDtxt.getText());
stmt.execute();
JOptionPane.showMessageDialog(this, "Record Deleted");
loadTable();

}
catch(SQLException e) {
e.getLocalizedMessage();
}
}

 Table selection

private void EmpTableMouseClicked(java.awt.event.MouseEvent evt) {


// TODO add your handling code here:
DefaultTableModel tbModel = (DefaultTableModel) EmpTable.getModel();
int selectedIndex = EmpTable.getSelectedRow();

String ID = tbModel.getValueAt(selectedIndex,0).toString();
String sName = tbModel.getValueAt(selectedIndex, 1).toString();
String gName = tbModel.getValueAt(selectedIndex, 2).toString();
String oName = tbModel.getValueAt(selectedIndex, 3).toString();
String dob = tbModel.getValueAt(selectedIndex, 4).toString();
String gender = tbModel.getValueAt(selectedIndex, 5).toString();
String depart = tbModel.getValueAt(selectedIndex, 6).toString();
String job = tbModel.getValueAt(selectedIndex, 7).toString();
String salary = tbModel.getValueAt(selectedIndex, 8).toString();
//Converting salary to string

empIDtxt.setText(ID);
empIDtxt.setEditable(false);
surNametxt.setText(sName);
givenNametxt.setText(gName);
otherNametxt.setText(oName);
dobtxt.setText(dob);
gendertxt.setSelectedItem(gender);
departtxt.setSelectedItem(depart);
jobtxt.setText(job);
salarytxt.setText(salary);

You might also like