-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathempServices.java
More file actions
83 lines (73 loc) · 2.73 KB
/
empServices.java
File metadata and controls
83 lines (73 loc) · 2.73 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
import java.sql.*;
import java.util.*;
import java.util.logging.Logger;
import java.util.stream.Collectors;
public class empServices {
static final connectionClass sqlConnector = new connectionClass();
static final Logger logger = Logger.getLogger(empServices.class.getName());
public void insertIntoTable(ArrayList<Employee> employees) {
PreparedStatement ps = null;
String insertQuery = "INSERT INTO employe(name, age, dept, location) VALUES (?, ?, ?, ?)";
try {
ps = sqlConnector.createPrepareStatement(insertQuery);
for (Employee e : employees) {
ps.setString(1, e.name);
ps.setInt(2, e.age);
ps.setString(3, e.dept);
ps.setString(4, e.location);
ps.executeUpdate();
}
} catch (SQLException e1) {
e1.printStackTrace();
} finally {
try {
if (ps != null) ps.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
public List<Employee> selectFromTable() {
List<Employee> Emp_Data_Db = new ArrayList<>();
Statement st = null;
ResultSet rs = null;
try {
st = sqlConnector.createStatement();
rs = st.executeQuery("SELECT * FROM employe");
while (rs.next()) {
Employee e = new Employee(
rs.getInt("emp_id"),
rs.getString("name"),
rs.getInt("age"),
rs.getString("dept"),
rs.getString("location")
);
Emp_Data_Db.add(e);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (rs != null) rs.close();
if (st != null) st.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
return Emp_Data_Db;
}
public void arraySortComparator(List<Employee> employees) {
logger.info("Sorted by Name (Comparator):");
employees.sort(Comparator.comparing(emp -> emp.name.toLowerCase()));
employees.forEach(e -> logger.info(e.toString()));
}
public List<Employee> getNoidaEmp(List<Employee> employees) {
System.out.println("Employees in Noida (using Stream API):");
return employees.stream()
.filter(e -> e.location.equalsIgnoreCase("Noida"))
.collect(Collectors.toList());
}
public void closeService() {
sqlConnector.closeConnection();
}
}