Example usage for org.springframework.jdbc.support JdbcUtils closeStatement

List of usage examples for org.springframework.jdbc.support JdbcUtils closeStatement

Introduction

In this page you can find the example usage for org.springframework.jdbc.support JdbcUtils closeStatement.

Prototype

public static void closeStatement(@Nullable Statement stmt) 

Source Link

Document

Close the given JDBC Statement and ignore any thrown exception.

Usage

From source file:org.works.integration.storedproc.derby.DerbyStoredProcedures.java

public static void findCoffee(int coffeeId, String[] coffeeDescription) throws SQLException {
    Connection connection = null;
    PreparedStatement statement = null;

    try {/*  w ww  .  j  a  v a  2  s.  c om*/
        connection = DriverManager.getConnection("jdbc:default:connection");
        String sql = "SELECT * FROM COFFEE_BEVERAGES WHERE ID = ? ";
        statement = connection.prepareStatement(sql);
        statement.setLong(1, coffeeId);

        ResultSet resultset = statement.executeQuery();
        resultset.next();
        coffeeDescription[0] = resultset.getString("COFFEE_DESCRIPTION");

    } finally {
        JdbcUtils.closeStatement(statement);
        JdbcUtils.closeConnection(connection);
    }

}

From source file:net.mlw.vlh.adapter.jdbc.spring.util.SpringConnectionCreator.java

public void close(ResultSet result, PreparedStatement statement, Connection connection) {
    JdbcUtils.closeResultSet(result);/*from   w  w  w. j  av  a2  s  .c  o  m*/
    JdbcUtils.closeStatement(statement);
    // deprecated since spring 1.2
    DataSourceUtils.releaseConnection(connection, getDataSource());
}

From source file:org.dcache.chimera.DirectoryStreamImpl.java

DirectoryStreamImpl(FsInode dir, JdbcTemplate jdbc) {
    _jdbc = jdbc;//from w  w  w.jav a 2 s  . c o m

    Connection connection = null;
    PreparedStatement ps = null;
    ResultSet rs;
    try {
        connection = DataSourceUtils.getConnection(_jdbc.getDataSource());
        ps = connection.prepareStatement(QUERY);
        ps.setFetchSize(50);
        ps.setLong(1, dir.ino());
        ps.setLong(2, dir.ino());
        ps.setLong(3, dir.ino());
        rs = ps.executeQuery();
    } catch (SQLException ex) {
        JdbcUtils.closeStatement(ps);
        DataSourceUtils.releaseConnection(connection, _jdbc.getDataSource());
        throw _jdbc.getExceptionTranslator().translate("StatementExecution", QUERY, ex);
    }
    _connection = connection;
    _resultSet = rs;
    _statement = ps;
}

From source file:com.emc.ecs.sync.service.RowIterator.java

@Override
public void close() {
    JdbcUtils.closeResultSet(rs);
    JdbcUtils.closeStatement(st);
    JdbcUtils.closeConnection(con);
}

From source file:org.dcache.chimera.DirectoryStreamImpl.java

public void close() throws IOException {
    try {//from w w w . ja  va 2  s.  co  m
        JdbcUtils.closeResultSet(_resultSet);
        JdbcUtils.closeStatement(_statement);
        DataSourceUtils.releaseConnection(_connection, _jdbc.getDataSource());
    } catch (DataAccessException e) {
        throw new IOException(e.getMessage(), e);
    }
}

From source file:org.paxml.table.jdbc.JdbcTable.java

public void close(boolean closeConnection) {
    JdbcUtils.closeResultSet(rs);//from  w w w. ja  v  a 2s  . c  o  m
    rs = null;
    JdbcUtils.closeStatement(ps);
    ps = null;
    if (closeConnection) {
        JdbcUtils.closeConnection(con);
    }
    con = null;
}

From source file:net.firejack.platform.model.upgrader.operator.AbstractOperator.java

/**
 * Executor method which execute sql commands
 *
 * @param type - operator class// w  w w .j a v a2 s. c o  m
 */
public void execute(T type) {
    try {
        Connection connection = dataSource.getConnection();
        try {
            connection.setAutoCommit(true);
            PreparedStatement statement = null;
            try {
                String[] sqlCommands = sqlCommands(type);
                for (String sqlCommand : sqlCommands) {
                    logger.info("Execute sql: \n" + sqlCommand);
                    statement = connection.prepareStatement(sqlCommand);
                    statement.executeUpdate();
                }
            } finally {
                JdbcUtils.closeStatement(statement);
            }
        } finally {
            connection.setAutoCommit(false);
            JdbcUtils.closeConnection(connection);
        }
    } catch (SQLException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.thinkbiganalytics.server.KyloUpgradeDatabaseVersionChecker.java

/**
 * Query the Database for the Kylo Version
 *
 * @return the KyloVersion from the database, or null if not found or if an error occurs
 *///  w  ww  .jav  a  2s  .  co  m
public KyloVersion getDatabaseVersion() {
    KyloVersion version = null;
    Connection connection = null;
    Statement statement = null;
    ResultSet resultSet = null;

    try {
        String user = environmentProperties.getPropertyValueAsString("spring.datasource.username");
        String password = environmentProperties.getPropertyValueAsString("spring.datasource.password");
        password = encryptionService.isEncrypted(password) ? encryptionService.decrypt(password) : password;
        String uri = environmentProperties.getPropertyValueAsString("spring.datasource.url");
        String driverClassName = environmentProperties
                .getPropertyValueAsString("spring.datasource.driverClassName");
        boolean testOnBorrow = BooleanUtils
                .toBoolean(environmentProperties.getPropertyValueAsString("spring.datasource.testOnBorrow"));
        String validationQuery = environmentProperties.getPropertyValueAsString("spring.data.validationQuery");

        PoolingDataSourceService.DataSourceProperties dataSourceProperties = new PoolingDataSourceService.DataSourceProperties(
                user, password, uri, driverClassName, testOnBorrow, validationQuery);

        DataSource dataSource = PoolingDataSourceService.getDataSource(dataSourceProperties);

        connection = dataSource.getConnection();
        String query = "SELECT MAJOR_VERSION,MINOR_VERSION FROM kylo.KYLO_VERSION ";
        statement = connection.createStatement();
        ResultSet rs = statement.executeQuery(query);
        if (rs.next()) {
            String majorVersion = rs.getString("MAJOR_VERSION");
            String minorVersion = rs.getString("MINOR_VERSION");
            version = new KyloVersionUtil.Version(majorVersion, minorVersion);
        }

    } catch (SQLException e) {
        // this is ok.. If an error happens assume the upgrade is needed.  The method will return a null value if errors occur and the upgrade app will start.
    } finally {
        JdbcUtils.closeStatement(statement);
        JdbcUtils.closeResultSet(resultSet);
        JdbcUtils.closeConnection(connection);
    }
    return version;

}

From source file:com.thinkbiganalytics.server.upgrade.KyloUpgradeDatabaseVersionChecker.java

/**
 * Query the Database for the Kylo Version
 *
 * @return the KyloVersion from the database, or null if not found or if an error occurs
 *//*from w  w  w .  ja  v a2s  . c o  m*/
public KyloVersion getDatabaseVersion() {
    KyloVersion version = null;
    Connection connection = null;
    Statement statement = null;
    ResultSet resultSet = null;

    try {
        String user = environmentProperties.getPropertyValueAsString("spring.datasource.username");
        String password = environmentProperties.getPropertyValueAsString("spring.datasource.password");
        password = encryptionService.isEncrypted(password) ? encryptionService.decrypt(password) : password;
        String uri = environmentProperties.getPropertyValueAsString("spring.datasource.url");
        String driverClassName = environmentProperties
                .getPropertyValueAsString("spring.datasource.driverClassName");
        boolean testOnBorrow = BooleanUtils
                .toBoolean(environmentProperties.getPropertyValueAsString("spring.datasource.testOnBorrow"));
        String validationQuery = environmentProperties
                .getPropertyValueAsString("spring.datasource.validationQuery");

        PoolingDataSourceService.DataSourceProperties dataSourceProperties = new PoolingDataSourceService.DataSourceProperties(
                user, password, uri, driverClassName, testOnBorrow, validationQuery);

        DataSource dataSource = PoolingDataSourceService.getDataSource(dataSourceProperties);

        connection = dataSource.getConnection();
        String query = "SELECT * FROM KYLO_VERSION ORDER BY MAJOR_VERSION DESC, MINOR_VERSION DESC, POINT_VERSION DESC, TAG DESC";
        statement = connection.createStatement();
        ResultSet rs = statement.executeQuery(query);
        if (rs.next()) {
            String majorVersion = rs.getString("MAJOR_VERSION");
            String minorVersion = rs.getString("MINOR_VERSION");
            String pointVersion = rs.getString("POINT_VERSION");
            String tag = rs.getString("TAG");

            version = new KyloVersionUtil.Version(majorVersion, minorVersion, pointVersion, tag);
        }

    } catch (SQLException e) {
        // this is ok.. If an error happens assume the upgrade is needed.  The method will return a null value if errors occur and the upgrade app will start.
        e.printStackTrace();
    } finally {
        JdbcUtils.closeStatement(statement);
        JdbcUtils.closeResultSet(resultSet);
        JdbcUtils.closeConnection(connection);
    }
    return version;

}

From source file:me.j360.idgen.impl.SequenceIdGenServiceImpl.java

/**
 * Gets the next id as a Big Decimal. This method will only be called when
 * synchronized and when the data type is configured to be BigDecimal.
 * // w  w w .j ava 2s .  c o  m
 * @return the next id as a BigDecimal.
 * @throws IdCreationException
 */
protected BigDecimal getNextBigDecimalIdInner() {
    getLogger().debug("[IDGeneration Service] Requesting an Id using query: {}", query);
    try {
        // 2009.10.08 - without handling connection directly
        Connection conn = DataSourceUtils.getConnection(getDataSource());
        PreparedStatement stmt = null;
        ResultSet rs = null;
        try {
            stmt = conn.prepareStatement(query);
            rs = stmt.executeQuery();
            if (rs.next()) {
                return rs.getBigDecimal(1);
            } else {
                getLogger().error(
                        "[IDGeneration Service] Unable to allocate a block of Ids. Query for Id did not return a value.");
                throw new IdCreationException(
                        "[IDGeneration Service] Unable to allocate a block of Ids. Query for Id did not return a value.");
            }
        } finally {
            if (rs != null) {
                JdbcUtils.closeResultSet(rs);
            }
            if (stmt != null) {
                JdbcUtils.closeStatement(stmt);
            }
            // 2009.10.08 - without handling connection directly
            if (conn != null) {
                DataSourceUtils.releaseConnection(conn, getDataSource());
            }

        }
        // 2009.10.08 - without handling connection directly
    } catch (Exception ex) {
        if (ex instanceof IdCreationException)
            throw (IdCreationException) ex;
        getLogger().error(
                "[IDGeneration Service] We can't get a connection. So, unable to allocate a block of Ids.", ex);
        throw new IdCreationException(
                "[IDGeneration Service] We can't get a connection. So, unable to allocate a block of Ids.", ex);
    }
}