-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEmpDAO.java
More file actions
134 lines (104 loc) · 3.06 KB
/
Copy pathEmpDAO.java
File metadata and controls
134 lines (104 loc) · 3.06 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package java0929_jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class EmpDAO {
Connection conn = null;
Statement stmt = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
private static EmpDAO dao = new EmpDAO();
private EmpDAO() {
}
public static EmpDAO getInstance() {
return dao;
}
private Connection init() throws ClassNotFoundException, SQLException {
Class.forName("oracle.jdbc.OracleDriver");
String url = "jdbc:oracle:thin://@127.0.0.1:1521:xe";
String username = "hr";
String password = "a1234";
conn = DriverManager.getConnection(url, username, password);
return conn;
}
private void exit() throws SQLException {
if (rs != null)
rs.close();
if (stmt != null)
stmt.close();
if (pstmt != null)
pstmt.close();
if (conn != null)
conn.close();
}
public List<EmpDTO> rangeMethod(HashMap<String, Integer> map) {
List<EmpDTO> aList = new ArrayList<EmpDTO>();
try {
conn = init();
// String sql = "SELECT a.* FROM (SELECT rownum rm, e.* FROM EMPLOYEES e ORDER
// BY EMPLOYEE_ID) a WHERE rm > "
// + map.get("start") + " AND rm <= " + map.get("end");
String sql = "SELECT a.* FROM (SELECT rownum rm, e.* FROM EMPLOYEES e ORDER BY EMPLOYEE_ID) a WHERE rm > ? AND rm <= ?";
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, map.get("start"));
pstmt.setInt(2, map.get("end"));
rs = pstmt.executeQuery();
// stmt = conn.createStatement();
// rs = stmt.executeQuery(sql);
while (rs.next()) {
EmpDTO dto = new EmpDTO();
dto.setEmployee_id(rs.getInt("employee_id"));
dto.setFirst_name(rs.getString("first_name"));
dto.setSalary(rs.getInt("salary"));
dto.setHire_date(rs.getDate("hire_date"));
aList.add(dto);
}
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
} finally {
try {
exit();
} catch (SQLException e) {
e.printStackTrace();
}
}
return aList;
}
public List<EmpDTO> searchMethod(String str) {
List<EmpDTO> aList = new ArrayList<EmpDTO>();
try {
conn = init();
String sql = "SELECT * FROM EMPLOYEES WHERE lower(first_name) LIKE lower('%' || ? || '%')";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, str);
rs = pstmt.executeQuery();
// String sql = "SELECT * FROM EMPLOYEES WHERE lower(first_name) LIKE lower('%"
// + str + "%')";
// stmt = conn.createStatement();
// rs = stmt.executeQuery(sql);
while (rs.next()) {
EmpDTO dto = new EmpDTO();
dto.setEmployee_id(rs.getInt("employee_id"));
dto.setFirst_name(rs.getString("first_name"));
dto.setSalary(rs.getInt("salary"));
dto.setHire_date(rs.getDate("hire_date"));
aList.add(dto);
}
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
} finally {
try {
exit();
} catch (SQLException e) {
e.printStackTrace();
}
}
return aList;
}
}