Java Utililty Methods SQL Query

List of utility methods to do SQL Query

Description

The list of methods to do SQL Query are organized into topic(s).

Method

StringexecuteQuery(java.sql.Connection con, String select, Object pk)
execute Query
con.setAutoCommit(true);
java.sql.PreparedStatement ps = null;
java.sql.ResultSet rs = null;
try {
    ps = con.prepareStatement(select);
    ps.setObject(1, pk);
    rs = ps.executeQuery();
    Object obj;
...
ResultSetexecuteQuery(Statement state, String sql)
execute Query
try {
    ResultSet rs = state.executeQuery(sql);
    return rs;
} catch (SQLException ex) {
    throw ex;
voidExecuteQuery(String command)
Executes a SQL query without returning the result set.
try {
    mStatement.executeQuery(command);
} catch (SQLException e) {
    System.out.println(mSqlHelperPrefix + e);
ResultSetexecuteQuery(String query)
execute Query
ResultSet rs = null;
try {
    stmt = conn.createStatement();
    rs = stmt.executeQuery(query);
} catch (SQLException e) {
    System.err.println("Got an exception! ");
    e.printStackTrace();
    System.exit(0);
...
List>executeQuery(String sql, Connection conn, List param)
execute Query
List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();
PreparedStatement stmt = conn.prepareStatement(sql);
if (param != null) {
    for (int i = 0; i < param.size(); i++) {
        stmt.setObject(i + 1, param.get(i));
ResultSet rs = stmt.executeQuery();
...
ResultSetexecuteQuery(String sql, String[] parameters)
execute Query
try {
    ct = getConnection();
    ps = ct.prepareStatement(sql);
    if (parameters != null) {
        for (int i = 0; i < parameters.length; i++) {
            ps.setString(i + 1, parameters[i]);
    rs = ps.executeQuery();
} catch (Exception e) {
    e.printStackTrace();
    throw new RuntimeException(e.getMessage());
return rs;
String[]executeQuery2(java.sql.Connection con, String select, Object... pk)
execute Query
con.setAutoCommit(true);
java.sql.PreparedStatement ps = null;
java.sql.ResultSet rs = null;
try {
    ps = con.prepareStatement(select);
    if (pk != null) {
        for (int x = 0; x < pk.length; x++) {
            ps.setObject(x + 1, pk[x]);
...
ResultSetexecuteRetrievalByIDQuery(PreparedStatement theStatement, int theID)
execute Retrieval By ID Query
theStatement.setInt(1, theID);
return theStatement.executeQuery();
List>executeSafeQuery(Connection conn, String sql)
execute a query and return the result as a list of rows, each row is represented as column_name->column_value map.
List<Map<String, String>> rows = Lists.newArrayList();
Statement stmt = null;
ResultSet rs = null;
try {
    stmt = conn.createStatement();
    rs = stmt.executeQuery(sql);
    ResultSetMetaData md = rs.getMetaData();
    final int numCols = md.getColumnCount();
...
voidexecuteStatement(Connection connection, String query)
execute Statement
try {
    Statement statement = connection.createStatement();
    statement.executeUpdate(query);
    statement.close();
} catch (SQLException sqle) {
    System.err.println("Failed to execute: " + query);
    System.err.println(sqle.getMessage());