实现最简单的JDBC的封装
只需要三个类 JDBCUtil BaseDaoJdbc BaseDaoJdbcImpl
1. [代码]JDBC配置文件
1
2
3
4
5
6
7
8
9
|
#============================#
#===== Database sttings =====#
#============================#
#mysql database setting
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/database?useUnicode=true&characterEncoding=utf-8
jdbc.username=root
jdbc.password=root
|
2. [代码]JDBCUtil
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
|
public
class
JDBCUtil {
// 日志接口
private
static
Logger logger = LoggerFactory.getLogger(JDBCUtil.
class
);
//JDBC配置属性
private
static
Properties ps =
new
Properties();
// 配置文件位置
private
static
final
String db_setting =
"/config/acs-db.properties"
;
// 初始化JDBC配置文件属性
static
{
try
{
if
(JDBCUtil.
class
.getResourceAsStream(db_setting) ==
null
){
if
(logger.isInfoEnabled())logger.info(
"请调用 getInstance(clazz) 方法! "
);
}
else
{
ps.load(JDBCUtil.
class
.getResourceAsStream(db_setting));
// 加载JDBC驱动
Class.forName(ps.getProperty(
"jdbc.driver"
));
if
(logger.isInfoEnabled())logger.info(
"JDBC驱动启动成功! "
+ps.getProperty(
"jdbc.driver"
));
}
}
catch
(IOException | ClassNotFoundException e) {
logger.error(e.getMessage(),e);
}
}
private
JDBCUtil(){}
private
static
JDBCUtil jdbcUtil =
new
JDBCUtil();
/**
*
* getInstance(获取JDBCUtil实例)
* (注意事项:同项目下调用 – 可选)
* @return
* @exception
* @since 1.0.0
*/
public
static
JDBCUtil getInstance(){
return
jdbcUtil;
}
/**
* getInstance(获取JDBCUtil实例)
* (注意事项:不同项目下调用 – 可选)
* @param clazz 调用类字节码
* @return
* @throws IOException
* @exception
* @since 1.0.0
*/
public
static
JDBCUtil getInstance(Class<?> clazz)
throws
IOException{
if
(clazz.getResourceAsStream(db_setting) ==
null
)
throw
new
RuntimeException(
"acs-db.properties is not found "
) ;
ps.load(clazz.getResourceAsStream(db_setting));
// 加载JDBC驱动
try
{
Class.forName(ps.getProperty(
"jdbc.driver"
));
}
catch
(ClassNotFoundException e) {
logger.error(e.getMessage(),e);
}
return
jdbcUtil;
}
/**
*
* getConnection(获取数据库连接)
* (注意事项:-无)
* @return
* @exception
* @since 1.0.0
*/
public
Connection getConnection(){
Connection conn =
null
;
try
{
conn = DriverManager.getConnection(ps.getProperty(
"jdbc.url"
),
ps.getProperty(
"jdbc.username"
), ps.getProperty(
"jdbc.password"
));
}
catch
(SQLException e) {
logger.error(e.getMessage(),e);
}
return
conn;
}
// JDBC BASE DAO
private
BaseDaoJdbc baseDaoJdbc =
new
BaseDaoJdbcimpl();
/**
* getBaseDaoJdbc(获取原始的JDBC Dao 层)
* (注意事项:-无)
* @return
* @exception
* @since 1.0.0
*/
public
BaseDaoJdbc getBaseDaoJdbc (){
return
baseDaoJdbc;
}
/**
*
* query(查询方法)
* (注意事项: – 目前只支持 Map List返回值)
* @param resultClass 返回类型 如: Map.class
* @return
* @throws SQLException
* @exception
* @since 1.0.0
*/
public
<E> E query (String sql,Class<E> resultClass,Object ... obj){
ResultSet rs = baseDaoJdbc.queryAll(sql, getConnection(), obj);
try
{
if
(resultClass == Map.
class
){
if
(rs.next())
return
(E) getResultMap(rs);
}
else
if
(resultClass == List.
class
){
return
(E) getResultList(rs);
}
else
{
throw
new
RuntimeException(
""
+resultClass +
" 该类型目前还没有做扩展!"
);
}
}
catch
(SQLException e) {
logger.error(e.getMessage(),e);
}
finally
{
try
{
baseDaoJdbc.closeAll(rs, rs.getStatement(), rs.getStatement().getConnection());
}
catch
(SQLException e) {
logger.error(e.getMessage(),e);
}
if
(logger.isInfoEnabled())logger.info(
"关闭数据库连接!"
);
}
return
null
;
}
/**
*
* executeUpdate(增加、修改、删除 操作)
* (注意事项:无)
* @param sql
* @param obj
* @return
* @exception
* @since 1.0.0
*/
public
int
executeUpdate(String sql,Object ...obj){
int
k =
0
;
k = getBaseDaoJdbc().executeUpdate(sql, getConnection(), obj);
return
k;
}
/*
* 解析ResultSet 表列数据
*/
private
Map<String,Object> getResultMap(ResultSet rs)
throws
SQLException{
Map<String, Object> rawMap =
new
HashMap<String, Object>();
ResultSetMetaData rsmd = rs.getMetaData();
// 表对象信息
int
count = rsmd.getColumnCount();
// 列数
// 遍历之前需要调用 next()方法
for
(
int
i =
1
; i <= count; i++) {
String key = rsmd.getColumnLabel(i);
Object value = rs.getObject(key);
rawMap.put(key, value);
}
return
rawMap;
}
/*
* 解析ResultSet 表数据
*/
private
List<Map<String,Object>> getResultList(ResultSet rs)
throws
SQLException{
List<Map<String,Object>> rawList =
new
ArrayList<Map<String,Object>>();
while
(rs.next()){
Map<String, Object> rawMap = getResultMap(rs);
rawList.add(rawMap);
}
return
rawList;
}
}
|
3. [代码]BaseDaoJdbc
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
|
public
interface
BaseDaoJdbc {
/**
* 关闭所有连接
* @param conn
* @param stmt
* @param rst
* @return
*/
public
boolean
closeAll(ResultSet rst,Statement stmt , Connection conn);
/**
* 关闭连接对象
* @param conn
* @return
*/
public
boolean
closeConnection(Connection conn);
/**
* 关闭执行sql对象
* @param stmt
* @return
*/
public
boolean
closeStatement(Statement stmt);
/**
* 关闭结果集
* @param rst
* @return
*/
public
boolean
closeResultSet(ResultSet rst);
/**
* 增删改
* @param sql
* @param conn
* @param obj
* @return
*/
public
int
executeUpdate(String sql,Connection conn,Object...obj);
/**
* 查询所有
* @param sql
* @param conn
* @param obj
* @return
*/
public
ResultSet queryAll(String sql,Connection conn, Object... obj);
}
|
4. [代码]BaseDaoJdbcImpl
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
|
public
class
BaseDaoJdbcimpl
implements
BaseDaoJdbc {
public
boolean
closeAll(ResultSet rst,Statement stmt , Connection conn) {
// TODO 关闭所有连接
boolean
flag =
false
;
try
{
if
(rst!=
null
){
rst.close();
}
if
(stmt!=
null
){
stmt.close();
}
if
(conn!=
null
&&!conn.isClosed()){
conn.close();
flag =
true
;
}
}
catch
(SQLException e) {
e.printStackTrace();
}
return
flag;
}
public
boolean
closeConnection(Connection conn) {
// TODO 关闭Connection对象
try
{
if
(conn!=
null
&&!conn.isClosed()){
try
{
conn.close();
return
true
;
}
catch
(SQLException e) {
e.printStackTrace();
}
}
}
catch
(SQLException e) {
e.printStackTrace();
}
return
false
;
}
public
boolean
closeResultSet(ResultSet rst) {
// TODO 关闭ResultSet对象
if
(rst!=
null
){
try
{
rst.close();
return
true
;
}
catch
(SQLException e) {
e.printStackTrace();
}
}
return
false
;
}
public
boolean
closeStatement(Statement stmt) {
// TODO 关闭Statement 对象
if
(stmt!=
null
){
if
(stmt
instanceof
PreparedStatement){
try
{
((PreparedStatement) stmt).close();
}
catch
(SQLException e) {
e.printStackTrace();
}
}
}
return
false
;
}
/**
* 增删改
* @param sql
* @param obj
* @return
*/
public
int
executeUpdate(String sql,Connection conn,Object...obj){
int
i =
0
;
PreparedStatement psts =
null
;
try
{
psts = conn.prepareStatement(sql) ;
if
(obj!=
null
&& obj.length>
0
){
for
(
int
j=
0
;j<obj.length;j++){
psts.setObject((j+
1
), obj[j]) ;
}
}
i = psts.executeUpdate() ;
}
catch
(SQLException e) {
e.printStackTrace();
}
finally
{
closeAll(
null
, psts, conn);
}
return
i ;
}
/**
* 查询所有
* @param sql
* @param obj
* @return ResultSet
*/
public
ResultSet queryAll(String sql,Connection conn, Object... obj) {
PreparedStatement psts =
null
;
ResultSet rs =
null
;
try
{
psts = conn.prepareStatement(sql) ;
if
(obj!=
null
&& obj.length>
0
){
for
(
int
j=
0
;j<obj.length;j++){
psts.setObject((j+
1
), obj[j]) ;
}
}
rs = psts.executeQuery() ;
}
catch
(SQLException e) {
e.printStackTrace();
}
return
rs ;
}
|