/** * Process a query and returns only the first result of a result set as a String. To be used when * it is certain that only one String (single cell) will be returned from the DB * * @param conn The Connection object to which the DML should be sent * @param query The query statements to be processed * @return String with the result returned from the database * @throws SQLException */ public static String processQueryWithSingleResult(Connection conn, String query) throws SQLException { try (Statement sm = SQLUtil.executeQueryWithResults(conn, query); ResultSet rs = sm.getResultSet()) { rs.next(); String result = rs.getString(1); rs.getStatement().close(); return result; } }
/** * Utility method for processing DML with proper output * * @param out The output (PrintStream or Connection) object to which the DML should be sent * @param query The DML statements to be processed * @return the result of the statement */ public static AutoCloseable processQueryWithResults(Object out, String query) throws SQLException { if (out instanceof PrintStream) { // TODO: how to handle the PrintStream // case? PrintStream fout = (PrintStream) out; fout.println(query); return fout; } if (out instanceof Connection) { Connection conn = (Connection) out; return SQLUtil.executeQueryWithResults(conn, query); } return null; }