forked from hoohack/CodeInJavaNotes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJdbcTemplate.java
More file actions
75 lines (62 loc) · 2.32 KB
/
Copy pathJdbcTemplate.java
File metadata and controls
75 lines (62 loc) · 2.32 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
package chapter16;
import javax.sql.DataSource;
import java.math.BigDecimal;
import java.sql.*;
import java.util.Objects;
public class JdbcTemplate {
private DataSource dataSource;
public JdbcTemplate(DataSource dataSource) {
this.dataSource = dataSource;
}
public DataSource getDataSource() {
return dataSource;
}
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
public void setSqlVal(PreparedStatement statement, int index, Object arg) throws SQLException {
if (arg instanceof Integer) {
statement.setInt(index, (Integer) arg);
} else if (arg instanceof String) {
statement.setString(index, (String)arg);
} else if (arg instanceof Boolean) {
statement.setBoolean(index, (Boolean)arg);
} else if (arg instanceof Byte) {
statement.setByte(index, (Byte)arg);
} else if (arg instanceof Short) {
statement.setShort(index, (Short)arg);
} else if (arg instanceof Long) {
statement.setLong(index, (Long)arg);
} else if (arg instanceof Float) {
statement.setFloat(index, (Float)arg);
} else if (arg instanceof Double) {
statement.setDouble(index, (Double)arg);
} else if (arg instanceof BigDecimal) {
statement.setBigDecimal(index, (BigDecimal)arg);
} else if (arg instanceof Time) {
statement.setTime(index, (Time)arg);
} else if (arg instanceof Timestamp) {
statement.setTimestamp(index, (Timestamp)arg);
} else if (arg instanceof Date) {
statement.setDate(index, (Date)arg);
} else if (arg instanceof byte[]){
statement.setBytes(index, (byte[]) arg);
} else {
System.out.print("Wrong type...");
}
}
public void update(String sql, Object... args) throws SQLException {
if (Objects.isNull(args)) {
return;
}
Connection connection = dataSource.getConnection();
PreparedStatement statement = connection.prepareStatement(sql);
int argCount = args.length;
for (int i = 0;i < argCount; i++) {
setSqlVal(statement, i + 1, args[i]);
}
statement.executeUpdate();
}
// other method
// TODO
}