java(JDBC连接数据库)[对Statement进行封装](二)

2014-11-24 09:07:00 · 作者: · 浏览: 4
etMessage());
}
try {
columnCount = rsmd.getColumnCount();
} catch (SQLException e1) {
System.out.println(e1.getMessage());
}

List list = new ArrayList();

try {
while(rs.next()) {
Map map = new HashMap();
for(int i = 1; i <= columnCount; i++) {
map.put(rsmd.getColumnLabel(i), rs.getObject(i));
}
list.add(map);
}
} catch (SQLException e) {
System.out.println(e.getMessage());
} finally {
closeAll();
}

return list;
}


/**
* 释放所有资源
*/
private void closeAll() {
// 释放结果集连接
if (resultSet != null) {
try {
resultSet.close();
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}

// 释放声明连接
if (statement != null) {
try {
statement.close();
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}

// 释放数据库连接
if (connnection != null) {
try {
connnection.close();
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}
}
}

作者:haifengzhilian