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.oracle.tutorial.jdbc.CachedRowSetSample.java

public static void viewTable(Connection con) throws SQLException {
    Statement stmt = null;
    String query = "select * from MERCH_INVENTORY";
    try {//from  w  w w  . j  a v a  2  s.com
        stmt = con.createStatement();

        ResultSet rs = stmt.executeQuery(query);

        while (rs.next()) {
            System.out.println("Found item " + rs.getInt("ITEM_ID") + ": " + rs.getString("ITEM_NAME") + " ("
                    + rs.getInt("QUAN") + ")");
        }

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

From source file:com.fer.hr.olap.util.ObjectUtil.java

public static List<SimpleCubeElement> convert2simple(ResultSet rs) {
    try {/*from w  w  w. j  a v a 2  s. co m*/
        int width = 0;
        boolean first = true;
        List<SimpleCubeElement> elements = new ArrayList<>();
        if (rs != null) {
            while (rs.next()) {
                if (first) {
                    first = false;
                    width = rs.getMetaData().getColumnCount();
                }
                String[] row = new String[3];
                for (int i = 0; i < width; i++) {
                    row[i] = rs.getString(i + 1);
                }
                SimpleCubeElement s = new SimpleCubeElement(row[0], row[1], row[2]);
                elements.add(s);
            }
        }
        return elements;

    } catch (Exception e) {
        throw new SaikuServiceException("Error converting ResultSet into SimpleCubeElement", e);
    } finally {
        if (rs != null) {
            Statement statement = null;
            Connection con = null;
            try {
                statement = rs.getStatement();

            } catch (Exception e) {
                throw new SaikuServiceException(e);
            } finally {
                try {
                    rs.close();
                    if (statement != null) {
                        statement.close();
                    }
                } catch (Exception ee) {
                    LOG.error("Could not close statement", ee);
                }

                rs = null;
            }
        }
    }
}

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

public static void delete(Connection con, String tableName, String where)
        throws IllegalAccessException, InvocationTargetException, NoSuchMethodException, SQLException {
    Statement stmt = null;
    StringBuffer sql = new StringBuffer();
    try {/*w w  w  . ja  v a2 s.  c om*/
        sql.append("DELETE FROM " + tableName);
        sql.append(" WHERE " + where);
        sql.append("\n)");

        stmt = con.createStatement();
        stmt.executeUpdate(sql.toString());
    } 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:database.HashTablesTools.java

private static void dropTable(Connection connection, String tableName) {
    try {//  w  ww  . j a  v  a 2s . c  o  m
        Statement stmt = connection.createStatement();
        String sql = "DROP TABLE " + tableName;
        stmt.execute(sql);
        System.out.println("Drop table from myDB " + tableName + " !");
        stmt.close();
    } catch (SQLException e1) {
        System.out.println("Table " + tableName + " is not existing so cannot be droped");
    }
}

From source file:com.espertech.esperio.db.SupportDatabaseService.java

public static Object[][] readAll(String tableName) throws SQLException {
    Connection connection = getConnection(PARTURL, DBUSER, DBPWD);
    connection.setAutoCommit(true);//from www .jav a2s.  c om
    Statement stmt = connection.createStatement();
    String sql = "select * from " + tableName;
    log.info("Executing sql : " + sql);
    ResultSet resultSet = stmt.executeQuery(sql);

    List<Object[]> rows = new ArrayList<Object[]>();
    while (resultSet.next()) {
        List<Object> row = new ArrayList<Object>();
        for (int i = 0; i < resultSet.getMetaData().getColumnCount(); i++) {
            row.add(resultSet.getObject(i + 1));
        }
        rows.add(row.toArray());
    }

    Object[][] arr = new Object[rows.size()][];
    for (int i = 0; i < rows.size(); i++) {
        arr[i] = rows.get(i);
    }

    stmt.close();
    connection.close();
    return arr;
}

From source file:com.baidu.qa.service.test.util.JdbcUtil.java

protected static int excuteInsertOrUpdateSql(String sql, String dbname) throws Exception {

    //   ???// ww  w .j a v  a2s  . c  o m
    Connection con = null;
    Statement sm = null;
    try {
        con = MysqlDatabaseManager.getCon(dbname);
        Assert.assertNotNull("connect to db error:" + dbname, con);
        //??
        sm = con.createStatement();
        log.info("[sql:]" + sql);
        return sm.executeUpdate(sql);

    } catch (Exception e) {
        throw e;
    } finally {
        if (con != null) {
            con.close();

        }
        if (sm != null) {
            sm.close();
        }

    }

}

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

public static void getCoffeesBoughtBySupplier(String supplierName, Connection con) throws SQLException {
    Statement stmt = null;
    String query = "SELECT COFFEES.COF_NAME " + "FROM COFFEES, SUPPLIERS " + "WHERE SUPPLIERS.SUP_NAME LIKE '"
            + supplierName + "' " + "and SUPPLIERS.SUP_ID = COFFEES.SUP_ID";

    try {//  w w w. ja v  a  2s.c  om
        stmt = con.createStatement();
        ResultSet rs = stmt.executeQuery(query);
        System.out.println("Coffees bought from " + supplierName + ": ");
        while (rs.next()) {
            String coffeeName = rs.getString(1);
            System.out.println("     " + coffeeName);
        }
    } catch (SQLException e) {
        JDBCTutorialUtilities.printSQLException(e);
    } finally {
        if (stmt != null) {
            stmt.close();
        }
    }
}

From source file:com.quest.orahive.HiveJdbcClient.java

private static void initializeOracleSession(Connection connection, OraHiveOptions opts) {

    String sql = "";
    try {//from ww w .  j  av  a2 s  . co m
        sql = "begin \n" + "  dbms_application_info.set_module(module_name => '%s', action_name => '%s'); \n"
                + "end;";

        sql = String.format(sql, Constants.ORAHIVE_PRODUCT_NAME, getOracleTableName(opts));

        Statement statement = connection.createStatement();
        statement.execute(sql);
        statement.close();
    } catch (Exception ex) {
        LOG.error(String.format("An error occurred while attempting to execute "
                + "the following Oracle session-initialization statement:" + "\n%s" + "\nError:" + "\n%s", sql,
                ex.getMessage()));
    }
}

From source file:edu.ucsb.nceas.MCTestCase.java

protected static void dbQuery(String sqlStatement, String methodName) throws SQLException {

    DBConnectionPool connPool = DBConnectionPool.getInstance();
    DBConnection dbconn = DBConnectionPool.getDBConnection(methodName);
    int serialNumber = dbconn.getCheckOutSerialNumber();

    Statement statement = dbconn.createStatement();

    debug("Executing against db: " + sqlStatement);
    statement.executeQuery(sqlStatement);

    statement.close();

    DBConnectionPool.returnDBConnection(dbconn, serialNumber);
}

From source file:edu.ucsb.nceas.MCTestCase.java

protected static void dbUpdate(String sqlStatement, String methodName) throws SQLException {

    DBConnectionPool connPool = DBConnectionPool.getInstance();
    DBConnection dbconn = DBConnectionPool.getDBConnection(methodName);
    int serialNumber = dbconn.getCheckOutSerialNumber();

    Statement statement = dbconn.createStatement();

    debug("Executing against db: " + sqlStatement);
    statement.executeUpdate(sqlStatement);

    statement.close();

    DBConnectionPool.returnDBConnection(dbconn, serialNumber);
}