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:TestDebug_MySQL.java

public static int countRows(Connection conn, String tableName) throws SQLException {
    Statement stmt = null;
    ResultSet rs = null;/*from w w w . ja v  a2s. com*/
    int rowCount = -1;
    try {
        stmt = conn.createStatement();
        rs = stmt.executeQuery("SELECT COUNT(*) FROM " + tableName);
        // get the number of rows from the result set
        rs.next();
        rowCount = rs.getInt(1);
    } finally {
        rs.close();
        stmt.close();
    }

    return rowCount;
}

From source file:CountRows_MySQL.java

public static int countRows(Connection conn, String tableName) throws SQLException {
    // select the number of rows in the table
    Statement stmt = null;
    ResultSet rs = null;//from w w  w.j ava2 s .c  om
    int rowCount = -1;
    try {
        stmt = conn.createStatement();
        rs = stmt.executeQuery("SELECT COUNT(*) FROM " + tableName);
        // get the number of rows from the result set
        rs.next();
        rowCount = rs.getInt(1);
    } finally {
        rs.close();
        stmt.close();
    }
    return rowCount;
}

From source file:CountRows_Oracle.java

public static int countRows(Connection conn, String tableName) throws SQLException {
    // select the number of rows in the table
    Statement stmt = null;
    ResultSet rs = null;//from ww w .j  ava  2  s . c o  m
    int rowCount = -1;
    try {
        stmt = conn.createStatement();
        rs = stmt.executeQuery("SELECT COUNT(*) FROM " + tableName);
        // get the number of rows from the result set
        rs.next();
        rowCount = rs.getInt(1);
    } finally {
        rs.close();
        stmt.close();
    }

    return rowCount;
}

From source file:com.amazon.services.awsrum.utils.RedshiftUtils.java

/**
 * Helper method to determine if a table exists in the Redshift database
 * //from   ww w .j ava2 s .  c  om
 * @param loginProperties
 *            A properties file containing the authentication credentials for the database
 * @param redshiftURL
 *            The JDBC URL of the Redshift database
 * @param tableName
 *            The table to check existence of
 * @return true if connection to the database is successful and the table exists, otherwise
 *         false
 */
public static boolean tableExists(Properties loginProperties, String redshiftURL, String tableName) {
    Connection conn = null;
    try {
        conn = DriverManager.getConnection(redshiftURL, loginProperties);

        Statement stmt = conn.createStatement();
        stmt.executeQuery("SELECT * FROM " + tableName + " LIMIT 1;");
        stmt.close();
        conn.close();
        return true;
    } catch (SQLException e) {
        LOG.error(e);
        try {
            conn.close();
        } catch (Exception e1) {
        }
        return false;
    }
}

From source file:com.forsrc.utils.DbUtils.java

/**
 * Close.// ww w  .  j a v a2 s.com
 *
 * @param stmt the stmt
 * @param rs   the rs
 */
public static void close(Statement stmt, ResultSet rs) {

    try {
        if (null != rs) {
            rs.close();
        }
    } catch (SQLException e) {
        //LogUtils.LOGGER.error(e.getMessage(), e);
    }

    try {
        if (null != stmt) {
            stmt.close();
        }
    } catch (SQLException e) {
        //LogUtils.LOGGER.error(e.getMessage(), e);
    }

}

From source file:gridool.db.DBInsertOperation.java

private static void truncateTable(@Nonnull final Connection conn, @Nonnull final String table)
        throws SQLException {
    final Statement st = conn.createStatement();
    try {//w  ww. j a  v a 2s .  com
        st.executeUpdate("DELETE FROM " + table);
        conn.commit();
    } finally {
        st.close();
    }
}

From source file:ProxyAuthTest.java

private static void exStatement(String query) throws Exception {
    Statement stmt = con.createStatement();
    stmt.execute(query);/* www .j av  a 2 s.  co m*/
    if (!noClose) {
        stmt.close();
    }
}

From source file:it.geosolutions.geobatch.imagemosaic.ImageMosaicUpdateTest.java

@AfterClass
public static void dispose() throws Exception {
    if (GeoServerTests.skipTest()) {
        return;//from  ww  w.  j ava  2s  . c  om
    }

    // clean up GS
    GeoServerRESTPublisher publisher = new GeoServerRESTPublisher(GeoServerTests.URL, GeoServerTests.UID,
            GeoServerTests.PWD);
    publisher.removeCoverageStore(WORKSPACE, STORE, true);

    // remove created dir
    FileUtils.deleteDirectory(BASE_DIR);

    // delete table
    if (PostGisDataStoreTests.existsPostgis()) {

        // props
        final Properties props = ImageMosaicProperties
                .getPropertyFile(PostGisDataStoreTests.getDatastoreProperties());

        // delete
        Class.forName("org.postgresql.Driver");
        Connection connection = DriverManager.getConnection(
                "jdbc:postgresql://" + props.getProperty("host") + ":" + props.getProperty("port") + "/"
                        + props.getProperty("database"),
                props.getProperty("user"), props.getProperty("passwd"));
        Statement st = connection.createStatement();
        st.execute("DROP TABLE IF EXISTS " + STORE);
        st.close();
        connection.close();
    }

}

From source file:com.uber.hoodie.hive.client.HoodieHiveClient.java

private static void closeQuietly(ResultSet resultSet, Statement stmt) {
    try {/*from w w w .jav a2s.c o m*/
        if (stmt != null)
            stmt.close();
        if (resultSet != null)
            resultSet.close();
    } catch (SQLException e) {
        LOG.error("Could not close the resultset opened ", e);
    }
}

From source file:de.mendelson.comm.as2.database.DBDriverManager.java

/**Creates a new locale database
 * @return true if it was created successfully
 * @param DB_TYPE of the database that should be created, as defined in this class
 *///w w w.  j  a  v a2s  .  co  m
public static boolean createDatabase(final int DB_TYPE) {
    // It will be create automatically if it does not yet exist
    // the given files in the URL is the name of the database
    // "sa" is the user name and "" is the (empty) password
    StringBuilder message = new StringBuilder("Creating database ");
    String createResource = null;
    int dbVersion = 0;
    if (DB_TYPE == DBDriverManager.DB_CONFIG) {
        message.append("CONFIG");
        dbVersion = AS2ServerVersion.getRequiredDBVersionConfig();
        createResource = SQLScriptExecutor.SCRIPT_RESOURCE_CONFIG;
    } else if (DB_TYPE == DBDriverManager.DB_RUNTIME) {
        message.append("RUNTIME");
        dbVersion = AS2ServerVersion.getRequiredDBVersionRuntime();
        createResource = SQLScriptExecutor.SCRIPT_RESOURCE_RUNTIME;
    } else if (DB_TYPE != DBDriverManager.DB_DEPRICATED) {
        throw new RuntimeException("Unknown DB type requested in DBDriverManager.");
    }
    Logger.getLogger(AS2Server.SERVER_LOGGER_NAME).info(message.toString());
    Connection connection = null;
    try {
        connection = DriverManager.getConnection("jdbc:hsqldb:" + DBDriverManager.getDBName(DB_TYPE), "sa", "");
        Statement statement = connection.createStatement();
        statement.execute("ALTER USER " + DB_USER_NAME.toUpperCase() + " SET PASSWORD '" + DB_PASSWORD + "'");
        statement.close();
        SQLScriptExecutor executor = new SQLScriptExecutor();
        executor.create(connection, createResource, dbVersion);
        Logger.getLogger(AS2Server.SERVER_LOGGER_NAME).info("Database created.");
    } catch (Exception e) {
        throw new RuntimeException("Database not created: " + e.getMessage());
    } finally {
        if (connection != null) {
            try {
                connection.close();
            } catch (Exception e) {
                Logger.getLogger(AS2Server.SERVER_LOGGER_NAME)
                        .severe("Database creation failed: " + e.getMessage());
                e.printStackTrace();
            }
        }
    }
    return (true);
}