This OOP JDBC Employee Crud Project will teach you how to do basic database functions that are CREATE RETIEVE, UPDATE and DELETE and SEARCH using mysql Database. The INSERT, SELECT, UPDATE and DELETE statements can be used in any database system, because this is support by all relational database systems.will discuss about how to work with oop using crud operations are (create,read,update,delete).java oops concepts must when you moving to the java frameworks.
We will learn how to INSERT, SELECT, UPDATE and DELETE and SEARCH in database by writing code to manage the records table in the database named payrollsystem. students table consist of following columns empname,salary,phone.
First you have to create package com.example.oop.model.
Employee.java
public class Employee { private int id; private String empname; private int salary; private int phone; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getEmpname() { return empname; } public void setEmpname(String empname) { this.empname = empname; } public int getSalary() { return salary; } public void setSalary(int salary) { this.salary = salary; } public int getPhone() { return phone; } public void setPhone(int phone) { this.phone = phone; } }
we created the database payrollsystem.inside the database created the table which name is employees in the mysql database.
After that created the package com.example.oop.EmployeeDb.
import java.sql.Connection; import java.sql.DriverManager; public class EmployeeDb { static Connection con; static String driver = "com.mysql.jdbc.Driver"; static String url = "jdbc:mysql://localhost/payollsystem"; static String uname = "root"; static String pass = ""; public static Connection getConnection() throws Exception{ if(con == null){ Class.forName(driver); con = DriverManager.getConnection(url,uname, pass); } return con; }
After that created the package com.example.oop.employeecontroller. create the interface EmployeeDAO.
public interface EmployeeDAO { public void save(Employee employees); public void update(Employee employees); public void delete(Employee employees); public Employee get(int id); public List<Employee> list(); }
EmployeeDAOIm.java
after that create the class EmployeeDAOIm and implement the particular interface EmployeeDAO. inside the class
we create the crud operation methods.
public class EmployeeDAOIm { Connection con; PreparedStatement ps; String sql; public void save(Employee employees) { try { con = EmployeeDb.getConnection(); sql = "INSERT INTO employee(empname,salary,phone) VALUES (?,?,?)"; ps = con.prepareStatement(sql); ps.setString(1, employees.getEmpname()); ps.setInt(2, employees.getSalary()); ps.setInt(3, employees.getPhone()); ps.executeUpdate(); JOptionPane.showMessageDialog(null, "Saved!"); } catch (Exception e) { e.printStackTrace(); JOptionPane.showMessageDialog(null, "Error"); } } public void update(Employee employees) { try { Connection con = EmployeeDb.getConnection(); String sql = "UPDATE employee SET empname=?,salary=?,phone=? WHERE id=?"; PreparedStatement ps = con.prepareStatement(sql); ps.setString(1, employees.getEmpname()); ps.setInt(2, employees.getSalary()); ps.setInt(3, employees.getPhone()); ps.setInt(4, employees.getId()); ps.executeUpdate(); JOptionPane.showMessageDialog(null, "Updated!"); } catch (Exception e) { e.printStackTrace(); JOptionPane.showMessageDialog(null, "Error"); } } public void delete(Employee employees) { try { Connection con = EmployeeDb.getConnection(); String sql = "delete from employee WHERE id=?"; PreparedStatement ps = con.prepareStatement(sql); ps.setInt(1, employees.getId()); ps.executeUpdate(); JOptionPane.showMessageDialog(null, "Deleteddd!"); } catch (Exception e) { e.printStackTrace(); JOptionPane.showMessageDialog(null, "Error"); } } public Employee get(int id) { Employee st = new Employee(); try { Connection con = EmployeeDb.getConnection(); String sql = "SELECT * FROM employee WHERE id=?"; PreparedStatement ps = con.prepareStatement(sql); ps.setInt(1, id); ResultSet rs = ps.executeQuery(); if(rs.next()){ st.setId(rs.getInt("id")); st.setEmpname(rs.getString("empname")); st.setSalary(rs.getInt("salary")); st.setPhone(rs.getInt("phone")); } } catch (Exception e) { e.printStackTrace(); JOptionPane.showMessageDialog(null, "Error"); } return st; } public List<Employee> list() { List<Employee> list = new ArrayList<Employee>(); try { Connection con = EmployeeDb.getConnection(); String sql = "SELECT * FROM employee "; PreparedStatement ps = con.prepareStatement(sql); ResultSet rs = ps.executeQuery(); while(rs.next()) { Employee st = new Employee(); st.setId(rs.getInt("id")); st.setEmpname(rs.getString("empname")); st.setSalary(rs.getInt("salary")); st.setPhone(rs.getInt("phone")); list.add(st); } } catch (Exception e) { e.printStackTrace(); JOptionPane.showMessageDialog(null, "Error"); } return list; } }
we designed the swing GUI design.After that created the package com.example.oop.employeeview.
save button which we use to save data in to the mysql database.
double click the save button paste the code code inside.
Employee e = new Employee(); String empname = txtEname.getText(); int sal = Integer.parseInt(txtSal.getText()); int phone = Integer.parseInt(txtPhone.getText()); e.setEmpname(empname); e.setSalary(sal); e.setPhone(phone); EmployeeDAOIm dao = new EmployeeDAOIm(); dao.save(e); Load(); txtEname.setText(""); txtSal.setText(""); txtPhone.setText(""); txtEname.requestFocus();
Get the data from the mysql database to pass into JTable.so create the method which load() inside the method we paste the following code.
public void Load() { EmployeeDAOIm dao = new EmployeeDAOIm(); List<Employee> list = dao.list(); DefaultTableModel DFT = (DefaultTableModel) jTable1.getModel(); DFT.setRowCount(0); for(Employee st: list) { int sid = st.getId(); String empname = st.getEmpname(); int salary = st.getSalary(); int phone = st.getPhone(); DFT.addRow(new Object[]{sid,empname,salary,phone}); } }
after done it. you have to call the load method inside the constructor of class.so when application runs all data will be loaded.i shown below how to call inside the constructor.
public EmployeeForm() { initComponents(); Load(); }
Search button which we use to seach data from the mysql database.double click the seach button paste the code code inside.
eID = Integer.parseInt(txtID.getText()); EmployeeDAOIm dao = new EmployeeDAOIm(); Employee st = dao.get(eID); txtEname.setText(st.getEmpname()); txtSal.setText(String.valueOf(st.getSalary())); txtPhone.setText(String.valueOf(st.getPhone()));
After search the record you have to make a changes what you need and click edit button.
Employee st = new Employee(); String empname = txtEname.getText(); int sal = Integer.parseInt(txtSal .getText()); int phone = Integer.parseInt(txtPhone .getText()); st.setEmpname(empname); st.setSalary(sal); st.setPhone(phone); st.setId(eID); EmployeeDAOIm dao = new EmployeeDAOIm(); dao.update(st); Load(); txtEname.setText(""); txtSal.setText(""); txtPhone.setText(""); txtEname.requestFocus();
After search the record by using student what you want delete. and click delete button.
Employee st = new Employee(); st.setId(eID); EmployeeDAOIm dao = new EmployeeDAOIm(); dao.delete(st); Load(); txtEname.setText(""); txtSal.setText(""); txtPhone.setText(""); txtEname.requestFocus(); Buy the Source Code
Inventory Management App in React.this app explain the complete module of the Inventory sales management system in React…
Introduction to Inventory Management with POS Systems Efficient inventory management is crucial for businesses looking…
In today's fast-paced business environment, an efficient inventory management system is crucial for success. Companies…
Introduction to Java Inventory Management Systems In today's fast-paced business environment, effective inventory management is…
Initialize the employee number, Hourswork,and Hoursrate to calculate a grosswage use the following condition. if…
Act as a Java developer to create a program that calculates the gross wage for…