All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jarbframework.utils.JdbcUtils Maven / Gradle / Ivy

There is a newer version: 2.4.3
Show newest version
package org.jarbframework.utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import javax.sql.DataSource;

/**
 * Java Database Connectivity (JDBC) utility class.
 * @author Jeroen van Schagen
 * @since 08-05-2011
 */
public final class JdbcUtils {

    /** Utility class, do not instantiate. */
    private JdbcUtils() {
    }
    
    public static  T doWithConnection(String driverClassName, String url, String userName, String password, JdbcConnectionCallback callback) {
        Connection connection = null;
        try {
            connection = createConnection(driverClassName, url, userName, password);
            return callback.doWork(connection);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            closeQuietly(connection);   
        }
    }

    private static Connection createConnection(String driverClassName, String url, String userName, String password) throws SQLException {
        Classes.forName(driverClassName); // Register appropriate JDBC driver
        return DriverManager.getConnection(url, userName, password);
    }

    /**
     * Open a data source connection and provide callback functionality on that connection.
     * After the callback has been invoked, the newly created connection will be closed.
     * 
     * @param  type of object returned by callback
     * @param dataSource the data source that we should use
     * @param callback the callback functionality being invoked
     * @return result of the callback, if any
     */
    public static  T doWithConnection(DataSource dataSource, JdbcConnectionCallback callback) {
        Connection connection = null;
        try {
            connection = dataSource.getConnection();
            return callback.doWork(connection);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            closeQuietly(connection);
        }
    }

    /**
     * Close the connection, whenever it isn't {@code null}, and wrap any SQL exceptions into a runtime exception.
     * 
     * @param connection our connection to close
     */
    public static void closeQuietly(Connection connection) {
        try {
            if (connection != null) {
                connection.close();
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * Commits the connection whenver auto commit has been disabled.
     * 
     * @param connection our connection to commit
     */
    public static void commitSafely(Connection connection) {
        try {
            if (! connection.getAutoCommit()) {
                connection.commit();
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy