Created
July 14, 2016 09:00
-
-
Save yigityus/a42c2318d0217a81aaa3d91721701c60 to your computer and use it in GitHub Desktop.
query db
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
| public List<Map<String, Object>> query(String sql) { | |
| Connection conn = null; | |
| PreparedStatement ps = null; | |
| ResultSet rs = null; | |
| List<Map<String, Object>> rows = new ArrayList<Map<String, Object>>(); | |
| try { | |
| conn = getConnection(); | |
| ps = conn.prepareStatement(sql); | |
| rs = ps.executeQuery(); | |
| ResultSetMetaData metaData = rs.getMetaData(); | |
| int columnCount = metaData.getColumnCount(); | |
| while (rs.next()) { | |
| Map<String, Object> columns = new LinkedHashMap<String, Object>(); | |
| for (int i = 1; i <= columnCount; i++) { | |
| columns.put(metaData.getColumnLabel(i), rs.getObject(i)); | |
| } | |
| rows.add(columns); | |
| } | |
| } catch (Exception e) { | |
| logger.error(e); | |
| } finally { | |
| closePreparedStatement(ps); | |
| closeConnection(conn); | |
| closeResultset(rs); | |
| } | |
| return rows; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment