这段代码是将结果集封装到Map里,字段为key,记录为value。然后再将每个Map添加到List里。
大家有什么建议和想法可以留言给我。
[java] view plain copy import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import java.util.HashMap; public class DBHelper { public static void main(String[] args) throws ClassNotFoundException, SQLException { Class.forName("oracle.jdbc.driver.OracleDriver"); String url = "jdbc:oracle:thin:@localhost:1521:orcl"; String user = "ssmy"; String password = "ssmy"; Connection conn = DriverManager.getConnection(url, user, password); Statement stmt = conn.createStatement(); /*//造数据 * for(char letter='a';letter<='z';letter++){ int id = letter-97; String * name = ""; String sex = (id&1)!=0?"男":"女"; String state = "Y"; * * String sql = * "insert into person (id,name, sex,state) values("+id+","+ * name+","+sex+","+state+")"; ps.execute(sql); } */ ResultSet rs = stmt.executeQuery("select t.* from SSMY_SYS_USER t"); ResultSetMetaData data = rs.getMetaData(); ArrayList<HashMap<String, String>> al = new ArrayList<HashMap<String, String>>(); while (rs.next()) { HashMap<String, String> map = new HashMap<String, String>(); for (int i = 1; i <= data.getColumnCount(); i++) {// 数据库里从 1 开始 String c = data.getColumnName(i); String v = rs.getString(c); System.out.println(c + ":" + v + "\t"); map.put(c, v); } System.out.println("======================"); al.add(map); } System.out.println(al); rs.close(); stmt.close(); conn.close(); } }