-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEmployeeManager.java
More file actions
102 lines (94 loc) · 3.93 KB
/
EmployeeManager.java
File metadata and controls
102 lines (94 loc) · 3.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class EmployeeManager {
public void addEmployee(FullTimeEmployee emp) {
try (Connection conn = DBConnection.getConnection()) {
String sql = "INSERT INTO employeeData (empId, name, phoneNumber, emailAddress, ssn, salary, jobTitle, division) VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
PreparedStatement stmt = conn.prepareStatement(sql);
stmt.setInt(1, emp.empId);
stmt.setString(2, emp.name);
stmt.setString(3, emp.phoneNumber);
stmt.setString(4, emp.emailAddress);
stmt.setString(5, emp.ssn);
stmt.setDouble(6, emp.salary);
stmt.setString(7, emp.jobTitle);
stmt.setString(8, emp.division);
stmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
public FullTimeEmployee searchEmployee(String keyword) {
try (Connection conn = DBConnection.getConnection()) {
String sql = "SELECT * FROM employeeData WHERE name = ? OR ssn = ? OR empId = ?";
PreparedStatement stmt = conn.prepareStatement(sql);
stmt.setString(1, keyword);
stmt.setString(2, keyword);
stmt.setString(3, keyword);
ResultSet rs = stmt.executeQuery();
if (rs.next()) {
return new FullTimeEmployee(
rs.getString("name"),
rs.getString("phoneNumber"),
rs.getString("emailAddress"),
rs.getInt("empId"),
rs.getString("ssn"),
rs.getDouble("salary"),
rs.getString("jobTitle"),
rs.getString("division")
);
}
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
public void updateEmployee(int empId, String field, String newValue) {
try (Connection conn = DBConnection.getConnection()) {
String sql = "UPDATE employeeData SET " + field + " = ? WHERE empId = ?";
PreparedStatement stmt = conn.prepareStatement(sql);
stmt.setString(1, newValue);
stmt.setInt(2, empId);
stmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
public void updateSalary(double lowerBound, double upperBound, double percentage) {
try (Connection conn = DBConnection.getConnection()) {
String sql = "UPDATE employeeData SET salary = salary * (1 + ? / 100) WHERE salary >= ? AND salary < ?";
PreparedStatement stmt = conn.prepareStatement(sql);
stmt.setDouble(1, percentage);
stmt.setDouble(2, lowerBound);
stmt.setDouble(3, upperBound);
stmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
public List<FullTimeEmployee> getEmployees() {
List<FullTimeEmployee> employees = new ArrayList<>();
try (Connection conn = DBConnection.getConnection()) {
String sql = "SELECT * FROM employeeData";
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
FullTimeEmployee emp = new FullTimeEmployee(
rs.getString("name"),
rs.getString("phoneNumber"),
rs.getString("emailAddress"),
rs.getInt("empId"),
rs.getString("ssn"),
rs.getDouble("salary"),
rs.getString("jobTitle"),
rs.getString("division")
);
employees.add(emp);
}
} catch (SQLException e) {
e.printStackTrace();
}
return employees;
}
}