Example usage for java.sql ResultSet close

List of usage examples for java.sql ResultSet close

Introduction

In this page you can find the example usage for java.sql ResultSet close.

Prototype

void close() throws SQLException;

Source Link

Document

Releases this ResultSet object's database and JDBC resources immediately instead of waiting for this to happen when it is automatically closed.

Usage

From source file:com.concursive.connect.web.modules.communications.utils.EmailUpdatesUtils.java

public static void manageQueue(Connection db, TeamMember teamMember) throws SQLException {
    //Determine if the member is part of any other projects and has a matching email updates preference
    PreparedStatement pst = db.prepareStatement("SELECT count(*) AS record_count " + "FROM project_team pt "
            + "WHERE pt.user_id = ? " + "AND pt.email_updates_schedule = ? ");
    int i = 0;/*  ww w .ja  v  a2  s.  com*/
    pst.setInt(++i, teamMember.getUserId());
    pst.setInt(++i, teamMember.getEmailUpdatesSchedule());
    ResultSet rs = pst.executeQuery();
    int records = 0;
    if (rs.next()) {
        records = rs.getInt("record_count");
    }
    rs.close();
    pst.close();

    if (records == 0) {
        //Delete the queue since it is no longer needed.
        String field = "";
        int emailUpdatesSchedule = teamMember.getEmailUpdatesSchedule();
        if (emailUpdatesSchedule > 0) {
            if (emailUpdatesSchedule == TeamMember.EMAIL_OFTEN) {
                field = "schedule_often";
            } else if (emailUpdatesSchedule == TeamMember.EMAIL_DAILY) {
                field = "schedule_daily";
            } else if (emailUpdatesSchedule == TeamMember.EMAIL_WEEKLY) {
                field = "schedule_weekly";
            } else if (emailUpdatesSchedule == TeamMember.EMAIL_MONTHLY) {
                field = "schedule_monthly";
            }
            i = 0;
            pst = db.prepareStatement(
                    "DELETE FROM email_updates_queue " + "WHERE enteredby = ? AND " + field + " = ? ");
            pst.setInt(++i, teamMember.getUserId());
            pst.setBoolean(++i, true);
            pst.executeUpdate();
            pst.close();
        }
    }
}

From source file:com.krawler.database.DbPool.java

/**
 * Closes a ResultSet and wraps any resulting exception in a
 * ServiceException.//from   ww  w  . ja v a 2  s.c om
 * 
 * @param rs
 * @throws ServiceException
 */
public static void closeResults(ResultSet rs) throws ServiceException {
    if (rs != null) {
        try {
            rs.close();
        } catch (SQLException e) {
            throw ServiceException.FAILURE("closing statement", e);
        }
    }
}

From source file:com.chiralbehaviors.CoRE.kernel.KernelUtil.java

static void alterTriggers(Connection connection, boolean enable) throws SQLException {
    for (String table : new String[] { "ruleform.agency", "ruleform.product", "ruleform.location" }) {
        String query = String.format("ALTER TABLE %s %s TRIGGER ALL", table, enable ? "ENABLE" : "DISABLE");
        connection.createStatement().execute(query);
    }/* www. j a v a2s.co  m*/
    ResultSet r = connection.createStatement().executeQuery(KernelUtil.SELECT_TABLE);
    while (r.next()) {
        String table = r.getString("name");
        String query = String.format("ALTER TABLE %s %s TRIGGER ALL", table, enable ? "ENABLE" : "DISABLE");
        connection.createStatement().execute(query);
    }
    r.close();
}

From source file:com.aurel.track.admin.server.status.ServerStatusBL.java

/**
 * // w  w w  . jav a2  s . c o  m
 */
public static Double loadPing() {
    //
    //
    //
    //
    Connection connection = null;
    try {
        connection = InitDatabase.getConnection();
        Date start = new Date();
        for (int i = 0; i < 10; ++i) {
            Statement stmt = connection.createStatement();
            ResultSet rs = stmt.executeQuery("SELECT OBJECTID FROM TSITE");
            while (rs.next()) {
                rs.getInt(1);
            }
            rs.close();
            stmt.close();
        }
        Date stop = new Date();
        return new Double((stop.getTime() - start.getTime())) / 10.0;
    } catch (Exception e) {
        LOGGER.error("Can't ping: " + e.getMessage());
    } finally {
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException e) {
                LOGGER.warn("Closing the connection failed with " + e.getMessage());
                LOGGER.debug(ExceptionUtils.getStackTrace(e));
            }
        }
    }
    return -999.99;
}

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

private static void closeQuietly(ResultSet resultSet, Statement stmt) {
    try {/*w  w  w  . j  ava  2s  . c om*/
        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:massbank.extend.ChemicalFormulaUtils.java

/**
 * CIqXg/*from   ww  w  .  j  a  v  a 2s  . c  o  m*/
 */
public static List<String[]> getIonMassList() throws IOException {
    List<String[]> massList = new ArrayList();
    try {

        Class.forName("com.mysql.jdbc.Driver");
        String conUrl = "jdbc:mysql://localhost/FORMULA_STRUCTURE_RELATION";
        Connection con = DriverManager.getConnection(conUrl, "bird", "bird2006");
        Statement stmt = con.createStatement();
        String sql = "SELECT FORMULA, MASS FROM ION_MASS order by MASS";
        ResultSet rs = stmt.executeQuery(sql);
        while (rs.next()) {
            String formula = rs.getString("FORMULA");
            String mass = rs.getString("MASS");
            massList.add(new String[] { formula, mass });
        }
        rs.close();
        stmt.close();
        con.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return massList;
}

From source file:net.tirasa.ilgrosso.resetdb.Main.java

private static void resetOracle(final Connection conn) throws Exception {

    final Statement statement = conn.createStatement();

    ResultSet resultSet = statement.executeQuery(
            "SELECT 'DROP VIEW ' || object_name || ';'" + " FROM user_objects WHERE object_type='VIEW'");
    final List<String> drops = new ArrayList<String>();
    while (resultSet.next()) {
        drops.add(resultSet.getString(1));
    }//from  w w w .  j ava 2s.  c  o  m
    resultSet.close();

    for (String drop : drops) {
        statement.executeUpdate(drop.substring(0, drop.length() - 1));
    }
    drops.clear();

    resultSet = statement.executeQuery("SELECT 'DROP INDEX ' || object_name || ';'"
            + " FROM user_objects WHERE object_type='INDEX'" + " AND object_name NOT LIKE 'SYS_%'");
    while (resultSet.next()) {
        drops.add(resultSet.getString(1));
    }
    resultSet.close();

    for (String drop : drops) {
        try {
            statement.executeUpdate(drop.substring(0, drop.length() - 1));
        } catch (SQLException e) {
            LOG.error("Could not perform: {}", drop);
        }
    }
    drops.clear();

    resultSet = statement.executeQuery("SELECT 'DROP TABLE ' || table_name || ' CASCADE CONSTRAINTS;'"
            + " FROM all_TABLES WHERE owner='" + ((String) ctx.getBean("username")).toUpperCase() + "'");
    while (resultSet.next()) {
        drops.add(resultSet.getString(1));
    }
    resultSet.close();

    resultSet = statement
            .executeQuery("SELECT 'DROP SEQUENCE ' || sequence_name || ';'" + " FROM user_SEQUENCES");
    while (resultSet.next()) {
        drops.add(resultSet.getString(1));
    }
    resultSet.close();

    for (String drop : drops) {
        statement.executeUpdate(drop.substring(0, drop.length() - 1));
    }

    statement.close();
    conn.close();
}

From source file:net.tirasa.ilgrosso.resetdb.Main.java

private static void resetMySQL(final Connection conn) throws Exception {

    final Statement statement = conn.createStatement();

    ResultSet resultSet = statement
            .executeQuery("SELECT concat('DROP VIEW IF EXISTS ', table_name, ' CASCADE;')"
                    + "FROM information_schema.views;");
    final List<String> drops = new ArrayList<String>();
    while (resultSet.next()) {
        drops.add(resultSet.getString(1));
    }//from  w  w  w.  ja v a2 s  .c o  m
    resultSet.close();

    for (String drop : drops) {
        statement.executeUpdate(drop.substring(0, drop.length() - 1));
    }
    drops.clear();

    drops.add("SET FOREIGN_KEY_CHECKS = 0;");
    resultSet = statement.executeQuery("SELECT concat('DROP TABLE IF EXISTS ', table_name, ' CASCADE;')"
            + "FROM information_schema.tables;");
    while (resultSet.next()) {
        drops.add(resultSet.getString(1));
    }
    resultSet.close();
    drops.add("SET FOREIGN_KEY_CHECKS = 1;");

    for (String drop : drops) {
        statement.executeUpdate(drop.substring(0, drop.length() - 1));
    }

    statement.close();
    conn.close();
}

From source file:com.nabla.wapp.report.server.ReportManager.java

protected static Integer getRole(final Connection conn, final String name) throws SQLException {
    if (name != null) {
        final PreparedStatement stmt = StatementFormat.prepare(conn,
                "SELECT id FROM role WHERE name LIKE ? AND uname IS NOT NULL;", name);
        try {//from ww  w  .  jav  a2  s.  com
            final ResultSet rs = stmt.executeQuery();
            try {
                if (rs.next())
                    return rs.getInt(1);
            } finally {
                rs.close();
            }
        } finally {
            stmt.close();
        }
    }
    return null;
}

From source file:Main.java

public static String getCLOB(int id) throws Exception {
    Connection conn = null;//from ww  w .  jav a 2s  . c o m
    ResultSet rs = null;
    PreparedStatement pstmt = null;
    String query = "SELECT clobData FROM tableName WHERE id = ?";

    try {
        conn = getConnection();
        pstmt = conn.prepareStatement(query);
        pstmt.setInt(1, id);
        rs = pstmt.executeQuery();
        rs.next();
        Clob clob = rs.getClob(1);
        // materialize CLOB onto client
        String wholeClob = clob.getSubString(1, (int) clob.length());
        return wholeClob;
    } finally {
        rs.close();
        pstmt.close();
        conn.close();
    }
}