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:net.tirasa.ilgrosso.resetdb.Main.java

private static void resetSQLServer(final Connection conn) throws SQLException {

    Statement statement = conn.createStatement();
    final ResultSet resultSet = statement.executeQuery("SELECT sysobjects.name " + "FROM sysobjects "
            + "JOIN sysusers " + "ON sysobjects.uid = sysusers.uid "
            + "WHERE OBJECTPROPERTY(sysobjects.id, N'IsView') = 1");
    final List<String> drops = new ArrayList<String>();
    while (resultSet.next()) {
        drops.add("DROP VIEW " + resultSet.getString(1));
    }//from ww w .ja  va2  s.co  m
    resultSet.close();
    statement.close();

    statement = conn.createStatement();
    for (String drop : drops) {
        statement.executeUpdate(drop);
    }
    statement.close();

    statement = conn.createStatement();
    statement.executeUpdate("EXEC sp_MSforeachtable \"ALTER TABLE ? NOCHECK CONSTRAINT all\"");
    statement.close();

    statement = conn.createStatement();
    statement.executeUpdate("EXEC sp_MSforeachtable \"DROP TABLE ?\"");
    statement.close();

    conn.close();
}

From source file:application.bbdd.pool.java

public static void realizaConsulta2() {
    Connection conexion = null;/*from w  w  w  .j a  va2  s  .c  o  m*/
    Statement sentencia = null;
    ResultSet rs = null;

    try {
        conexion = getConexion();
        sentencia = conexion.createStatement();
        rs = sentencia.executeQuery("select count(*) from db");
        rs.next();
        JOptionPane.showMessageDialog(null, "El numero de bd es: " + rs.getInt(1));
        logStatistics();

    } catch (SQLException e) {
        JOptionPane.showMessageDialog(null, e.toString());
    } finally {
        try {
            rs.close();
            sentencia.close();
            liberaConexion(conexion);
        } catch (Exception fe) {
            JOptionPane.showMessageDialog(null, fe.toString());
        }
    }
}

From source file:act.installer.brenda.SQLConnection.java

/**
 * A handy function that closes a result set when an iterator has hit the end.  Does some ugly stuff with exceptions
 * but needs to be used inside an iterator.
 * @param results The result set to check for another row.
 * @param stmt A statement to close when we're out of results.
 * @return True if the result set has more rows, false otherwise (after closing).
 *//*from   w ww .  ja  v  a2s  .co  m*/
private static boolean hasNextHelper(ResultSet results, Statement stmt) {
    try {
        // TODO: is there a better way to do this?
        if (results.isLast()) {
            results.close(); // Tidy up if we find we're at the end.
            stmt.close();
            return false;
        } else {
            return true;
        }
    } catch (SQLException e) {
        /* Note: this is usually not a great thing to do.  In this circumstance we don't expect the
         * calling code to do anything but crash anyway, so... */
        throw new RuntimeException(e);
    }
}

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

public static void viewTable(Connection con) throws SQLException {
    Statement stmt = null;
    String query = "select * from COFFEE_HOUSES";
    try {//from www. ja v  a  2  s.  c  om
        stmt = con.createStatement();

        ResultSet rs = stmt.executeQuery(query);

        while (rs.next()) {
            System.out.println(rs.getInt("STORE_ID") + ", " + rs.getString("CITY") + ", " + rs.getInt("COFFEE")
                    + ", " + rs.getInt("MERCH") + ", " + rs.getInt("TOTAL"));
        }

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

From source file:com.hangum.tadpole.summary.report.DailySummaryReportJOB.java

/**
 * ? quote sql? ? ./*from  www .ja  v a  2s  .co m*/
 * 
 * @param userDB
 * @param strDML
 * @param args
 */
public static String executSQL(UserDBDAO userDB, String strTitle, String strDML) throws Exception {
    if (logger.isDebugEnabled())
        logger.debug("execute query " + strDML);

    java.sql.Connection javaConn = null;
    Statement stmt = null;
    ResultSet rs = null;
    try {
        SqlMapClient client = TadpoleSQLManager.getInstance(userDB);
        javaConn = client.getDataSource().getConnection();
        stmt = javaConn.createStatement();
        rs = stmt.executeQuery(strDML);

        return DailySummaryReport.makeResultSetTOHTML(strTitle, rs, 100);
    } finally {
        try {
            if (rs != null)
                rs.close();
        } catch (Exception e) {
        }
        try {
            if (stmt != null)
                stmt.close();
        } catch (Exception e) {
        }
        try {
            if (javaConn != null)
                javaConn.close();
        } catch (Exception e) {
        }
    }
}

From source file:edumsg.core.PostgresConnection.java

public static void disconnect(ResultSet rs, PreparedStatement statment, Connection conn, Statement query) {
    if (rs != null) {
        try {//w w w.j  av  a  2  s . c o  m
            rs.close();
        } catch (SQLException e) {
        }
    }
    if (statment != null) {
        try {
            statment.close();
        } catch (SQLException e) {
        }
    }
    if (conn != null) {
        try {
            conn.close();
        } catch (SQLException e) {
        }
    }

    if (query != null) {
        try {
            query.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

From source file:application.bbdd.pool.java

public static void realizaConsulta1() {
    Connection conexion = null;//ww  w  .  jav a 2  s.com
    Statement sentencia = null;
    ResultSet rs = null;

    try {
        // BasicDataSource nos reserva una conexion y nos la devuelve
        conexion = getConexion();
        sentencia = conexion.createStatement();
        rs = sentencia.executeQuery("select count(*) from user");
        rs.next();
        JOptionPane.showMessageDialog(null, "El numero de usuarios es: " + rs.getInt(1));
        logStatistics();

    } catch (SQLException e) {
        JOptionPane.showMessageDialog(null, e.toString());
    } finally {
        try {
            rs.close();
            sentencia.close();
            liberaConexion(conexion);
        } catch (Exception fe) {
            JOptionPane.showMessageDialog(null, fe.toString());
        }
    }
}

From source file:com.concursive.connect.web.modules.setup.utils.SetupUtils.java

/**
 * Determines if the database schema has been created
 *
 * @param db/*from w  w w .j a  v  a  2  s. c  o m*/
 * @return
 */
public static boolean isDatabaseInstalled(Connection db) {
    int count = -1;
    try {
        Statement st = db.createStatement();
        ResultSet rs = st.executeQuery("SELECT count(*) AS recordcount " + "FROM database_version ");
        rs.next();
        count = rs.getInt("recordcount");
        rs.close();
        st.close();
    } catch (Exception e) {
    }
    return count > 0;
}

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

public static void assertRowCount(long expected, String tableName, Connection connection) {
    Statement stmt = null;
    ResultSet rs = null;//from w ww .  j  a  va2  s .c o  m
    try {
        stmt = connection.createStatement();
        rs = stmt.executeQuery("SELECT count(*) FROM " + tableName);
        rs.next();
        assertEquals(expected, rs.getLong(1));
    } catch (SQLException e) {
        LOG.error("Can't verify number of rows", e);
        fail();
    } finally {
        try {
            connection.commit();
            if (stmt != null) {
                stmt.close();
            }
            if (rs != null) {
                rs.close();
            }
        } catch (SQLException ex) {
            LOG.info("Ignored exception in finally block.");
        }
    }
}

From source file:dbcount.DBCountPageView.java

private static boolean verify(final DBMapReduceJobConf jobConf, final long totalPageview) throws SQLException {
    //check total num pageview
    String dbUrl = jobConf.getReduceOutputDbUrl();
    final Connection conn;
    try {//w w w .  jav a 2s.c o m
        conn = jobConf.getConnection(dbUrl, true);
    } catch (ClassNotFoundException e) {
        throw new IllegalStateException(e);
    }
    String sumPageviewQuery = "SELECT SUM(pageview) FROM Pageview";
    Statement st = null;
    ResultSet rs = null;
    try {
        st = conn.createStatement();
        rs = st.executeQuery(sumPageviewQuery);
        rs.next();
        long sumPageview = rs.getLong(1);

        LOG.info("totalPageview=" + totalPageview);
        LOG.info("sumPageview=" + sumPageview);

        return totalPageview == sumPageview && totalPageview != 0;
    } finally {
        if (st != null) {
            st.close();
        }
        if (rs != null) {
            rs.close();
        }
        conn.close();
    }
}