设为首页 加入收藏

TOP

工具类之数据库工具类:DBUtil(采用反射机制)(一)
2014-11-24 08:16:23 来源: 作者: 【 】 浏览:13
Tags:工具 数据库 DBUtil 采用 反射 机制
经常操作数据库的码农们一定知道操作数据库是一项很复杂的工作,它不仅要解决各种乱码的问题还要解决各种数据表的增删改查等的操作。

另外每次操作数据库都要用到数据库连接、执行SQL语句、关闭连接的操作,所以在这里我就把这些功能封装到了一个工具类中,该类使用的是反射机制写成的,也就是说它可以帮助你完成对任何数据表的操作。关键代码如下:

首先是配置文件:config.properties

driverName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/
dateName=dateName
sqlSetting= useUnicode=true&characterEncoding=utf-8
user=userName
pwd=userPwd

其次是数据库连接工具类:DBUtil.java

public class DBUtil {

private static String driverName = null;
private static String url = null;
private static String dateName = null;
private static String sqlSetting = null;
private static String user = null;
private static String pwd = null;

/**
* 读取配置文件并为初始化成员变量
*/
static{
InputStream is = DBUtil.class.getClassLoader().getResourceAsStream("config.properties");
Properties properties = new Properties();
try {
if(is != null)
properties.load(is);
} catch (IOException e) {
e.printStackTrace();
}
driverName = properties.getProperty("driverName");
url = properties.getProperty("url");
dateName = properties.getProperty("dateName");
sqlSetting = properties.getProperty("sqlSetting");
user = properties.getProperty("user");
pwd = properties.getProperty("pwd");
url += dateName + sqlSetting;
}

/**
* 加载数据库驱动
*/
static{
try {
Class.forName(driverName);
} catch (ClassNotFoundException e) {
throw new ExceptionInInitializerError("加载驱动出错");
}
}

/**
* 获取数据库连接
* @return 返回数据库的连接
*/
public static Connection getConnection(){
try {
return DriverManager.getConnection(url, user, pwd);
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}

/**
* 关闭数据库连接
* @param rs 要关闭结果集对象
* @param pstat 要关闭的预编译语句对象
* @param conn 要关闭的数据库连接
*/
public static void close(ResultSet rs,PreparedStatement pstat,Connection conn){
try {
if(rs != null)
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
if(pstat != null)
pstat.close();
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
if(conn != null)
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}

然后就是泛型表的增删改查类:

public class DBDao {

/**
* 保存给出的t对象到相应的数据库中
*
* @param t 要保存到数据库的对象
*/
public static void insert(T t) {
// 获取对象t的class对象
@SuppressWarnings("unchecked")
Class cla = (Class) t.getClass();
// 获取对象t的所有字段
Field[] fields = cla.getDeclaredFields();
// 声明列表用于存放对象t的字段变量名
List keys = new ArrayList();
// 声明列表用于存放对象t的字段的值
List values = new ArrayList();
// 声明Method对象用于接收字段的get方法
Method method = null;
// 声明Object对象用于接收字段值
Object obj = null;
// 如果字段数组不为空,遍历对象t的字段数组
if (fields != null && fields.length > 0) {
for (Field field : fields) {
// 如果该字段不是ID字段,就保存到字段列表中
if (!field.getName().equals("id")) {
keys.add(field.getName());
try {
// 获取该字段对应的get方法
method = cla.getDeclaredMethod(getMethodName(field
.getName()));
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
}
try {
// 执行该字段的get方法并接收返回值
obj = method.invoke(t);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
// 将返回的结果保存到字段值列表中
values.add(obj);
}
}
}
// 组拼sql语句
StringBuffer sql = new StringBuffer("insert into "
+ cla.getName().substring(cla.getName().lastIndexOf(".") + 1)
+ "(");
StringBuffer sqlValues = new StringBuffer("values(");
for (int i = 0; i < keys.size() - 1; i++) {
sql.append(keys.get(i) + ",");
sqlValues.append(" ,");
}
sql.append(keys.get(keys.si

首页 上一页 1 2 3 4 5 下一页 尾页 1/5/5
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇[Mongo]按时间分组统计(时间格式.. 下一篇postgresql数据库配置csv格式日志..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·如何从内核协议栈到 (2025-12-27 03:19:09)
·什么是网络协议?有哪 (2025-12-27 03:19:06)
·TCP/ IP协议有哪些 (2025-12-27 03:19:03)
·怎样用 Python 写一 (2025-12-27 02:49:19)
·如何学习python数据 (2025-12-27 02:49:16)