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

ResultSetexecuteSQL(Connection conn, String sql)
Creates a SQL Statement and execute the statement using JDBC executeQuery method.
ResultSet result = null;
Statement statement = null;
try {
    statement = conn.createStatement();
    result = statement.executeQuery(sql);
} catch (SQLException ex) {
    if (statement != null)
        statement.close();
...
ResultSetexecuteSql(Connection conn, String sql)
execute Sql
Statement stmt = conn.createStatement();
stmt.setQueryTimeout(20);
ResultSet rs = stmt.executeQuery(sql);
return rs;
voidexecuteSQL(Connection connection, String file)
Executes the SQL statements from file.
String[] values = readFile(file).split(";");
Statement statement = connection.createStatement();
try {
    for (int i = 0; i < values.length; i++) {
        String sql = values[i].trim();
        if ((sql.length() != 0) && (!sql.startsWith("#"))) {
            statement.executeUpdate(sql);
} finally {
    statement.close();
voidexecuteSQL(Connection session, String sql, Object... params)
execute SQL
try (PreparedStatement dbStat = session.prepareStatement(sql)) {
    if (params != null) {
        for (int i = 0; i < params.length; i++) {
            dbStat.setObject(i + 1, params[i]);
    dbStat.execute();
voidexecuteSqlAutoCommit(Connection connection, String sql)
Execute the user-specified init SQL.
if (sql != null) {
    connection.setAutoCommit(true);
    Statement statement = connection.createStatement();
    try {
        statement.execute(sql);
    } finally {
        statement.close();
voidexecuteSQLCommand(Connection conn, String sql, byte[] blob)
execute SQL Command
PreparedStatement st = null;
conn.setAutoCommit(true);
try {
    st = conn.prepareStatement(sql);
    st.setBytes(1, blob);
    st.executeUpdate();
} finally {
    if (st != null)
...
ResultSetexecuteSQLCommandWithResult(Connection conn, String sql)
execute SQL Command With Result
conn.setAutoCommit(true);
Statement st = conn.createStatement();
return st.executeQuery(sql);
voidexecuteSqlFile(Connection connection, String sqlPath)

Executes the sql scripts in the given sql file.

String[] sqlStatements = loadSqlFile(sqlPath);
Statement stmt = null;
try {
    stmt = connection.createStatement();
    for (int i = 0; i < sqlStatements.length; i++) {
        if (sqlStatements[i].trim().length() != 0) {
            stmt.executeUpdate(sqlStatements[i]);
} finally {
    closeStatement(stmt);
voidexecuteSqlFile(String filename, Connection connection)
Execute the sql statements in a file.
Reader file = new FileReader(filename);
char[] buffer = new char[1024];
int retLength = 0;
StringBuffer content = new StringBuffer();
while ((retLength = file.read(buffer)) >= 0) {
    content.append(buffer, 0, retLength);
file.close();
...
voidexecuteSqlFromSql(Connection connection, String sql, String name)
execute Sql From Sql
PreparedStatement statement = connection.prepareStatement(sql);
statement.setString(1, name);
statement.execute();
connection.commit();