-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJobDAO.java
More file actions
72 lines (57 loc) · 1.46 KB
/
Copy pathJobDAO.java
File metadata and controls
72 lines (57 loc) · 1.46 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
package java0928_jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
public class JobDAO {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
public JobDAO() {
}
public Connection init() throws ClassNotFoundException, SQLException {
Class.forName("oracle.jdbc.OracleDriver");
String url = "jdbc:oracle:thin:@127.0.0.1:1521:xe";
String user = "hr";
String password = "a1234";
Connection conn = DriverManager.getConnection(url, user, password);
return conn;
}
public List<JobDTO> listJobs() {
List<JobDTO> aList = new ArrayList<JobDTO>();
try {
conn = init();
stmt = conn.createStatement();
String sql = "SELECT * FROM JOBS";
rs = stmt.executeQuery(sql);
while (rs.next()) {
JobDTO jDTO = new JobDTO();
jDTO.setJob_Id(rs.getString("job_id"));
jDTO.setJob_Title(rs.getString("job_title"));
jDTO.setMin_Salary(rs.getInt("min_salary"));
jDTO.setMax_Salary(rs.getInt("max_salary"));
aList.add(jDTO);
}
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
} finally {
try {
exit();
} catch (SQLException e) {
e.printStackTrace();
}
}
return aList;
}
public void exit() throws SQLException {
if (rs != null)
rs.close();
if (stmt != null)
stmt.close();
if (conn != null)
conn.close();
}
}