Example usage for java.sql Statement close

List of usage examples for java.sql Statement close

Introduction

In this page you can find the example usage for java.sql Statement close.

Prototype

void close() throws SQLException;

Source Link

Document

Releases this Statement object's database and JDBC resources immediately instead of waiting for this to happen when it is automatically closed.

Usage

From source file:com.thoughtworks.go.server.database.DatabaseFixture.java

public static int update(String query, H2Database h2Database) {
    BasicDataSource source = h2Database.createDataSource();
    Connection con = null;/*from w w  w  . ja  v a 2  s . c o m*/
    Statement stmt = null;
    try {
        con = source.getConnection();
        stmt = con.createStatement();
        return stmt.executeUpdate(query);
    } catch (SQLException e) {
        throw new RuntimeException(e);
    } finally {
        try {
            assert stmt != null;
            stmt.close();
            con.close();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}

From source file:com.cloudera.sqoop.manager.OracleUtils.java

/**
 * Drop a table if it exists./* w w  w .  j a  va2s .c  o  m*/
 */
public static void dropTable(String tableName, ConnManager manager) throws SQLException {
    Connection connection = null;
    Statement st = null;

    try {
        connection = manager.getConnection();
        connection.setAutoCommit(false);
        st = connection.createStatement();

        // create the database table and populate it with data.
        st.executeUpdate(getDropTableStatement(tableName));

        connection.commit();
    } finally {
        try {
            if (null != st) {
                st.close();
            }
        } catch (SQLException sqlE) {
            LOG.warn("Got SQLException when closing connection: " + sqlE);
        }
    }
}

From source file:gridool.util.jdbc.JDBCUtils.java

/**
 * Close a <code>Statement</code>, avoid closing if null.
 *///w w  w .j av  a  2 s.c  om
public static void close(Statement stmt) throws SQLException {
    if (stmt != null) {
        stmt.close();
    }
}

From source file:com.p6spy.engine.spy.P6TestUtil.java

public static int queryForInt(Connection con, String sql) throws SQLException {
    Statement stmt = null;
    ResultSet rs = null;/*  ww  w.  j  a  v a  2  s . c om*/
    try {
        stmt = con.createStatement();
        rs = stmt.executeQuery(sql);
        rs.next();
        return rs.getInt(1);
    } finally {
        if (rs != null)
            try {
                rs.close();
            } catch (Exception e) {
            }
        if (stmt != null)
            try {
                stmt.close();
            } catch (Exception e) {
            }
    }
}

From source file:com.wso2telco.core.dbutils.DbUtils.java

/**
 * Close statement.//w w  w  . j  a  v a2  s . co  m
 *
 * @param statement
 *            the statement
 */
private static void closeStatement(Statement statement) {
    if (statement != null) {
        try {
            statement.close();
        } catch (SQLException e) {
            log.warn("Database error. Could not close Statement. Continuing with" + " others. - "
                    + e.getMessage(), e);
        }
    }
}

From source file:com.tacitknowledge.util.migration.jdbc.util.SqlUtil.java

/**
 * Ensures the given connection, statement, and result are properly closed.
 *
 * @param conn the connection to close; may be <code>null</code>
 * @param stmt the statement to close; may be <code>null</code>
 * @param rs   the result set to close; may be <code>null</code>
 *//*from w w w. ja  va 2  s.  c  o m*/
public static void close(Connection conn, Statement stmt, ResultSet rs) {
    if (rs != null) {
        try {
            log.debug("Closing ResultSet: " + rs.toString());
            rs.close();
        } catch (SQLException e) {
            log.error("Error closing ResultSet", e);
        }
    }

    if (stmt != null) {
        try {
            log.debug("Closing Statement: " + stmt.toString());
            stmt.close();
        } catch (SQLException e) {
            log.error("Error closing Statement", e);
        }
    }

    if (conn != null) {
        try {
            if (!conn.isClosed()) {
                log.debug("Closing Connection " + conn.toString());
                conn.close();
            } else {
                log.debug("Connection (" + conn.toString() + ") already closed.");
            }
        } catch (SQLException e) {
            log.error("Error closing Connection", e);
        }
    }
}

From source file:biz.source_code.miniConnectionPoolManager.TestMiniConnectionPoolManager.java

private static void execSql(Connection conn, String sql) throws SQLException {
        Statement st = null;
        try {/* ww w  .j  av  a2  s .  co m*/
            st = conn.createStatement();
            st.executeUpdate(sql);
        } finally {
            if (st != null)
                st.close();
        }
    }

From source file:com.aurel.track.admin.server.status.ServerStatusBL.java

/**
 * /* www  . j  a va  2 s . c  o  m*/
 */
public static Double loadPing() {
    //
    //
    //
    //
    Connection connection = null;
    try {
        connection = InitDatabase.getConnection();
        Date start = new Date();
        for (int i = 0; i < 10; ++i) {
            Statement stmt = connection.createStatement();
            ResultSet rs = stmt.executeQuery("SELECT OBJECTID FROM TSITE");
            while (rs.next()) {
                rs.getInt(1);
            }
            rs.close();
            stmt.close();
        }
        Date stop = new Date();
        return new Double((stop.getTime() - start.getTime())) / 10.0;
    } catch (Exception e) {
        LOGGER.error("Can't ping: " + e.getMessage());
    } finally {
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException e) {
                LOGGER.warn("Closing the connection failed with " + e.getMessage());
                LOGGER.debug(ExceptionUtils.getStackTrace(e));
            }
        }
    }
    return -999.99;
}

From source file:com.autentia.tnt.bill.migration.BillToBillPaymentMigration.java

private static void liberaConexion(Connection con, Statement stmt, ResultSet rs) {
    if (rs != null) {
        try {/* w ww.ja  v a  2  s  .  c o m*/
            rs.close();
        } catch (SQLException sqlex) {
            log.error("Error al liberar el ResultSet", sqlex);
        }
    }

    if (stmt != null) {
        try {
            stmt.close();
        } catch (SQLException sqlex) {
            log.error("Error al liberar el Statement", sqlex);
        }
    }

    if (con != null) {
        try {
            con.close();
        } catch (SQLException sqlex) {
            log.error("Error al liberar la conexin", sqlex);
        }
    }
}

From source file:net.hydromatic.optiq.impl.jdbc.JdbcSchema.java

private static void close(Connection connection, Statement statement, ResultSet resultSet) {
    if (resultSet != null) {
        try {//w ww  . jav  a2 s.  c  om
            resultSet.close();
        } catch (SQLException e) {
            // ignore
        }
    }
    if (statement != null) {
        try {
            statement.close();
        } catch (SQLException e) {
            // ignore
        }
    }
    if (connection != null) {
        try {
            connection.close();
        } catch (SQLException e) {
            // ignore
        }
    }
}