-
Notifications
You must be signed in to change notification settings - Fork 467
Expand file tree
/
Copy pathJdbcTemplate.java
More file actions
102 lines (82 loc) · 3.01 KB
/
JdbcTemplate.java
File metadata and controls
102 lines (82 loc) · 3.01 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
package core.jdbc;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.springframework.jdbc.core.PreparedStatementCreator;
public class JdbcTemplate {
public void update(String sql,PreparedStatementSetter pss) throws DateAccessException {
try(Connection con = ConnectionManager.getConnection();
PreparedStatement pstmt = con.prepareStatement(sql);
) {
pss.setParameters(pstmt);
pstmt.executeUpdate();
} catch (SQLException e) {
throw new DateAccessException(e);
}
}
public void update(PreparedStatementCreator psc,KeyHolder holder) throws DateAccessException {
try(Connection con = ConnectionManager.getConnection()) {
PreparedStatement pstmt = psc.createPreparedStatement(con);
pstmt.executeUpdate();
ResultSet rs=pstmt.getGeneratedKeys();
if(rs.next()) {
holder.setId(rs.getLong(1));
}
rs.close();
} catch (SQLException e) {
throw new DateAccessException(e);
}
}
public void update(String sql,Object... parameters)throws DateAccessException {
update(sql,createPreparedStatementSetter(parameters));
}
public <T> List<T> query(String sql,PreparedStatementSetter pss,RowMapper<T> rm) throws DateAccessException{
ResultSet rs = null;
try(Connection con= ConnectionManager.getConnection();
PreparedStatement pstmt = con.prepareStatement(sql)){
pss.setParameters(pstmt);
rs = pstmt.executeQuery();
List<T> result = new ArrayList<T>();
while(rs.next()){
result.add(rm.mapRow(rs));
}
return result;
} catch (SQLException e) {
throw new DateAccessException(e);
}finally {
if(rs!=null) {
try {
rs.close();
} catch (SQLException e) {
throw new DateAccessException(e);
}
}
}
}
public <T> List<T> query(String sql,RowMapper<T> rm,Object... parameters) throws DateAccessException{
return query(sql,createPreparedStatementSetter(parameters),rm);
}
public <T> T queryForObject(String sql,PreparedStatementSetter pss,RowMapper<T> rm) throws DateAccessException{
List<T> result = query(sql,pss,rm);
if(result.isEmpty()) {
return null;
}else {
return result.get(0);
}
}
public <T> T queryForObject(String sql,RowMapper<T> rm,Object... parameters) throws DateAccessException{
return queryForObject(sql,createPreparedStatementSetter(parameters),rm);
}
private PreparedStatementSetter createPreparedStatementSetter(Object...parameters) {
return new PreparedStatementSetter() {
public void setParameters(PreparedStatement pstmt)throws SQLException{
for(int i=0;i<parameters.length;i++) {
pstmt.setObject(i+1,parameters[i]);
}
}
};
}
}