为了简化一些操作,同时为了能兼容其他数据库,因此并没用使用SqlHelper的形式。
这只是个简单模型的开发方式,没用纳入数据连接池等内容。
我看了网上大部分的SqlHelper在类型转换方面都很有问题,而且返回结果使用ArrayList包装了一下。在这里的包装主要是为了避免这两个问题。
首先申明数据库接口,这代表了一个数据库能进行的操作。
package
dao;
import
java.sql.SQLException;
public
interface
Database {
int
ExecuteNoneQuery(String cmdtext, Parameters parms)
throws
SQLException;
<
T
>
T ExecuteObject(Data2Object
<
T
>
convertor, String cmdtext,
Parameters parms)
throws
SQLException;
Object ExecuteScalar(String cmdtext, Parameters parms)
throws
SQLException;
Parameters CreateFixedParms(
int
size);
}
实现该接口的MySql包装形式,其实和SqlHelper差不多:
package
dao;
import
java.sql.Connection;
import
java.sql.Date;
import
java.sql.DriverManager;
import
java.sql.PreparedStatement;
import
java.sql.ResultSet;
import
java.sql.SQLException;
public
class
MySqlDatabase
implements
Database {
private
Connection conn;
public
MySqlDatabase(String connString)
throws
SQLException {
conn
=
DriverManager.getConnection(connString);
}
public
int
ExecuteNoneQuery(String cmdtext, Parameters parms)
throws
SQLException {
PreparedStatement pstmt
=
null
;
try
{
pstmt
=
conn.prepareStatement(cmdtext);
prepareCommand(pstmt, parms);
return
pstmt.executeUpdate();
}
catch
(Exception ex) {
}
finally
{
if
(pstmt
!=
null
) {
pstmt.clearParameters();
pstmt.close();
}
if
(conn
!=
null
)
conn.close();
}
return
-
1
;
}
public
<
T
>
T ExecuteObject(Data2Object
<
T
>
convertor, String cmdtext,
Parameters parms)
throws
SQLException {
PreparedStatement pstmt
=
null
;
ResultSet rs
=
null
;
try
{
pstmt
=
conn.prepareStatement(cmdtext);
prepareCommand(pstmt, parms);
rs
=
pstmt.executeQuery();
return
convertor.DataMap(rs);
}
catch
(Exception ex) {
}
finally
{
if
(rs
!=
null
)
rs.close();
if
(pstmt
!=
null
)
pstmt.close();
if
(conn
!=
null
)
conn.close();
}
return
null
;
}
public
Object ExecuteScalar(String cmdtext, Parameters parms)
throws
SQLException {
PreparedStatement pstmt
=
null
;
ResultSet rs
=
null
;
try
{
pstmt
=
conn.prepareStatement(cmdtext);
prepareCommand(pstmt, parms);
rs
=
pstmt.executeQuery();
if
(rs.next()) {
return
rs.getObject(
1
);
}
else
{
return
null
;
}
}
catch
(Exception e) {
}
finally
{
if
(rs
!=
null
)
rs.close();
if
(pstmt
!=
null
)
pstmt.close();
if
(conn
!=
null
)
conn.close();
}
return
null
;
}
private
void
prepareCommand(PreparedStatement pstmt, Parameters parms)
throws
SQLException {
if
(parms
!=
null
&&
parms.getLength()
>
0
) {
for
(
int
i
=
0
; i
<
parms.getLength(); i
++
) {
MySqlParameter parm
=
parms.getParameter(i);
String value
=
parm.getValue().toString();
switch
(parm.getType()) {
case
String:
pstmt.setString(i
+
1
, value);
break
;
case
Int16:
pstmt.setShort(i
+
1
, Short.parseShort(value));
break
;
case
Int32:
pstmt.setInt(i
+
1
, Integer.parseInt(value));
break
;
case
Int64:
pstmt.setLong(i
+
1
, Long.parseLong(value));
break
;
case
DateTime:
pstmt.setDate(i
+
1
, Date.valueOf(value));
break
;
default
:
pstmt.setObject(i
+
1
, value);
break
;
}
}
}
}
static
{
try
{
Class.forName(
"
com.mysql.jdbc.Driver
"
).newInstance();
}
catch
(Exception ex) {
}
}
public
Parameters CreateFixedParms(
int
size) {
return
new
FixedParameters(size);
}
}
Data2Object<T>接口负责将ResultSet转换为对象。
package
dao;
import
java.sql.ResultSet;
import
java.sql.SQLException;
public
interface
Data2Object
<
T
>
{
public
T DataMap(ResultSet rs)
throws
SQLException;
}
DatabaseManager负责调用
package
dao;
import
java.sql.SQLException;
public
abstract
class
DatabaseManager {
public
static
Database getMySqlDatabase()
throws
SQLException{
return
new
MySqlDatabase(
"
jdbc:mysql://localhost/test?user=root&password=123&useUnicode=true&characterEncoding=UTF-8
"
);
}
}
申明数据库枚举类型
package
dao;
public
enum
DBType {
String,
Int16,
Int32,
Int64,
DateTime,
}
无参数调用的情况:
package
bean;
import
java.sql.ResultSet;
import
java.sql.ResultSetMetaData;
import
java.sql.SQLException;
import
java.util.ArrayList;
import
java.util.List;
import
dao.Data2Object;
import
dao.Database;
import
dao.DatabaseManager;
import
dao.SqlHelper;
public
class
ListBean {
public
List
<
TestBean
>
Find()
throws
SQLException{
String sql
=
"
select * from TestTable
"
;
Database db
=
DatabaseManager.getMySqlDatabase();
return
db.ExecuteObject(
new
ListConvertor(), sql,
null
);
}
public
class
ListConvertor
implements
Data2Object
<
List
<
TestBean
>>
{
public
List
<
TestBean
>
DataMap(ResultSet rs)
throws
SQLException {
List
<
TestBean
>
tests
=
new
ArrayList
<
TestBean
>
();
while
(rs.next()) {
TestBean bean
=
new
TestBean();
bean.setId(rs.getInt(
1
));
bean.setName(rs.getString(
2
));
tests.add(bean);
}
return
tests;
}
}
}
申明参数接口
package
dao;
public
interface
Parameters {
void
Add(MySqlParameter parm);
int
getLength();
MySqlParameter getParameter(
int
i);
}
实现该接口
package
dao;
public
class
MySqlParameter {
private
DBType type;
private
int
len;
public
MySqlParameter(DBType type,
int
len, Object value) {
super
();
this
.type
=
type;
this
.len
=
len;
this
.value
=
value;
}
/**
*
@return
the type
*/
public
DBType getType() {
return
type;
}
/**
*
@param
type the type to set
*/
public
void
setType(DBType type) {
this
.type
=
type;
}
/**
*
@return
the len
*/
public
int
getLen() {
return
len;
}
/**
*
@param
len the len to set
*/
public
void
setLen(
int
len) {
this
.len
=
len;
}
/**
*
@return
the value
*/
public
Object getValue() {
return
value;
}
/**
*
@param
value the value to set
*/
public
void
setValue(Object value) {
this
.value
=
value;
}
private
Object value;
}
包装定长参数数组
package
dao;
public
class
FixedParameters
implements
Parameters {
private
MySqlParameter[] parms;
private
int
ptr
=
0
;
public
FixedParameters(
int
size){
parms
=
new
MySqlParameter[size];
}
public
void
Add(MySqlParameter parm) {
parms[ptr]
=
parm;
ptr
++
;
}
public
MySqlParameter[] getBuffer() {
return
parms;
}
public
int
getLength() {
return
parms.length;
}
public
MySqlParameter getParameter(
int
i) {
return
parms[i];
}
}
添加操作,这里包装地还不够好,没在调用部分把MySql专有方式隔离出去。懒得再写个继承关系了,将就一下把,呵呵
public
int
Add(TestBean test)
throws
SQLException{
String sql
=
"
insert into TestTable (name) values (?);
"
;
Database db
=
DatabaseManager.getMySqlDatabase();
Parameters parms
=
db.CreateFixedParms(
1
);
parms.Add(
new
MySqlParameter(DBType.String,
0
,test.getName()));
return
db.ExecuteNoneQuery(sql, parms);
}