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.flexive.ejb.beans.structure.FxStructureUtils.java

/**
 * Remove all groups that are no longer referenced
 *
 * @param con a valid connection/* ww w. j a v  a 2s.  co  m*/
 * @throws java.sql.SQLException on errors
 */
public static void removeOrphanedGroups(Connection con) throws SQLException {
    Statement stmt = null;
    try {
        stmt = con.createStatement();
        stmt.executeUpdate(
                "DELETE FROM " + TBL_STRUCT_GROUPS + ML + " WHERE ID NOT IN(SELECT DISTINCT AGROUP FROM "
                        + TBL_STRUCT_ASSIGNMENTS + " WHERE AGROUP IS NOT NULL)");
        stmt.executeUpdate(
                "DELETE FROM " + TBL_STRUCT_GROUP_OPTIONS + " WHERE ID NOT IN(SELECT DISTINCT AGROUP FROM "
                        + TBL_STRUCT_ASSIGNMENTS + " WHERE AGROUP IS NOT NULL)");
        int removed = stmt.executeUpdate(
                "DELETE FROM " + TBL_STRUCT_GROUPS + " WHERE ID NOT IN(SELECT DISTINCT AGROUP FROM "
                        + TBL_STRUCT_ASSIGNMENTS + " WHERE AGROUP IS NOT NULL)");
        if (removed > 0)
            LOG.info(removed + " orphaned groups removed.");
    } catch (SQLException e) {
        LOG.warn("Some orphaned groups could not be removed yet.");
    } finally {
        if (stmt != null)
            stmt.close();
    }
}

From source file:ca.sqlpower.persistance.CatNap.java

public static void load(Connection con, String tableName, Object loadTo, String where)
        throws IllegalAccessException, InvocationTargetException, NoSuchMethodException, SQLException {
    BeanUtils.describe(loadTo);//from w  w w  .  j a v  a  2  s  .c o m
    Statement stmt = null;
    StringBuffer sql = new StringBuffer();
    try {
        sql.append("SELECT * FROM " + tableName + " WHERE " + where);
        sql.append("\n");

        stmt = con.createStatement();
        ResultSet rs = stmt.executeQuery(sql.toString());
        while (rs.next()) {
            ResultSetMetaData metaData = rs.getMetaData();
            for (int i = 0; i < metaData.getColumnCount(); i++) {
                String beanPropertyName = underscoreToCamelCaps(metaData.getColumnName(i).toLowerCase());

                BeanUtils.setProperty(loadTo, beanPropertyName, rs.getObject(i));

            }
        }
    } catch (SQLException ex) {
        System.err.println("Catnap: Insert failed. Statement was:\n" + sql);
        throw ex;
    } finally {
        try {
            if (stmt != null)
                stmt.close();
        } catch (SQLException ex) {
            System.err.println(
                    "Catnap: Couldn't close the statement.  Damn.  But at least you won a stack trace:");
            ex.printStackTrace();
        }
    }
}

From source file:ems.util.DataHandler.java

public static boolean updateVoterDetails(String emailId, String mobileNo, String alternatMobileNo, String dob,
        String age, String community, String gender, String wardNo, String wardSrNo) {
    String sqlQuery = String.format(Q_U_VOTER_DETAILS, emailId, mobileNo, alternatMobileNo, dob, age, community,
            gender, wardNo, wardSrNo);//from   w  w  w  . j  a  va2 s  .  c  o  m
    Connection con = getConnection();
    Statement s = null;
    try {
        log.info("sqlQuery:" + sqlQuery);
        s = con.createStatement();
        int i = s.executeUpdate(sqlQuery);
        log.info("updateVoterDetails|" + i);
        return true;
    } catch (SQLException e) {
        log.error("updateVoterDetails: " + e.getMessage());
    } finally {
        try {
            if (s != null) {
                s.close();
            }
            if (con != null) {
                con.close();
            }
        } catch (SQLException ex) {
            log.error("updateVoterDetails: " + ex.getMessage());
        }
    }
    return false;
}

From source file:net.codjo.dataprocess.server.treatmenthelper.TreatmentHelper.java

private static List<TreatmentFragment> checkIntegrityRepositoryContent(Connection con) throws SQLException {
    List<TreatmentFragment> treatmentFragmentList = new ArrayList<TreatmentFragment>();
    Statement stmt = con.createStatement();
    try {//w  w  w . ja  va2 s.  com
        ResultSet rs = stmt.executeQuery("select TREATMENT_ID, CONTENT from PM_REPOSITORY_CONTENT "
                + " where CONTENT not like '%</" + DataProcessConstants.TREATMENT_ENTITY_XML + ">%'");
        try {
            while (rs.next()) {
                String content = rs.getString("CONTENT");
                String contentFragment = content.substring(content.length() - LENGTH)
                        .replace(DataProcessConstants.SPECIAL_CHAR_REPLACER_N, " ")
                        .replace(DataProcessConstants.SPECIAL_CHAR_REPLACER_R, " ");
                treatmentFragmentList.add(new TreatmentFragment(rs.getString("TREATMENT_ID"), contentFragment));
            }
            return treatmentFragmentList;
        } finally {
            rs.close();
        }
    } finally {
        stmt.close();
    }
}

From source file:com.persistent.cloudninja.utils.TenantProfileUtility.java

/**
 * Close Statement/*from w  ww .  jav a  2  s. c o m*/
 */
public void closeStatement(Statement statement) {
    if (statement != null) {
        try {
            statement.close();
        } catch (SQLException sqlException) {
            sqlException.printStackTrace();
        }
    }
}

From source file:com.flexive.ejb.beans.structure.FxStructureUtils.java

/**
 * Remove all properties that are no longer referenced
 *
 * @param con a valid connection/*w ww  . java  2s.  c  o  m*/
 * @throws java.sql.SQLException on errors
 */
public static void removeOrphanedProperties(Connection con) throws SQLException {
    Statement stmt = null;
    try {
        stmt = con.createStatement();
        stmt.executeUpdate(
                "DELETE FROM " + TBL_STRUCT_PROPERTIES + ML + " WHERE ID NOT IN(SELECT DISTINCT APROPERTY FROM "
                        + TBL_STRUCT_ASSIGNMENTS + " WHERE APROPERTY IS NOT NULL)");
        stmt.executeUpdate("DELETE FROM " + TBL_STRUCT_PROPERTY_OPTIONS
                + " WHERE ID NOT IN(SELECT DISTINCT APROPERTY FROM " + TBL_STRUCT_ASSIGNMENTS
                + " WHERE APROPERTY IS NOT NULL)");
        int removed = stmt.executeUpdate(
                "DELETE FROM " + TBL_STRUCT_PROPERTIES + " WHERE ID NOT IN(SELECT DISTINCT APROPERTY FROM "
                        + TBL_STRUCT_ASSIGNMENTS + " WHERE APROPERTY IS NOT NULL)");
        if (removed > 0)
            LOG.info(removed + " orphaned properties removed.");
    } catch (SQLException e) {
        LOG.warn("Some orphaned properties could not be removed yet.");
    } finally {
        if (stmt != null)
            stmt.close();
    }
}

From source file:kr.co.bitnine.octopus.testutils.MemoryDatabase.java

public void runExecuteUpdate(String sqlStmt) throws SQLException {
    Statement stmt = initialConnection.createStatement();
    stmt.executeUpdate(sqlStmt);//from   ww w. jav  a  2 s . co  m
    stmt.close();
}

From source file:org.web4thejob.orm.CreateSchemaTest.java

@Test
public void schemaExportTest() throws IOException, SQLException {

    Log4jConfigurer.initLogging("classpath:org/web4thejob/conf/log4j.xml");

    Properties datasource = new Properties();
    datasource.load(new ClassPathResource(DatasourceProperties.PATH).getInputStream());

    final Configuration configuration = new Configuration();
    configuration.setProperty(AvailableSettings.DIALECT, datasource.getProperty(DatasourceProperties.DIALECT));
    configuration.setProperty(AvailableSettings.DRIVER, datasource.getProperty(DatasourceProperties.DRIVER));
    configuration.setProperty(AvailableSettings.URL, "jdbc:hsqldb:mem:mydb");
    configuration.setProperty(AvailableSettings.USER, datasource.getProperty(DatasourceProperties.USER));
    configuration.setProperty(AvailableSettings.PASS, datasource.getProperty(DatasourceProperties.PASSWORD));

    final ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
            .applySettings(configuration.getProperties()).build();

    Connection connection = serviceRegistry.getService(ConnectionProvider.class).getConnection();
    Statement statement = connection.createStatement();
    statement.executeUpdate("CREATE SCHEMA w4tj;");
    statement.close();

    PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
    try {//from   w  w  w.  ja v a 2 s .c o  m
        for (Resource resource : resolver.getResources("classpath*:org/web4thejob/orm/**/*.hbm.xml")) {

            if (resource.getFile().getName().equals("AuxiliaryDatabaseObjects.hbm.xml"))
                continue;

            configuration.addFile(resource.getFile());
        }
    } catch (IOException e) {
        e.printStackTrace();
        throw new RuntimeException(e);
    }

    SchemaExport schemaExport = new SchemaExport(serviceRegistry, configuration);
    schemaExport.execute(Target.EXPORT, SchemaExport.Type.CREATE);

    if (!schemaExport.getExceptions().isEmpty()) {
        throw new RuntimeException((Throwable) schemaExport.getExceptions().get(0));
    }

}

From source file:org.brixcms.rmiserver.boot.Bootstrapper.java

/**
 * @param sf//from  ww  w.j a  va 2  s. c o  m
 */
private void bootstrapSchema() {
    Connection con = null;
    try {
        con = datasource.getConnection();
        Dialect dialect = Dialect.getDialect(configuration.getProperties());
        String[] schema = configuration.generateSchemaCreationScript(dialect);
        for (String stmt : schema) {
            Statement st = con.createStatement();
            st.executeUpdate(stmt);
            st.close();
        }
        con.commit();
    } catch (SQLException e1) {
        if (con != null) {
            try {
                con.rollback();
            } catch (SQLException e2) {
                try {
                    con.close();
                } catch (SQLException e3) {
                }
            }
        }
    }
}

From source file:com.amazonaws.services.kinesis.connectors.redshift.RedshiftBasicEmitter.java

private void executeStatement(String statement, Connection conn) throws IOException {
    try {//from w ww  .  j a va  2  s.c o  m
        Statement stmt = conn.createStatement();
        stmt.execute(statement);
        stmt.close();
        return;
    } catch (SQLException e) {
        LOG.error(e);
        throw new IOException(e);
    }

}