* @param value 给出的查询条件中的参数值
* @return 返回查询到的结果
*/
public static
// 组合SQL语句
StringBuffer sql = new StringBuffer("select * from "
+ cla.getName().substring(cla.getName().lastIndexOf(".") + 1)
+ " ");
if (where != null && where.length() > 0) {
sql.append(where);
}
// 获取Class对象cla对应的类中方法
Method[] methods = cla.getDeclaredMethods();
// 连接数据库
Connection conn = null;
PreparedStatement pstat = null;
ResultSet rs = null;
T t = null;
try {
conn = DBUtil.getConnection();
pstat = conn.prepareStatement(sql.toString());
// 设置SQL语句中的参数
for (int i = 0; i < value.length; i++) {
pstat.setObject(i + 1, value[i]);
}
// 获取结果集
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()) {
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();
}
}
}
}
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(rs, pstat, conn);
}
return t;
}
/**
*
* @param cla 给定的Class对象
* @param where 给定的查询条件
* @param value 给定的查询条件中的参数值
* @return 返回查询到的结果集
*/
public static
// 组合SQL语句
StringBuffer sql = new StringBuffer("select * from "
+ cla.getName().substring(cla.getName().lastIndexOf(".") + 1)
+ " ");
if (where != null && where.length() > 0) {
sql.append(where);
}
// 获取Class对象cla对应的类中方法
Method[] methods = cla.getDeclaredMethods();
// 连接数据库
Connection conn = null;
PreparedStatement pstat = null;
ResultSet rs = null;
List
T t = null;
try {
conn = DBUtil.getConnection();
pstat = conn.prepareStatement(sql.toString());
// 设置SQL语句中的参数
for (int i = 0; i < value.length; i++) {
pstat.setObject(i + 1, value[i]);
}
// 获取结果集
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 (SQLExcepti