设为首页 加入收藏

TOP

工具类之数据库工具类:DBUtil(采用反射机制)(三)
2014-11-24 08:16:23 来源: 作者: 【 】 浏览:15
Tags:工具 数据库 DBUtil 采用 反射 机制
, Object[] value) {
// 获取对象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);
}
}
}
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) + " = ");
if (where != null && where.length() > 0) {
sql.append(where);
}
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));
}
for (int i = 0, j = values.size(); i < value.length; i++, j++) {
pstat.setObject(j + 1, value[i]);
}
pstat.execute();
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(null, pstat, conn);
}
}


/**
* 查询所有结果
*
* @param cla 给定的Class对象
* @return 返回所有的结果
*/
public static List queryAll(Class cla) {
// 设置SQL语句
StringBuffer sql = new StringBuffer("select * from "+cla.getName().substring(cla.getName().lastIndexOf(".") + 1));
// 获取cla对象所属类的方法
Method[] methods = cla.getDeclaredMethods();
// 创建列表用于保存查询的结果集
List listResult = new ArrayList();
// 声明对象t用于遍历结果集
T t = null;
// 连接数据库
Connection conn = null;
PreparedStatement pstat = null;
ResultSet rs = null;
try {
conn = DBUtil.getConnection();
pstat = conn.prepareStatement(sql.toString());
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);
}
// 遍历结果集
while (rs.next()) {
try {
t = cla.newInstance();
} catch (InstantiationException e1) {
e1.printStackTrace();
} catch (IllegalAccessException e1) {
e1.printStackTrace();
}
for (String columnName : columnNames) {
// 根据字段名获取相应的set方法名
String methodName = setMethodName(columnName);
for (int i = 0; i < methods.length; i++) {
// 方法名在方法数组中找出相应的set方法
if (methodName.equals(methods[i].getName())) {
try {
// 执行相应的set方法,为对象t设置属性值
methods[i].invoke(t, rs.getObject(columnName));
break;
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}
}
// 将遍历出的对象添加到指定是列表中
listResult.add(t);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 关闭数据库连接
DBUtil.close(rs, pstat, conn);
}
// 返回结果列表
return listResult;
}
/**
* 根据给定的条件查询一条数据
* @param cla 给出的类的Class对象
* @param where 给出的查询条件
首页 上一页 1 2 3 4 5 下一页 尾页 3/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)