Example usage for org.apache.commons.dbutils DbUtils close

List of usage examples for org.apache.commons.dbutils DbUtils close

Introduction

In this page you can find the example usage for org.apache.commons.dbutils DbUtils close.

Prototype

public static void close(Statement stmt) throws SQLException 

Source Link

Document

Close a Statement, avoid closing if null.

Usage

From source file:org.batoo.jpa.core.impl.jdbc.dbutils.AbstractQueryRunner.java

/**
 * Close a <code>ResultSet</code>. This implementation avoids closing if null and does <strong>not</strong> suppress any exceptions.
 * Subclasses can override to provide special handling like logging.
 * //from   w w w . j av a2s  .c o  m
 * @param rs
 *            ResultSet to close
 * @throws SQLException
 *             if a database access error occurs
 * @since DbUtils 1.1
 */
protected void close(ResultSet rs) throws SQLException {
    DbUtils.close(rs);
}

From source file:org.batoo.jpa.core.impl.jdbc.dbutils.AbstractQueryRunner.java

/**
 * Close a <code>Statement</code>. This implementation avoids closing if null and does <strong>not</strong> suppress any exceptions.
 * Subclasses can override to provide special handling like logging.
 * /*ww  w.  j a v a  2 s. c om*/
 * @param stmt
 *            Statement to close
 * @throws SQLException
 *             if a database access error occurs
 * @since DbUtils 1.1
 */
protected void close(Statement stmt) throws SQLException {
    DbUtils.close(stmt);
}

From source file:org.batoo.jpa.jdbc.dbutils.QueryRunner.java

/**
 * Calls query after checking the parameters to ensure nothing is null.
 * /*from www  .j a v a 2 s. co m*/
 * @param conn
 *            The connection to use for the query call.
 * @param closeConn
 *            True if the connection should be closed, false otherwise.
 * @param sql
 *            The SQL statement to execute.
 * @param params
 *            An array of query replacement parameters. Each row in this array is one set of batch replacement values.
 * @return The results of the query.
 * @throws SQLException
 *             If there are database or parameter errors.
 */
private <T> T query(Connection conn, boolean closeConn, String sql, ResultSetHandler<T> rsh, Object... params)
        throws SQLException {
    PreparedStatement statement = null;
    ResultSet resultSet = null;

    try {
        statement = conn.prepareStatement(sql);
        if (params != null) {
            this.fillStatement(statement, params);
        }

        resultSet = statement.executeQuery();

        return rsh.handle(resultSet);
    } catch (final SQLException e) {
        throw this.convertSqlException(e, sql, params);
    } finally {
        try {
            DbUtils.close(resultSet);
        } finally {
            DbUtils.close(statement);

            if (closeConn) {
                DbUtils.close(conn);
            }
        }
    }
}

From source file:org.batoo.jpa.jdbc.dbutils.QueryRunner.java

/**
 * Calls update after checking the parameters to ensure nothing is null.
 * //ww w .jav  a2s  .  c  om
 * @param connection
 *            The connection to use for the update call.
 * @param closeConn
 *            True if the connection should be closed, false otherwise.
 * @param sql
 *            The SQL statement to execute.
 * @param params
 *            An array of update replacement parameters. Each row in this array is one set of update replacement values.
 * @return The number of rows updated.
 * @throws SQLException
 *             If there are database or parameter errors.
 */
private int update(Connection connection, boolean closeConn, String sql, Object... params) throws SQLException {
    if (connection == null) {
        throw new SQLException("Null connection");
    }

    if (sql == null) {
        if (closeConn) {
            DbUtils.close(connection);
        }
        throw new SQLException("Null SQL statement");
    }

    PreparedStatement statement = null;
    try {
        statement = connection.prepareStatement(sql);
        if (params != null) {
            this.fillStatement(statement, params);
        }

        return statement.executeUpdate();
    } catch (final SQLException e) {
        throw this.convertSqlException(e, sql, params);
    } finally {
        DbUtils.close(statement);

        if (closeConn) {
            DbUtils.close(connection);
        }
    }
}

From source file:org.culturegraph.mf.sql.util.JdbcUtil.java

public static ResultSet getTableMetadata(Connection jdbcConnection, String tableNamePattern, String schema,
        String catalog, boolean isQuoted) {
    ResultSet rs = null;//from w ww.  j ava 2 s . co  m
    try {
        DatabaseMetaData meta = jdbcConnection.getMetaData();
        if ((isQuoted && meta.storesMixedCaseQuotedIdentifiers())) {
            rs = meta.getTables(catalog, schema, tableNamePattern, TYPES);
        } else if ((isQuoted && meta.storesUpperCaseQuotedIdentifiers())
                || (!isQuoted && meta.storesUpperCaseIdentifiers())) {
            rs = meta.getTables(StringHelper.toUpperCase(catalog), StringHelper.toUpperCase(schema),
                    StringHelper.toUpperCase(tableNamePattern), TYPES);
        } else if ((isQuoted && meta.storesLowerCaseQuotedIdentifiers())
                || (!isQuoted && meta.storesLowerCaseIdentifiers())) {
            rs = meta.getTables(StringHelper.toLowerCase(catalog), StringHelper.toLowerCase(schema),
                    StringHelper.toLowerCase(tableNamePattern), TYPES);
        } else {
            rs = meta.getTables(catalog, schema, tableNamePattern, TYPES);
        }
        return rs;
        // while (rs.next()) {
        // String tableName = rs.getString("TABLE_NAME");
        // System.out.println("table = " + tableName);
        // }

    } catch (SQLException sqlException) {
        sqlException.printStackTrace();
    } finally {
        try {
            DbUtils.close(rs);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    return null;
}

From source file:org.okinawaopenlabs.orientdb.client.ConnectionManagerJdbc.java

/**
 * close database/*from w  w  w .j  ava  2  s. c o m*/
 *
 * @param database
 */
synchronized public void close(Connection conn) throws SQLException {
    if (conn != null && !conn.isClosed()) {
        DbUtils.close(conn);
    }
}

From source file:org.okinawaopenlabs.orientdb.client.ConnectionManagerJdbc.java

synchronized public void close(ResultSet rs) throws SQLException {
    if (rs != null && !rs.isClosed()) {
        DbUtils.close(rs);
    }//from www  .j  av a  2 s.c o  m
}

From source file:org.openlogics.gears.jdbc.DataStore.java

/**
 * @param preparedSt/*from w  w w . j  av a 2s  .  c o  m*/
 * @param handler
 * @param <T>
 * @return
 * @throws SQLException
 * @deprecated Used in an older version of teh CoreJavaBeans, but maybe useful yet, unitil find more features
 */
@Deprecated
protected <T> T select(PreparedStatement preparedSt, ResultSetHandler<? extends T> handler)
        throws SQLException {
    logger.debug("Attempting to execute a preparedStatement QUERY: " + preparedSt + ", mapped to ");
    ResultSet rs = null;
    try {
        rs = preparedSt.executeQuery();
        return handler.handle(rs);
    } finally {
        if (rs != null)
            rs.close();
        DbUtils.close(preparedSt);
        if (isAutoClose()) {
            closeDBConn();
        }
    }
}

From source file:org.openlogics.gears.jdbc.DataStore.java

/**
 * This method is always called after any transaction is executed.
 *
 * @throws SQLException//from  w  w  w  .  j a va2s .c  om
 */
public void closeConnection() throws SQLException {
    DbUtils.close(connection);
    this.connection = null;
}

From source file:org.silverpeas.dbbuilder.DBBuilderItem.java

private String extractVersionFromDatabase() throws SQLException {
    Connection connection = ConnectionFactory.getConnection();
    PreparedStatement pstmt = null;
    ResultSet rs = null;/*from ww  w  . ja v  a  2s.co m*/
    String version = NOTINSTALLED;
    try {
        pstmt = connection.prepareStatement("SELECT SR_VERSION FROM SR_PACKAGES where SR_PACKAGE = ?");
        pstmt.setString(1, module);
        rs = pstmt.executeQuery();
        if (rs.next()) {
            version = rs.getString("SR_VERSION");
        }
    } catch (SQLException sqlex) {
    } finally {
        DbUtils.close(rs);
        DbUtils.close(pstmt);
        DbUtils.close(connection);
    }
    return version;
}