Java Utililty Methods SQL Execute

List of utility methods to do SQL Execute

Description

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

Method

voidexecute(Connection connection, String sql, boolean closeConn)
execute
Statement stmt = null;
ResultSet rs = null;
try {
    stmt = connection.createStatement();
    boolean hasResults = stmt.execute(sql);
    if (hasResults) {
        rs = stmt.getResultSet();
        ResultSetMetaData metadata = rs.getMetaData();
...
voidexecute(final String url, final String username, final String password, final String sql, final Consumer consumer)
execute
try (final Connection conn = DriverManager.getConnection(url, username, password);
        final Statement stmt = conn.createStatement();
        final ResultSet rs = stmt.executeQuery(sql);) {
    consumer.accept(rs);
voidexecute(List sql)
execute
Connection conn = null;
Statement statement = null;
try {
    Class.forName("com.mysql.jdbc.Driver");
    conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "gary", "12345");
    conn.setAutoCommit(false);
    statement = conn.createStatement();
    int count = 0;
...
ResultSetexecute(Statement statement, String sql)
execute
if (statement == null) {
    throw new Exception();
ResultSet resultSet = null;
try {
    resultSet = statement.executeQuery(sql);
} catch (SQLException e) {
    throw e;
...
voidexecute(String sql, Connection connection)
execute
Statement shutdown = null;
try {
    shutdown = connection.createStatement();
    shutdown.execute(sql);
} catch (SQLException sqle) {
    throw new RuntimeException(String.format("Failed to execute SQL [%s]", sql), sqle);
} finally {
    if (shutdown != null) {
...
voidexecuteCall(Connection connection, String command, String schemaName, String tableName, int paramLength)
Executes the given call (does not commit)
CallableStatement cs = connection.prepareCall(command);
cs.setString(1, schemaName);
cs.setString(2, tableName);
paramLength += 3;
for (int i = 3; i < paramLength; i++) {
    cs.setShort(i, (short) 1);
cs.execute();
...
voidexecuteCallable(Connection conn, String sql)
execute Callable
System.out.println("Query SQL: " + sql);
CallableStatement cStmt = null;
ResultSet rs = null;
try {
    cStmt = conn.prepareCall(sql);
    boolean hadResults = cStmt.execute();
    while (hadResults) {
        rs = cStmt.getResultSet();
...
booleanexecuteCreateDB(String className, String URL, String userName, String password, String dbName)
execute Create DB
Connection con = null;
Statement stat = null;
try {
    con = getConnection(className, URL, userName, password);
    stat = con.createStatement();
    int count = stat.executeUpdate("CREATE DATABASE IF NOT EXISTS " + dbName);
    return count > 0 ? true : false;
} finally {
...
voidexecuteDDL(final Connection connection, final String sql, final int... ignoreErrors)
Helper method for executing DDL (or technically: any statement), ignoring the specified list of error codes.
if (ignoreErrors != null) {
    Arrays.sort(ignoreErrors);
try (Statement stmt = connection.createStatement()) {
    stmt.execute(sql);
} catch (SQLException ex) {
    if (ignoreErrors == null || ignoreErrors.length == 0)
        throw ex;
...
voidexecuteDDL(String ddl)

Execute a DDL statement

Connection conn = getLocalConnection();
print(ddl);
try {
    PreparedStatement createStatement = conn.prepareStatement(ddl);
    createStatement.execute();
    createStatement.close();
} catch (SQLException t) {
    SQLException s = new SQLException("Could not execute DDL:\n" + ddl);
...