Skip to content

Commit 1dd3a16

Browse files
committed
feat: implement Employee and EmployeeDAO classes with CRUD operations
1 parent 954ac52 commit 1dd3a16

File tree

3 files changed

+154
-0
lines changed

3 files changed

+154
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.example.employeemanager;
2+
3+
public class Employee {
4+
private int id;
5+
private String name;
6+
private String department;
7+
private double salary;
8+
9+
public Employee(int id, String name, String department, double salary) {
10+
this.id = id;
11+
this.name = name;
12+
this.department = department;
13+
this.salary = salary;
14+
}
15+
16+
public int getId() {
17+
return id;
18+
}
19+
20+
public void setId(int id) {
21+
this.id = id;
22+
}
23+
24+
public String getName() {
25+
return name;
26+
}
27+
28+
public void setName(String name) {
29+
this.name = name;
30+
}
31+
32+
public String getDepartment() {
33+
return department;
34+
}
35+
36+
public void setDepartment(String department) {
37+
this.department = department;
38+
}
39+
40+
public double getSalary() {
41+
return salary;
42+
}
43+
44+
public void setSalary(double salary) {
45+
this.salary = salary;
46+
}
47+
48+
@Override
49+
public String toString() {
50+
return "Employee{" +
51+
"id=" + id +
52+
", name='" + name + '\'' +
53+
", department='" + department + '\'' +
54+
", salary=" + salary +
55+
'}';
56+
}
57+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package com.example.employeemanager;
2+
3+
import java.sql.*;
4+
import java.util.ArrayList;
5+
import java.util.List;
6+
7+
public class EmployeeDAO {
8+
private final String url = "jdbc:mysql://localhost:3306/employeedb";
9+
private final String user = "root";
10+
private final String password = "password";
11+
12+
public Connection connect() throws SQLException {
13+
return DriverManager.getConnection(url, user, password);
14+
}
15+
16+
public void addEmployee(Employee employee) {
17+
String sql = "INSERT INTO employees (id, name, department, salary) VALUES (?, ?, ?, ?)";
18+
try (Connection conn = connect(); PreparedStatement pstmt = conn.prepareStatement(sql)) {
19+
pstmt.setInt(1, employee.getId());
20+
pstmt.setString(2, employee.getName());
21+
pstmt.setString(3, employee.getDepartment());
22+
pstmt.setDouble(4, employee.getSalary());
23+
pstmt.executeUpdate();
24+
} catch (SQLException e) {
25+
e.printStackTrace();
26+
}
27+
}
28+
29+
public List<Employee> getAllEmployees() {
30+
List<Employee> employees = new ArrayList<>();
31+
String sql = "SELECT * FROM employees";
32+
try (Connection conn = connect(); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery(sql)) {
33+
while (rs.next()) {
34+
Employee employee = new Employee(
35+
rs.getInt("id"),
36+
rs.getString("name"),
37+
rs.getString("department"),
38+
rs.getDouble("salary")
39+
);
40+
employees.add(employee);
41+
}
42+
} catch (SQLException e) {
43+
e.printStackTrace();
44+
}
45+
return employees;
46+
}
47+
48+
public void updateEmployee(Employee employee) {
49+
String sql = "UPDATE employees SET name = ?, department = ?, salary = ? WHERE id = ?";
50+
try (Connection conn = connect(); PreparedStatement pstmt = conn.prepareStatement(sql)) {
51+
pstmt.setString(1, employee.getName());
52+
pstmt.setString(2, employee.getDepartment());
53+
pstmt.setDouble(3, employee.getSalary());
54+
pstmt.setInt(4, employee.getId());
55+
pstmt.executeUpdate();
56+
} catch (SQLException e) {
57+
e.printStackTrace();
58+
}
59+
}
60+
61+
public void deleteEmployee(int id) {
62+
String sql = "DELETE FROM employees WHERE id = ?";
63+
try (Connection conn = connect(); PreparedStatement pstmt = conn.prepareStatement(sql)) {
64+
pstmt.setInt(1, id);
65+
pstmt.executeUpdate();
66+
} catch (SQLException e) {
67+
e.printStackTrace();
68+
}
69+
}
70+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.example.employeemanager;
2+
3+
import java.util.List;
4+
5+
public class Main {
6+
public static void main(String[] args) {
7+
EmployeeDAO employeeDAO = new EmployeeDAO();
8+
9+
// Add a new employee
10+
Employee emp1 = new Employee(1, "John Doe", "Engineering", 75000);
11+
employeeDAO.addEmployee(emp1);
12+
13+
// Retrieve all employees
14+
List<Employee> employees = employeeDAO.getAllEmployees();
15+
System.out.println("All Employees:");
16+
for (Employee emp : employees) {
17+
System.out.println(emp);
18+
}
19+
20+
// Update an employee
21+
emp1.setSalary(80000);
22+
employeeDAO.updateEmployee(emp1);
23+
24+
// Delete an employee
25+
employeeDAO.deleteEmployee(1);
26+
}
27+
}

0 commit comments

Comments
 (0)