xin3721网络学院为广大学员,准备了丰富了教学视频。相关视频教程地址为:java教程 // 创建resultset
ResultSet rset = null;
// 创建collection
Collection collection = null;
try {
// 赋予实例
ptmt = connection.prepareStatement(sql);
rset = ptmt.executeQuery();
collection = get(rset, clazz);
} catch (SQLException e) {
System.err.println(e.getMessage());
} finally {
try {
// 关闭rs并释放资源
if (rset != null) {
rset.close();
rset = null;
}
// 关闭ps并释放资源
if (ptmt != null) {
ptmt.close();
ptmt = null;
}
} catch (SQLException e) {
System.err.println(e.getMessage());
}
}
return collection;
}
public Collection get(final ResultSet result, final Class clazz) {
// 创建collection
Collection collection = null;
try {
ResultSetMetaData rsmd = result.getMetaData();
// 获得数据列数
int cols = rsmd.getColumnCount();
// 创建等同数据列数的arraylist类型collection实例
collection = new ArrayList(cols);
// 遍历结果集
while (result.next()) {
// 创建对象
Object object = null;
try {
// 从class获得对象实体
object = clazz.newInstance();
} catch (Exception e) {
}
// 循环每条记录
for (int i = 1; i <= cols; i++) {
beanRegister(object, rsmd.getColumnName(i), result