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.manydesigns.portofino.persistence.QueryUtils.java

/**
 * Runs a SQL query against a session. The query can contain placeholders for the parameters, as supported by
 * {@link PreparedStatement}. <br>
 * INSERT UPDATE DELETE DROP CREATE ALTER TRUNCATE RENAME sueprpan add
 * /*www  .  java  2 s . c o m*/
 * @param session the session
 * @param queryString the query
 * @param parameters parameters to substitute in the query
 * @return the results of the query as an Object[] (an array cell per column)
 */
public static int runSqlDml(Session session, final String queryString, final Object[] parameters) {
    final List<Integer> result = new ArrayList<Integer>();
    try {
        session.doWork(new Work() {
            public void execute(Connection connection) throws SQLException {
                Statement stmt = connection.createStatement();
                try {
                    result.add(stmt.executeUpdate(queryString));
                } finally {
                    stmt.close();
                }
            }
        });
    } catch (HibernateException e) {
        result.add(-1);
        session.getTransaction().rollback();
        session.beginTransaction();
        throw e;
    }
    if (result.size() > 0) {
        return result.get(0);
    }
    return -1;
}

From source file:com.manydesigns.portofino.persistence.QueryUtils.java

/**
 * Runs a SQL query against a session. The query is processed with an {@link OgnlSqlFormat}, so it can access values
 * from the OGNL context.<br>//from   w  w  w  .  ja  v a  2  s  .co  m
 * INSERT UPDATE DELETE DROP CREATE ALTER TRUNCATE RENAME hongliangpan add
 * 
 * @param session the session
 * @param sql the query string
 * @return the results of the query as an Object[] (an array cell per column)
 */
public static int runSqlDml(Session session, String sql) {
    OgnlSqlFormat sqlFormat = OgnlSqlFormat.create(sql);
    final String queryString = sqlFormat.getFormatString();
    final List<Integer> result = new ArrayList<Integer>();
    try {
        session.doWork(new Work() {
            public void execute(Connection connection) throws SQLException {
                Statement stmt = connection.createStatement();
                try {
                    result.add(stmt.executeUpdate(queryString));
                } finally {
                    stmt.close();
                }
            }
        });
    } catch (HibernateException e) {
        result.add(-1);
        session.getTransaction().rollback();
        session.beginTransaction();
        throw e;
    }
    if (result.size() > 0) {
        return result.get(0);
    }
    return -1;
}

From source file:sk.upjs.ics.paz1c.mp3library.SqliteMigration.java

private void execute(String sql) throws SQLException {
    Statement stmt = c.createStatement();
    stmt.executeUpdate(sql);/*from   www  .  ja v a  2 s  .c o m*/
    stmt.close();
}

From source file:com.aurel.track.dbase.UpdateDbSchema.java

/**
 * Gets the database version//from   w ww  . jav a  2  s  .  c o m
 * @param dbConnection
 * @return
 */
public static int getDBVersion(Connection dbConnection) {
    Statement istmt = null;
    ResultSet rs = null;
    try {
        istmt = dbConnection.createStatement();
        rs = istmt.executeQuery("SELECT DBVERSION FROM TSITE");
        if (rs == null || !rs.next()) {
            LOGGER.info("TSITE is empty.");
        } else {
            return rs.getInt(1);
        }
    } catch (Exception e) {
        LOGGER.debug(ExceptionUtils.getStackTrace(e));
    } finally {
        try {
            if (rs != null) {
                rs.close();
            }
        } catch (Exception e) {
            LOGGER.debug(ExceptionUtils.getStackTrace(e));
        }
        try {
            if (istmt != null) {
                istmt.close();
            }
        } catch (Exception e) {
            LOGGER.debug(ExceptionUtils.getStackTrace(e));
        }
        try {
            if (dbConnection != null) {
                dbConnection.close();
            }
        } catch (Exception e) {
            LOGGER.debug(ExceptionUtils.getStackTrace(e));
        }
    }
    return 0;
}

From source file:com.oracle.tutorial.jdbc.CoffeesTable.java

public static void viewTable(Connection con) throws SQLException {
    Statement stmt = null;
    String query = "select COF_NAME, SUP_ID, PRICE, SALES, TOTAL from COFFEES";
    try {//w w  w .  j  a  v  a2s  .c  o  m
        stmt = con.createStatement();
        ResultSet rs = stmt.executeQuery(query);

        while (rs.next()) {
            String coffeeName = rs.getString("COF_NAME");
            int supplierID = rs.getInt("SUP_ID");
            float price = rs.getFloat("PRICE");
            int sales = rs.getInt("SALES");
            int total = rs.getInt("TOTAL");
            System.out.println(coffeeName + ", " + supplierID + ", " + price + ", " + sales + ", " + total);
        }

    } catch (SQLException e) {
        JDBCTutorialUtilities.printSQLException(e);
    } finally {
        if (stmt != null) {
            stmt.close();
        }
    }
}

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

public void closeStatement(Statement statement) {

    if (statement != null) {
        try {/*  ww w  . j a va  2s .  co m*/
            statement.close();
        } catch (SQLException sqlException) {
            sqlException.printStackTrace();
        }
    }
}

From source file:org.softdays.mandy.AbstractDbSetupTest.java

public void execute(final String sql) {
    try {/*from  w  w  w .  j  ava 2 s.c  o m*/
        final Connection c = this.databaseTester.getConnection().getConnection();
        final Statement s = c.createStatement();
        s.executeUpdate(sql);
        s.close();
    } catch (final Exception e) {
        Assert.fail(e.getMessage());
    }
}

From source file:it.unibas.spicy.persistence.relational.SimpleDbConnectionFactory.java

public void close(Statement statement) {
    try {/*from w  w w.j a v  a  2  s  .  co m*/
        if (statement != null) {
            statement.close();
        }
    } catch (SQLException sqle) {
        logger.fatal(sqle.toString());
    }
}

From source file:com.linuxrouter.netcool.session.QueryUtils.java

public Boolean executeUpdate(String dbName, String sql) {
    Long start = System.currentTimeMillis();
    try {//from  www .  j a v  a  2 s . c  o m
        //connection caching...
        Connection con = null;
        if (connectionMap.get(dbName) == null) {
            BasicDataSource ds = DbUtils.getSimpleDataSourceByName(dbName);
            con = ds.getConnection();
            connectionMap.put(dbName, con);
        } else {
            con = connectionMap.get(dbName);

        }
        Statement st = con.createStatement();
        st.executeUpdate(sql);
        st.close();
    } catch (SQLException ex) {
        logger.error("Erro ao executar query:", ex);
        return false;
    }
    Long end = System.currentTimeMillis();
    return true;
}

From source file:com.buddycloud.mediaserver.business.jdbc.schema.Schema.java

@SuppressWarnings("unchecked")
public void runScript(MetaDataSource dataSource, String sqlFile)
        throws IOException, FileNotFoundException, SQLException {
    List<String> readLines = IOUtils.readLines(new FileInputStream(sqlFile));

    Connection connection = dataSource.getConnection();
    StringBuilder statementStr = new StringBuilder();

    for (String line : readLines) {
        statementStr.append(line);/* www  . ja v  a2s.  co m*/
        if (line.endsWith(SQL_DELIMITER)) {
            Statement statement = connection.createStatement();
            statement.execute(statementStr.toString());
            statement.close();
            statementStr.setLength(0);
        }
    }

    connection.close();
}