public static Map<String, String> queryToMap(String dataSourceName, String sql, Object[] params) { Connection conn = null; PreparedStatement stmt = null; ResultSet rs = null; try { conn = getConnection(dataSourceName); stmt = conn.prepareStatement(sql); if (null != params && params.length > 0) { setStatementParameters(stmt, params); } rs = stmt.executeQuery(); if (rs.next()) { Map<String, String> result = new HashMap<String, String>(); ResultSetMetaData rsmd = rs.getMetaData(); int columnCount = rsmd.getColumnCount(); for (int i = 1; i <= columnCount; ++i) { result.put(rsmd.getColumnName(i).toUpperCase(), rs.getString(i)); } return result; } } catch (SQLException sqlex) { logger.warning(sqlex.getMessage()); throw new RuntimeException(sqlex); } finally { closeDB(conn, stmt, rs); } return null; }
public static ResultSet executeQuery(String dataSourceName, String sql) throws SQLException { Connection conn = getConnection(dataSourceName); Statement stmt = null; ResultSet rs = null; try { stmt = conn.createStatement(); rs = stmt.executeQuery(sql); } catch (SQLException e) { throw e; } finally { closeDB(conn, stmt, null); } return rs; }
public static int executeUpdate(String dataSourceName, String sql) throws SQLException { int resultCount = 0; Connection conn = getConnection(dataSourceName); Statement stmt = null; try { stmt = conn.createStatement(); resultCount = stmt.executeUpdate(sql); } catch (SQLException e) { throw e; } finally { closeDB(conn, stmt, null); } return resultCount; }
public static List<Object[]> queryToObjArrList( String dataSourceName, String sql, Object[] params) { Connection conn = null; PreparedStatement stmt = null; ResultSet rs = null; try { conn = getConnection(dataSourceName); stmt = conn.prepareStatement(sql); if (null != params && params.length > 0) { setStatementParameters(stmt, params); } rs = stmt.executeQuery(); return getResultObjArrList(rs); } catch (SQLException sqlex) { logger.warning(sqlex.getMessage()); throw new RuntimeException(sqlex); } finally { closeDB(conn, stmt, rs); } }