Created
June 28, 2013 20:37
-
-
Save arttuladhar/5887916 to your computer and use it in GitHub Desktop.
Converts resultSet to HashMap of Strings and Objects
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
| /** | |
| * <h2> Converts ResultSet to HashMap of List<String,Object></h2> | |
| * @param row | |
| * @param rs_SubItemType | |
| * @throws SQLException | |
| */ | |
| private static void getHashMap( List<Map<String, Object>> row, ResultSet rs_SubItemType) throws SQLException { | |
| ResultSetMetaData metaData = rs_SubItemType.getMetaData(); | |
| int colCount = metaData.getColumnCount(); | |
| while (rs_SubItemType.next()) { | |
| Map<String, Object> columns = new HashMap<String, Object>(); | |
| for (int i = 1; i <= colCount; i++) { | |
| columns.put(metaData.getColumnLabel(i), rs_SubItemType.getObject(i)); | |
| } | |
| row.add(columns); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For dynamic query results you can try something like:
public <T> List<T> findWithDynamicQuery(String query){ Connection conn = null; PreparedStatement statement = null; List<Object> mResults = new ArrayList<>(); try { conn = UtilConnection.getDBConnection().getConnection(); } catch (SQLException e) { e.printStackTrace(out); return (List<T>) mResults; } try { statement = conn.prepareStatement(query); ResultSet result = statement.executeQuery(); ResultSetMetaData metaData = result.getMetaData(); int column = metaData.getColumnCount(); while (result.next()) { HashMap<Object, Object> rows = new HashMap<>(); for(int i = 1; i <= column; i++){ rows.put(metaData.getColumnLabel(i), result.getObject(i)); } mResults.add(rows); } result.close(); return (List<T>) mResults; } catch (SQLException e) { e.printStackTrace(out); }finally{ if(statement != null){ try { statement.close(); } catch (SQLException e) { } } if(conn != null){ try { conn.close(); } catch (SQLException e) { } } } return (List<T>) mResults; }