-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSqlHelper.java
More file actions
89 lines (68 loc) · 2.58 KB
/
SqlHelper.java
File metadata and controls
89 lines (68 loc) · 2.58 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
package ru.javaops.webapp.sql;
import ru.javaops.webapp.exception.StorageException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class SqlHelper {
public final ConnectionFactory connectionFactory;
public SqlHelper(ConnectionFactory connectionFactory) {
this.connectionFactory = connectionFactory;
}
public void execute(String statement) {
execute(statement, PreparedStatement::execute);
}
public <T> T execute(String statement, SqlExecutor<T> executor) {
try (Connection conn = connectionFactory.getConnection();
PreparedStatement ps = conn.prepareStatement(statement)) {
return executor.execute(ps);
} catch (SQLException e) {
throw ExceptionUtil.convertException(e);
}
}
public <T> T transactionExecute(SqlTransaction<T> executor) {
try (Connection conn = connectionFactory.getConnection()) {
try {
conn.setAutoCommit(false);
T res = executor.execute(conn);
conn.commit();
return res;
} catch (SQLException e) {
conn.rollback();
throw ExceptionUtil.convertException(e);
}
} catch (SQLException e) {
throw new StorageException(e);
}
}
// public void executeWhile(String uuid, String statement, Connection conn, SqlResultSetExecutor executor) throws SQLException {
// try (PreparedStatement ps = conn.prepareStatement(statement)) {
// if (uuid != null) {
// ps.setString(1, uuid);
// }
// ResultSet rs = ps.executeQuery();
// while (rs.next()) {
// executor.execute(rs);
// }
// }
// }
// public void execute(String statement, Connection conn, SqlVoidExecutor executor) throws SQLException {
// try (PreparedStatement ps = conn.prepareStatement(statement)) {
// executor.execute(ps);
// }
// }
// public void transactionalExecute(SqlTransaction executor) {
// try (Connection conn = connectionFactory.getConnection()) {
// try {
// conn.setAutoCommit(false);
// executor.execute(conn);
// conn.commit();
// } catch (SQLException e) {
// conn.rollback();
// throw ExceptionUtil.convertException(e);
// }
// } catch (SQLException e) {
// throw new StorageException(e);
// }
// }
}