Created
August 15, 2012 15:27
-
-
Save rrguntaka/3361017 to your computer and use it in GitHub Desktop.
JDBC Result set to Java HashMap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
try { | |
Class.forName(driver); | |
Connection con = DriverManager.getConnection(url + db, user, pass); | |
Statement stmt = con.createStatement(); | |
ResultSet rs = stmt.executeQuery(query); | |
ResultSetMetaData rsmd = rs.getMetaData(); | |
List<String> columns = new ArrayList<String>(rsmd.getColumnCount()); | |
for(int i = 1; i <= rsmd.getColumnCount(); i++){ | |
columns.add(rsmd.getColumnName(i)); | |
} | |
List<Map<String,String>> data = new ArrayList<Map<String,String>>(); | |
while(rs.next()){ | |
Map<String,String> row = new HashMap<String, String>(columns.size()); | |
for(String col : columns) { | |
row.put(col, rs.getString(col)); | |
} | |
data.add(row); | |
} | |
System.out.println(data); | |
rs.close(); | |
stmt.close(); | |
con.close(); | |
} catch (Exception e) { | |
System.out.println(e); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How do you read through the list of maps??