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.dynamobi.network.DynamoNetworkUdr.java

/**
 * Grabbing the list of repos along with their availability
 * status for internal use. /*from w  w w .  j  a va2 s .c o m*/
 */
private static List<RepoInfo> getRepoUrls() throws SQLException {
    List<RepoInfo> repos = new ArrayList<RepoInfo>();

    Connection conn = DriverManager.getConnection("jdbc:default:connection");
    String query = "SELECT repo_url FROM localdb.sys_network.repositories " + "ORDER BY repo_url";
    PreparedStatement ps = conn.prepareStatement(query);
    ps.execute();
    ResultSet rs = ps.getResultSet();
    while (rs.next()) {
        String repo = rs.getString(1);
        boolean accessible = true;
        try {
            JSONObject ob = downloadMetadata(repo);
        } catch (SQLException e) {
            if (e.getMessage().equals(URL_TIMEOUT))
                accessible = false;
        }
        repos.add(new RepoInfo(repo, accessible));
    }
    rs.close();
    ps.close();

    return repos;
}

From source file:com.ubipass.middleware.ems.EMSUtil.java

/**
 * Get last time of system shutdown.//ww  w  . j  av a 2 s.c  o  m
 * 
 * @return time stamp of last system shutdown
 */
private static long getLastShutdownTime() {

    Connection conn = null;
    Statement stmt = null;
    ResultSet rs = null;
    long result = 0;

    try {
        conn = DBConnPool.getConnection();

        // get last shutdown time from table sysconfig
        stmt = conn.createStatement();
        rs = stmt.executeQuery("SELECT currentTime FROM sysconfig");
        rs.next();
        result = rs.getLong(1);
    } catch (Exception e) {
        SystemLogger.error("[EMS] getLastShutdownTime() error: " + e.getMessage());
    } finally {
        try {
            if (rs != null) {
                rs.close();
            }

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

            if (conn != null) {
                conn.close();
            }
        } catch (Exception e) {

        }
    }

    return (result == 0) ? new Date().getTime() : result;

}

From source file:bullioneconomy.bullionchart.java

/**
 * Creates a dataset, consisting of two series of monthly data.
 *
 * @return The dataset./*from  w ww  .jav  a 2 s.  co m*/
 */
private static XYDataset createDataset() throws ClassNotFoundException, SQLException, ParseException {

    TimeSeries s1 = new TimeSeries("Actual", Day.class);
    TimeSeries s2 = new TimeSeries("Forecasted", Day.class);
    Class.forName("com.mysql.jdbc.Driver");
    try (Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/BULLION", "yajnab",
            "petrol123")) {
        Statement stmt = con.createStatement();
        ResultSet result = stmt.executeQuery("SELECT * FROM gold");
        ArrayList<Double> arm = new ArrayList<>();
        predictor pcd = new predictor();
        arm = pcd.ARIMApredict();
        int i = 0;
        while (result.next()) {

            String datefeed = result.getString(1);
            Double value = result.getDouble(2);
            int[] m = new int[3];
            //bullionchart bcc = new bullionchart();
            //m = bcc.dateget(datefeed);
            m = dateget(datefeed);
            s1.add(new Day(m[0], m[1], m[2]), value);
            s2.add(new Day(m[0], m[1], m[2]), arm.get(i));
            i++;
        }
        result.close();
        /*s1.add(new Month(2, 2001), 181.8);
        s1.add(new Month(3, 2001), 167.3);
        s1.add(new Month(4, 2001), 153.8);
        s1.add(new Month(5, 2001), 167.6);
        s1.add(new Month(6, 2001), 158.8);
        s1.add(new Month(7, 2001), 148.3);
        s1.add(new Month(8, 2001), 153.9);
        s1.add(new Month(9, 2001), 142.7);
        s1.add(new Month(10, 2001), 123.2);
        s1.add(new Month(11, 2001), 131.8);
        s1.add(new Month(12, 2001), 139.6);
        s1.add(new Month(1, 2002), 142.9);
        s1.add(new Month(2, 2002), 138.7);
        s1.add(new Month(3, 2002), 137.3);
        s1.add(new Month(4, 2002), 143.9);
        s1.add(new Month(5, 2002), 139.8);
        s1.add(new Month(6, 2002), 137.0);
        s1.add(new Month(7, 2002), 132.8);*/
    }

    /*TimeSeries s2 = new TimeSeries("Forecasted", Month.class);
    s2.add(new Month(2, 2001), 129.6);
    s2.add(new Month(3, 2001), 123.2);
    s2.add(new Month(4, 2001), 117.2);
    s2.add(new Month(5, 2001), 124.1);
    s2.add(new Month(6, 2001), 122.6);
    s2.add(new Month(7, 2001), 119.2);
    s2.add(new Month(8, 2001), 116.5);
    s2.add(new Month(9, 2001), 112.7);
    s2.add(new Month(10, 2001), 101.5);
    s2.add(new Month(11, 2001), 106.1);
    s2.add(new Month(12, 2001), 110.3);
    s2.add(new Month(1, 2002), 111.7);
    s2.add(new Month(2, 2002), 111.0);
    s2.add(new Month(3, 2002), 109.6);
    s2.add(new Month(4, 2002), 113.2);
    s2.add(new Month(5, 2002), 111.6);
    s2.add(new Month(6, 2002), 108.8);
    s2.add(new Month(7, 2002), 101.6);*/

    TimeSeriesCollection dataset = new TimeSeriesCollection();
    dataset.addSeries(s1);
    dataset.addSeries(s2);

    dataset.setDomainIsPointsInTime(true);

    return dataset;

}

From source file:com.chaosinmotion.securechat.server.commands.CreateAccount.java

/**
 * Process the create account request. This should receive the following
 * objects: the username, the password, the device ID and the public key
 * for the device. This adds a new entry in the account database, and
 * creates a new device./* ww w.  ja v a2s.c om*/
 * 
 * If the user account cannot be created, this returns nil.
 * @param requestParams
 * @return
 */
public static UserInfo processRequest(JSONObject requestParams)
        throws ClassNotFoundException, SQLException, IOException {
    String username = requestParams.optString("username");
    String password = requestParams.optString("password");
    String deviceid = requestParams.optString("deviceid");
    String pubkey = requestParams.optString("pubkey");

    /*
     * Attempt to insert a new user into the database
     */

    Connection c = null;
    PreparedStatement ps = null;
    ResultSet rs = null;

    try {
        c = Database.get();
        ps = c.prepareStatement("INSERT INTO Users " + "    ( username, password ) " + "VALUES "
                + "    ( ?, ? ); SELECT currval('Users_userid_seq')");
        ps.setString(1, username);
        ps.setString(2, password);

        try {
            ps.execute();
        } catch (SQLException ex) {
            return null; // Can't insert; duplicate username?
        }
        int utc = ps.getUpdateCount();
        int userid = 0;
        if ((utc == 1) && ps.getMoreResults()) {
            rs = ps.getResultSet();
            if (rs.next()) {
                userid = rs.getInt(1);
            }
            rs.close();
            rs = null;
        }

        ps.close();
        ps = null;

        /*
         * We now have the user index. Insert the device. Note that it is
         * highly unlikely we will have a UUID collision, but we verify
         * we don't by deleting any rows in the device table with the
         * specified UUID. The worse case scenario is a collision which
         * knocks someone else off the air. (The alternative would be
         * to accidentally send the wrong person duplicate messages.)
         * 
         * Note that we don't actually use a device-identifying identifer,
         * choosing instead to pick a UUID, so we need to deal with
         * the possibility (however remote) of duplicate UUIDs.
         * 
         * In the off chance we did have a collision, we also delete all
         * old messages to the device; that prevents messages from being
         * accidentally delivered.
         */

        ps = c.prepareStatement("DELETE FROM Messages " + "WHERE messageid IN "
                + "    (SELECT Messages.messageid " + "     FROM Messages, Devices "
                + "     WHERE Messages.deviceid = Devices.deviceid " + "     AND Devices.deviceuuid = ?)");
        ps.setString(1, deviceid);
        ps.execute();
        ps.close();
        ps = null;

        ps = c.prepareStatement("DELETE FROM Devices WHERE deviceuuid = ?");
        ps.setString(1, deviceid);
        ps.execute();
        ps.close();
        ps = null;

        ps = c.prepareStatement("INSERT INTO Devices " + "    ( userid, deviceuuid, publickey ) " + "VALUES "
                + "    ( ?, ?, ?)");
        ps.setInt(1, userid);
        ps.setString(2, deviceid);
        ps.setString(3, pubkey);
        ps.execute();

        /*
         * Complete; return the user info record
         */

        return new Login.UserInfo(userid);
    } finally {
        if (rs != null)
            rs.close();
        if (ps != null)
            ps.close();
        if (c != null)
            c.close();
    }
}

From source file:au.org.ala.layers.stats.ObjectsStatsGenerator.java

private static void updateArea(String fid) {

    try {/* www . ja  v  a 2  s  . c o  m*/
        Connection conn = getConnection();
        String sql = "SELECT pid from objects where area_km is null and st_geometrytype(the_geom) <> 'Point'";
        if (fid != null) {
            sql = sql + " and fid = '" + fid + "'";
        }

        sql = sql + " limit 200000;";

        System.out.println("loading area_km ...");
        Statement s1 = conn.createStatement();
        ResultSet rs1 = s1.executeQuery(sql);

        LinkedBlockingQueue<String> data = new LinkedBlockingQueue<String>();
        while (rs1.next()) {
            data.put(rs1.getString("pid"));
        }

        CountDownLatch cdl = new CountDownLatch(data.size());

        AreaThread[] threads = new AreaThread[CONCURRENT_THREADS];
        for (int j = 0; j < CONCURRENT_THREADS; j++) {
            threads[j] = new AreaThread(data, cdl, getConnection().createStatement());
            threads[j].start();
        }

        cdl.await();

        for (int j = 0; j < CONCURRENT_THREADS; j++) {
            threads[j].s.close();
            threads[j].interrupt();
        }
        rs1.close();
        s1.close();
        conn.close();
        return;
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
    }
    return;
}

From source file:net.ontopia.topicmaps.cmdlineutils.rdbms.RDBMSIndexTool.java

protected static Map getPrimaryKeys(String table_name, DatabaseMetaData dbm) throws SQLException {
    // returns { table_name(colname,...) : index_name }
    Map result = new HashMap(5);
    ResultSet rs = dbm.getPrimaryKeys(null, null, table_name);
    String prev_index_name = null;
    String columns = null;/*from  w w  w.  j  av  a2  s .  c o m*/

    while (rs.next()) {
        String index_name = rs.getString(6);
        if (prev_index_name != null && !prev_index_name.equals(index_name)) {
            result.put(table_name + '(' + columns + ')', prev_index_name);
            columns = null;
        }
        // column_name might be quoted, so unquote it before proceeding
        String column_name = unquote(rs.getString(4), dbm.getIdentifierQuoteString());
        if (columns == null)
            columns = column_name;
        else
            columns = columns + "," + column_name;

        prev_index_name = index_name;
    }
    rs.close();

    if (prev_index_name != null)
        result.put(table_name + '(' + columns + ')', prev_index_name);

    return result;
}

From source file:net.ontopia.topicmaps.cmdlineutils.rdbms.RDBMSIndexTool.java

protected static Map getIndexes(String table_name, DatabaseMetaData dbm) throws SQLException {
    // returns { table_name(colname,...) : index_name }
    Map result = new HashMap(5);
    ResultSet rs = dbm.getIndexInfo(null, null, table_name, false, false);
    String prev_index_name = null;
    String columns = null;/*from  w  w  w  .j a v a 2 s.c om*/

    while (rs.next()) {
        String index_name = rs.getString(6);

        if (prev_index_name != null && !prev_index_name.equals(index_name)) {
            result.put(table_name + '(' + columns + ')', prev_index_name);
            columns = null;
        }
        // column_name might be quoted, so unquote it before proceeding
        String column_name = unquote(rs.getString(9), dbm.getIdentifierQuoteString());

        if (columns == null)
            columns = column_name;
        else
            columns = columns + "," + column_name;

        prev_index_name = index_name;
    }
    rs.close();

    if (prev_index_name != null)
        result.put(table_name + '(' + columns + ')', prev_index_name);

    return result;
}

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

public static void checkSQLBinaryTableContent(String[] expected, String tableName, Connection connection) {
    Statement stmt = null;/*  ww w  .ja  v  a2  s.co m*/
    ResultSet rs = null;
    try {
        stmt = connection.createStatement();
        rs = stmt.executeQuery("SELECT TOP 1 [b1], [b2] FROM " + tableName);
        rs.next();
        assertEquals(expected[0], rs.getString("b1"));
        assertEquals(expected[1], rs.getString("b2"));
    } catch (SQLException e) {
        LOG.error("Can't verify table content", 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:com.portfolio.data.utils.DomUtils.java

public static String readXmlString(Connection connexion, String id, StringBuffer outTrace) throws Exception {
    //  ---------------------------------------------------
    PreparedStatement ps = null;/* www.  j a  v a 2s. c  o m*/
    ResultSet rs = null;
    String reqSQL = null;
    String xmlString = "";
    try {
        reqSQL = "SELECT xml FROM tree where id=" + id;
        ps = (PreparedStatement) connexion.prepareStatement(reqSQL);
        rs = ps.executeQuery();
        if (rs.next()) {
            xmlString = rs.getString(1);
        }
    } catch (Exception e) {
        outTrace.append("Erreur readXmlString:  (req=" + reqSQL + "  error:" + e);
    } finally {
        rs.close();
        ps.close();
        return xmlString;
    }
}

From source file:gridool.db.helpers.GridDbUtils.java

/**
 * @return column position is not provided in the returning foreign keys
 *//*from  w ww  .  ja v a2 s.co  m*/
@Nonnull
public static Collection<ForeignKey> getExportedKeys(@Nonnull final Connection conn,
        @Nullable final String pkTableName, final boolean setColumnPositions) throws SQLException {
    DatabaseMetaData metadata = conn.getMetaData();
    String catalog = conn.getCatalog();
    final Map<String, ForeignKey> mapping = new HashMap<String, ForeignKey>(4);
    final ResultSet rs = metadata.getExportedKeys(catalog, null, pkTableName);
    try {
        while (rs.next()) {
            final String fkName = rs.getString("FK_NAME");
            ForeignKey fk = mapping.get(fkName);
            if (fk == null) {
                String fkTableName = rs.getString("FKTABLE_NAME");
                fk = new ForeignKey(fkName, fkTableName, pkTableName);
                mapping.put(fkName, fk);
            }
            fk.addColumn(rs, metadata);
        }
    } finally {
        rs.close();
    }
    final Collection<ForeignKey> fkeys = mapping.values();
    if (setColumnPositions) {
        for (ForeignKey fk : fkeys) {
            fk.setColumnPositions(metadata);
        }
    }
    return fkeys;
}