001.java将MySQL的ResultSet结果集遍历到List中
public List<T> findAll(String tableName) throws SQLException { String sb = "select * from " + tableName; //查询语句 Connection conn = mysqlConectComponent.getConnection();//JDBC连接方法,可以自己定义 PreparedStatement state = conn.prepareStatement(sb);//通过PreparedStatement执行查询语句 ResultSet rs = state.executeQuery();//将数据写入到ResultSet中 ResultSetMetaData md = rs.getMetaData();//获取键名 int columnCount = md.getColumnCount();//获取行的数量 List list = new ArrayList<T>();// 定义一个list,用来存放数据 while (rs.next()) { Map rowData = new HashMap();//声明Map for (int i = 1; i <= columnCount; i++) { rowData.put(md.getColumnName(i), rs.getObject(i));//获取键名及值 } list.add(rowData);//将数据添加到list中 } rs.close(); close(conn, state); return list; }