设为首页 加入收藏

TOP

工具类之数据库工具类:DBUtil(采用反射机制)(二)
2014-11-24 08:16:23 来源: 作者: 【 】 浏览:16
Tags:工具 数据库 DBUtil 采用 反射 机制
ze() - 1) + ") ");
sqlValues.append(" )");
sql.append(sqlValues);
Connection conn = null;
PreparedStatement pstat = null;
try {
conn = DBUtil.getConnection();
pstat = conn.prepareStatement(sql.toString());
for (int i = 0; i < values.size(); i++) {
pstat.setObject(i + 1, values.get(i));
}
pstat.execute();
} catch (SQLException e) {
e.printStackTrace();
}
DBUtil.close(null, pstat, conn);
}
/**
* 根据给定的Class对象和id查询相应的结果
*
* @param cla 给定的Class对象
* @param id 给定的id
* @return 返回查询到的相应的类的对象
*/
public static T select(Class cla, int id) {
// 设置SQL语句
String sql = "select * from "
+ cla.getName().substring(cla.getName().lastIndexOf(".") + 1)
+ " where id = ";
// 获取当前对象所属类中的方法
Method[] methods = cla.getDeclaredMethods();
Connection conn = null;
PreparedStatement pstat = null;
ResultSet rs = null;
T t = null;
try {
conn = DBUtil.getConnection();
pstat = conn.prepareStatement(sql);
pstat.setInt(1, id);
// 获取查询结果
rs = pstat.executeQuery();
// 获取查询结果集中的各个列的属性
ResultSetMetaData rsmd = pstat.getMetaData();
// 获取查询结果集中列的个数
int columnNum = rsmd.getColumnCount();
// 定义字符串数组存放结果集中的列名
String[] columnNames = new String[columnNum];
// 获取结果集中各列的列名并存放到数组中
for (int i = 0; i < columnNum; i++) {
columnNames[i] = rsmd.getColumnName(i + 1);
}
if (rs.next()) {
t = cla.newInstance();
for (String columnName : columnNames) {
// 获取结果集中各列对应的set方法名
String cName = setMethodName(columnName);
// 根据方法名获取方法
for (int i = 0; i < methods.length; i++) {
if (cName.equals(methods[i].getName())) {
methods[i].invoke(t, rs.getObject(columnName));
break;
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
DBUtil.close(rs, pstat, conn);
}
return t;
}
/**
* 根据给定的对象和id更新数据
*
* @param t 给定的对象
* @param id 给定的id
*/
public static void update(T t, int id) {
// 获取对象t的class对象
@SuppressWarnings("unchecked")
Class cla = (Class) t.getClass();
// 获取t对象中的所有字段
Field[] fields = cla.getDeclaredFields();
// 声明列表用于存放t对象中的字段名(ID除外)
List keys = new ArrayList();
// 声明列表用于存放t对象中的字段值(ID除外)
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语句
String table = t.getClass().getName()
.substring(t.getClass().getName().lastIndexOf(".") + 1);
StringBuffer sql = new StringBuffer("update " + table + " set ");
for (int i = 0; i < keys.size() - 1; i++) {
sql.append(keys.get(i) + " = ,");
}
sql.append(keys.get(keys.size() - 1) + " = where id = ");


// 连接数据库
Connection conn = null;
PreparedStatement pstat = null;
try {
conn = DBUtil.getConnection();
pstat = conn.prepareStatement(sql.toString());
// 为要执行的SQL语句配置参数
for (int i = 0; i < values.size(); i++) {
pstat.setObject(i + 1, values.get(i));
}
pstat.setInt(values.size() + 1, id);
pstat.execute();
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(null, pstat, conn);
}
}
/**
* 根据给定的对象、条件和数据更新数据
*
* @param t 给定的对象
* @param where 给定的条件
* @param value 给定的值
*/
public static void update(T t, String where
首页 上一页 1 2 3 4 5 下一页 尾页 2/5/5
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇[Mongo]按时间分组统计(时间格式.. 下一篇postgresql数据库配置csv格式日志..

评论

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

·Redis on AWS:Elast (2025-12-27 04:19:30)
·在 Spring Boot 项目 (2025-12-27 04:19:27)
·使用华为开发者空间 (2025-12-27 04:19:24)
·Getting Started wit (2025-12-27 03:49:24)
·Ubuntu 上最好用的中 (2025-12-27 03:49:20)