JDBC数据库通用DAO

package org.dave.common.database;
02
03 import java.sql.Connection;
04 import java.sql.SQLException;
05 import java.util.ResourceBundle;
06
07 import org.slf4j.Logger;
08 import org.slf4j.LoggerFactory;
09
10 import com.jolbox.bonecp.BoneCP;
11 import com.jolbox.bonecp.BoneCPConfig;
12
13 /**
14 * 数据库连接池
15 * @author David Day
16 */
17 public final class DatabaseConnectionPool {
18
19 private static final Logger LOG = LoggerFactory.getLogger(DatabaseConnectionPool.class);
20 private static final ResourceBundle BUNDLE = ResourceBundle.getBundle("connection");
21 private static final String DRIVER = "driver";
22 private static final String URL = "url";
23 private static final String USERNAME = "username";
24 private static final String PASSWORD = "password";
25 private static final String MAX_CONNECTION = "max_connection";
26 private static BoneCP pool;
27
28 /**
29 * 开启连接池
30 */
31 public static void startup() {
32 try {
33 Class.forName(BUNDLE.getString(DRIVER));
34 BoneCPConfig config = new BoneCPConfig();
35 config.setJdbcUrl(BUNDLE.getString(URL));
36 config.setUsername(BUNDLE.getString(USERNAME));
37 config.setPassword(BUNDLE.getString(PASSWORD));
38 config.setMaxConnectionsPerPartition(Integer.parseInt(BUNDLE.getString(MAX_CONNECTION)));
39 pool = new BoneCP(config);
40 } catch (Exception e) {
41 e.printStackTrace();
42 LOG.error(e.getMessage(), e);
43 throw new DatabaseException(e);
44 }
45 }
46
47 /**
48 * 关闭连接池
49 */
50 public static void shutdown() {
51 pool.shutdown();
52 }
53
54 /**
55 * @return 数据库连接
56 */
57 public static Connection getConnection() {
58 try {
59 return pool.getConnection();
60 } catch (SQLException e) {
61 e.printStackTrace();
62 LOG.error(e.getMessage(), e);
63 throw new DatabaseException(e);
64 }
65 }
66
67 }

[代码] 数据库异常01 package org.dave.common.database;
02
03 /**
04 * 数据库异常
05 * @author David Day
06 */
07 @SuppressWarnings("serial")
08 public class DatabaseException extends RuntimeException {
09
10 public DatabaseException(Throwable cause) {
11 super(cause);
12 }
13
14 }

[代码] 事务控制001 package org.dave.common.database;
002
003 import java.sql.Connection;
004 import java.sql.SQLException;
005
006 import org.slf4j.Logger;
007 import org.slf4j.LoggerFactory;
008
009 /**
010 * 数据库事务
011 * @author David Day
012 */
013 public class DatabaseTransaction {
014
015 /**
016 * 日志工具
017 */
018 private static final Logger LOG = LoggerFactory.getLogger(DatabaseTransaction.class);
019
020 /**
021 * 数据库连接
022 */
023 private Connection conn;
024
025 /**
026 * 实例化一个默认连接的事务
027 */
028 public DatabaseTransaction() {
029 this(DatabaseConnectionPool.getConnection());
030 }
031
032 /**
033 * 实例化一个默认连接的事务
034 * @param isOpenTrans 是否打开事务
035 */
036 public DatabaseTransaction(boolean isOpenTrans) throws DatabaseException {
037 this(DatabaseConnectionPool.getConnection(), isOpenTrans);
038 }
039
040 /**
041 * 实例化一个默认连接的事务
042 * @param conn 数据库连接
043 */
044 public DatabaseTransaction(Connection conn) {
045 this.conn = conn;
046 }
047
048 /**
049 * 实例化一个默认连接的事务
050 * @param conn 数据库连接
051 * @param isOpenTrans 是否打开事务
052 */
053 public DatabaseTransaction(Connection conn, boolean isOpenTrans) throws DatabaseException {
054 this.conn = conn;
055 setAutoCommit(!isOpenTrans);
056 }
057
058 /**
059 * @return 数据库连接
060 */
061 public Connection getConnection() {
062 return conn;
063 }
064
065 /**
066 * 设置是否自动提交
067 * @param autoCommit 自动提交
068 * @throws DatabaseException
069 */
070 private void setAutoCommit(boolean autoCommit) throws DatabaseException {
071 try {
072 conn.setAutoCommit(autoCommit);
073 } catch (SQLException e) {
074 e.printStackTrace();
075 LOG.error(e.getMessage(), e);
076 throw new DatabaseException(e);
077 }
078 }
079
080 /**
081 * 开始事务
082 * @throws DatabaseException
083 */
084 public void begin() throws DatabaseException {
085 setAutoCommit(false);
086 }
087
088 /**
089 * @return 是否打开事务
090 * @throws DatabaseException
091 */
092 public boolean isBegin() throws DatabaseException {
093 try {
094 return !conn.getAutoCommit();
095 } catch (SQLException e) {
096 e.printStackTrace();
097 LOG.error(e.getMessage(), e);
098 throw new DatabaseException(e);
099 }
100 }
101
102 /**
103 * 提交
104 * @throws DatabaseException
105 */
106 public void commit() throws DatabaseException {
107 try {
108 conn.commit();
109 } catch (SQLException e) {
110 e.printStackTrace();
111 LOG.error(e.getMessage(), e);
112 throw new DatabaseException(e);
113 }
114 }
115
116 /**
117 * 回滚
118 * @throws DatabaseException
119 */
120 public void rollback() throws DatabaseException {
121 try {
122 conn.rollback();
123 } catch (SQLException e) {
124 e.printStackTrace();
125 LOG.error(e.getMessage(), e);
126 throw new DatabaseException(e);
127 }
128 }
129
130 /**
131 * 关闭连接
132 * @throws DatabaseException
133 */
134 public void close() throws DatabaseException {
135 try {
136 conn.close();
137 } catch (SQLException e) {
138 e.printStackTrace();
139 LOG.error(e.getMessage(), e);
140 throw new DatabaseException(e);
141 }
142 }
143
144 /**
145 * @return 连接是否关闭
146 * @throws DatabaseException
147 */
148 public boolean isClose() throws DatabaseException {
149 try {
150 return conn.isClosed();
151 } catch (SQLException e) {
152 e.printStackTrace();
153 LOG.error(e.getMessage(), e);
154 throw new DatabaseException(e);
155 }
156 }
157
158 }

[代码] 通用数据模型1 package org.dave.common.database;
2
3 import java.io.Serializable;
4
5 @SuppressWarnings("serial")
6 public abstract class DataModel implements Serializable { }

[代码] 结果转换器01 package org.dave.common.database.convert;
02
03 import java.sql.ResultSet;
04 import java.sql.SQLException;
05
06 /**
07 * 结果映射器
08 * @author David Day
09 */
10 public interface ResultConverter<T> {
11
12 /**
13 * 映射
14 * @param rs 结果集
15 * @return 映射结果
16 * @throws SQLException
17 */
18 public T convert(ResultSet rs) throws SQLException ;
19
20 }

[代码] 通用dao001 package org.dave.common.database.access;
002
003 import java.sql.Connection;
004 import java.sql.PreparedStatement;
005 import java.sql.ResultSet;
006 import java.sql.SQLException;
007 import java.sql.Statement;
008 import java.util.ArrayList;
009 import java.util.List;
010
011 import org.dave.common.database.convert.ResultConverter;
012 import org.slf4j.Logger;
013 import org.slf4j.LoggerFactory;
014
015 /**
016 * 数据存取类
017 * @author David Day
018 */
019 public abstract class DataAccess {
020
021 /**
022 * 日志工具
023 */
024 private static final Logger LOG = LoggerFactory.getLogger(DataAccess.class);
025 /**
026 * 数据库连接
027 */
028 private Connection conn;
029
030 /**
031 * @param conn 数据库连接
032 */
033 protected DataAccess(Connection conn) {
034 this.conn = conn;
035 }
036
037 /**
038 * 插入数据
039 * @param sql
040 * @param generatedKeysConverter 主键映射
041 * @param params
042 * @return 主键
043 * @throws DataAccessException
044 */
045 protected <T> T insert(String sql, ResultConverter<T> generatedKeysConverter, Object... params) throws DataAccessException {
046 try {
047 PreparedStatement pstmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
048 setParameters(pstmt, params);
049 executeUpdate(pstmt);
050 ResultSet rs = pstmt.getGeneratedKeys();
051 nextResult(rs);
052 return convertResult(rs, generatedKeysConverter);
053 } catch (SQLException e) {
054 e.printStackTrace();
055 LOG.error(e.getMessage(), e);
056 throw new DataAccessException(e);
057 }
058 }
059
060 /**
061 * 更新数据
062 * @param sql
063 * @param params
064 * @return 影响行数
065 * @throws DataAccessException
066 */
067 protected int update(String sql, Object... params) throws DataAccessException {
068 return executeUpdate(getPreparedStatement(sql, params));
069 }
070
071 /**
072 * 查询单个结果
073 * @param <T>
074 * @param sql
075 * @param converter
076 * @param params
077 * @return
078 */
079 protected <T> T queryForObject(String sql, ResultConverter<T> converter, Object... params) {
080 ResultSet rs = executeQuery(sql, params);
081 if (nextResult(rs)) {
082 return convertResult(rs, converter);
083 } else {
084 return null;
085 }
086 }
087
088 /**
089 * 查询结果列表
090 * @param <T>
091 * @param sql
092 * @param converter
093 * @param params
094 * @return
095 */
096 protected <T> List<T> queryForList(String sql, ResultConverter<T> converter, Object... params) {
097 ResultSet rs = executeQuery(sql, params);
098 List<T> list = new ArrayList<T>();
099 while (nextResult(rs)) {
100 list.add(convertResult(rs, converter));
101 }
102 return list;
103 }
104
105 /**
106 * @param sql SQL语句
107 * @return 预编译声明
108 */
109 private PreparedStatement getPreparedStatement(String sql, Object... params) throws DataAccessException {
110 PreparedStatement pstmt = getPreparedStatement(sql);
111 setParameters(pstmt, params);
112 return pstmt;
113 }
114
115 /**
116 * @param sql SQL语句
117 * @return 预编译声明
118 */
119 private PreparedStatement getPreparedStatement(String sql) throws DataAccessException {
120 try {
121 return conn.prepareStatement(sql);
122 } catch (SQLException e) {
123 e.printStackTrace();
124 LOG.error(e.getMessage(), e);
125 throw new DataAccessException(e);
126 }
127 }
128
129 /**
130 * 为预编译声明传入参数
131 * @param pstmt 预编译声明
132 * @param params 参数
133 * @throws DataAccessException
134 */
135 private void setParameters(PreparedStatement pstmt, Object... params) throws DataAccessException {
136 try {
137 for (int i = 0; i < params.length; i++) {
138 pstmt.setObject(i + 1, params[i]);
139 }
140 } catch (SQLException e) {
141 e.printStackTrace();
142 LOG.error(e.getMessage(), e);
143 throw new DataAccessException(e);
144 }
145 }
146
147 /**
148 * 执行更新操作
149 * @param pstmt
150 * @return 影响行数
151 * @throws DataAccessException
152 */
153 private int executeUpdate(PreparedStatement pstmt) throws DataAccessException {
154 try {
155 return pstmt.executeUpdate();
156 } catch (SQLException e) {
157 e.printStackTrace();
158 LOG.error(e.getMessage(), e);
159 throw new DataAccessException(e);
160 }
161 }
162
163 /**
164 * 执行查询操作
165 * @param pstmt 预编译声明
166 * @return 结果集
167 * @throws DataAccessException
168 */
169 private ResultSet executeQuery(PreparedStatement pstmt) throws DataAccessException {
170 try {
171 return pstmt.executeQuery();
172 } catch (SQLException e) {
173 e.printStackTrace();
174 LOG.error(e.getMessage(), e);
175 throw new DataAccessException(e);
176 }
177 }
178
179 /**
180 * 执行查询操作
181 * @param sql SQL语句
182 * @param params 参数
183 * @return 结果集
184 * @throws DataAccessException
185 */
186 private ResultSet executeQuery(String sql, Object... params) throws DataAccessException {
187 return executeQuery(getPreparedStatement(sql, params));
188 }
189
190 /**
191 * 移动到下一行记录
192 * @param rs 结果集
193 * @return 是否有下一行记录
194 * @throws DataAccessException
195 */
196 private boolean nextResult(ResultSet rs) throws DataAccessException {
197 try {
198 return rs.next();
199 } catch (SQLException e) {
200 e.printStackTrace();
201 LOG.error(e.getMessage(), e);
202 throw new DataAccessException(e);
203 }
204 }
205
206 /**
207 * 映射
208 * @param rs 结果集
209 * @return 映射结果
210 * @throws DataAccessException
211 */
212 private <T> T convertResult(ResultSet rs, ResultConverter<T> converter) throws DataAccessException {
213 try {
214 return converter.convert(rs);
215 } catch (SQLException e) {
216 e.printStackTrace();
217 LOG.error(e.getMessage(), e);
218 throw new DataAccessException(e);
219 }
220 }
221
222 }

[代码] 数据库存取异常
view source

print
?01 package org.dave.common.database.access;
02
03 /**
04 * 数据库存取异常
05 * @author David Day
06 */
07 @SuppressWarnings("serial")
08 public class DataAccessException extends RuntimeException {
09
10 public DataAccessException(Throwable cause) {
11 super(cause);
12 }
13
14

你可能感兴趣的:(JDBC数据库通用DAO)