1、将ResultSet集合转换成List类型
/*
* 将ResultSet转换成List封装
* */
private static List convertList(ResultSet rs) throws SQLException {
List list = new ArrayList();
ResultSetMetaData md = rs.getMetaData();
int columnCount = md.getColumnCount(); //判断一下当前有几个返回的字段数目
while (rs.next()) {
Map rowData = new HashMap();
for (int i = 1; i <= columnCount; i++) {
rowData.put(md.getColumnName(i), rs.getObject(i));
}
list.add(rowData); [每一个记录都是Map类型]
}
return list;
}
遍历的话可以这样来做:
List resultSet = pagebean.getCurrPageRecords();
Map rowData = new HashMap();
for (Iterator i = resultSet.iterator(); i.hasNext();) {//遍历List对象
rowData = (Map)i.next();
out.print("
" + String.valueOf(rowData.get("id")) + " | " + rowData.get("title") + " |
");
}
2、JDBC实现获取插入之后的自增长ID的方法
preparedStatement = this.connection.prepareStatement("insert into a_tmp(cards,years,months,times) values(?,?,?,?)",PreparedStatement.RETURN_GENERATED_KEYS);
preparedStatement.clearParameters();
preparedStatement.setString(1, cards);
preparedStatement.setString(2, years);
preparedStatement.setString(3, months);
preparedStatement.setString(4, times);
preparedStatement.executeUpdate();
ResultSet rs = preparedStatement.getGeneratedKeys();
if(rs.next()){
count=rs.getInt(1);
}
return count;
|
阅读(601) | 评论(1) | 转发(0) |