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:Main.java

/**
 * /* w  ww .  jav a  2s .  c  om*/
 * @param conn
 * @param table
 * @param c_name
 */
public static void updateContentVersion(Connection conn, String table, String c_name) throws Exception {
    ResultSet rs;
    Statement s;
    int v;

    s = conn.createStatement();
    rs = s.executeQuery("SELECT c_version FROM " + table + " WHERE c_name = '" + c_name + "'");
    v = 1;

    if (rs.next()) {
        v = rs.getInt(1);
        v++;
    }

    rs.close();

    s.executeUpdate("UPDATE " + table + " SET c_version = " + v + " WHERE c_name = '" + c_name + "'");
    s.close();
}

From source file:com.netradius.hibernate.support.HibernateUtil.java

/**
 * Utilitiy method to close JDBC connections, statements and result sets.
 *
 * @param con the connection to close or null
 * @param stmt the statement to close or null
 * @param rs the result set to close or null
 *//* w  w  w.  j ava2  s  .  c  o  m*/
public static void close(Connection con, Statement stmt, ResultSet rs) {
    if (rs != null) {
        try {
            rs.close();
        } catch (SQLException x) {
            log.error("Error closing result set: " + x.getMessage(), x);
        }
    }
    if (stmt != null) {
        try {
            con.close();
        } catch (SQLException x) {
            log.error("Error closing statement: " + x.getMessage(), x);
        }
    }
    if (con != null) {
        try {
            con.close();
        } catch (SQLException x) {
            log.error("Error closing connection: " + x.getMessage(), x);
        }
    }
}

From source file:com.nabla.dc.server.handler.fixed_asset.Asset.java

static private int getAssetCostBeforeDisposal(final Connection conn, int assetId) throws SQLException {
    final PreparedStatement stmt = StatementFormat.prepare(conn,
            "SELECT SUM(amount) FROM fa_transaction WHERE fa_asset_id=? AND class='COST';", assetId);
    try {//from  ww w .j  a v a2  s. c  o  m
        final ResultSet rs = stmt.executeQuery();
        try {
            return rs.next() ? rs.getInt(1) : 0;
        } finally {
            rs.close();
        }
    } finally {
        stmt.close();
    }
}

From source file:com.nabla.dc.server.handler.fixed_asset.Asset.java

static private int getAssetDepreciationBeforeDisposal(final Connection conn, int assetId) throws SQLException {
    final PreparedStatement stmt = StatementFormat.prepare(conn,
            "SELECT SUM(amount) FROM fa_transaction WHERE fa_asset_id=? AND class='DEP';", assetId);
    try {// w ww.j  a v a2  s. co m
        final ResultSet rs = stmt.executeQuery();
        try {
            return rs.next() ? rs.getInt(1) : 0;
        } finally {
            rs.close();
        }
    } finally {
        stmt.close();
    }
}

From source file:com.afforess.nsdump.Test.java

public static void testRegionDump() throws IOException, SQLException {
    RegionsDump dump = new RegionsDump();
    dump.parse();//from  w  ww.j  a v a2  s. c  o m

    Connection conn = dump.getDatabaseConnection();
    PreparedStatement statement = conn.prepareStatement("SELECT (numnations) FROM regions");
    ResultSet result = statement.executeQuery();
    int total = 0, regions = 0;
    while (result.next()) {
        total += result.getInt(1);
        regions++;
    }
    System.out.println("Total nations: " + total);
    System.out.println("Total regions: " + regions);
    result.close();
    conn.close();

    File db = new File("./ns-db.h2.db");
    db.delete();
}

From source file:com.l2jfree.gameserver.instancemanager.RaidPointsManager.java

public static void init() {
    _list.clear();//from  ww w.  ja  va2s  .  co m

    Connection con = null;
    try {
        con = L2DatabaseFactory.getInstance().getConnection();
        PreparedStatement statement = con.prepareStatement("SELECT * FROM character_raid_points");
        ResultSet rset = statement.executeQuery();

        while (rset.next())
            getList(rset.getInt("charId")).put(rset.getInt("boss_id"), rset.getInt("points"));

        rset.close();
        statement.close();
    } catch (Exception e) {
        _log.warn("", e);
    } finally {
        L2DatabaseFactory.close(con);
    }
}

From source file:io.agi.framework.persistence.jdbc.JdbcUtil.java

/**
 * Execute a SQL query that returns data.
 *
 * @param dbUrl/*from ww w.  ja  v  a 2  s. co m*/
 * @param user
 * @param password
 * @param sql
 * @param cb
 */
public static void ExecuteQuery(String dbUrl, String user, String password, String sql, ResultSetCallback cb) {
    Connection c = null;
    Statement s = null;
    try {

        //            c = DriverManager.getConnection(dbUrl, user, password);
        c = GetConnection(dbUrl, user, password);

        //STEP 4: Execute a query
        s = c.createStatement();
        ResultSet rs = s.executeQuery(sql);

        if (cb != null) {
            cb.onResultSet(rs);
        }

        //STEP 6: Clean-up environment
        rs.close();
        s.close();
        c.close();
    } catch (SQLException se) {
        logger.error(se.toString(), se);
    } catch (Exception e) {
        logger.error(s.toString(), s);
    } finally {
        try {
            if (s != null)
                s.close();
        } catch (SQLException se2) {
            logger.error(se2.toString(), se2);
        }

        try {
            if (c != null)
                c.close();
        } catch (SQLException se) {
            logger.error(se.toString(), se);
        }

    }
}

From source file:com.concursive.connect.web.modules.upgrade.utils.UpgradeUtils.java

/**
 * Queries the database to see if the script has already been executed
 *
 * @param db      Description of the Parameter
 * @param version Description of the Parameter
 * @return The installed value//from   w  w w  .j  a  v  a 2  s.  c o  m
 * @throws java.sql.SQLException Description of the Exception
 */
public static boolean isInstalled(Connection db, String version) throws SQLException {
    boolean isInstalled = false;
    // Query the installed version
    PreparedStatement pst = db.prepareStatement(
            "SELECT script_version " + "FROM database_version " + "WHERE script_version = ? ");
    pst.setString(1, version);
    ResultSet rs = pst.executeQuery();
    if (rs.next()) {
        isInstalled = true;
    }
    rs.close();
    pst.close();
    return isInstalled;
}

From source file:com.nabla.wapp.server.database.Database.java

/**
 * Test if a table has any data// ww  w .ja v  a  2s. c o  m
 * @param conn      - database connection
 * @param tableName   - table name
 * @return success
 * @throws SQLException
 */
public static boolean isTableEmpty(final Connection conn, final String tableName) throws SQLException {
    final Statement stmt = conn.createStatement();
    try {
        final ResultSet rs = stmt.executeQuery("SELECT COUNT(*) FROM " + tableName + ";");
        try {
            return rs.next() && rs.getInt(1) == 0;
        } finally {
            rs.close();
        }
    } finally {
        stmt.close();
    }
}

From source file:edumsg.core.PostgresConnection.java

public static void disconnect(ResultSet rs, PreparedStatement statment, Connection conn) {
    if (rs != null) {
        try {/*from  w w  w.  j  a  v  a 2s.  co m*/
            rs.close();
        } catch (SQLException e) {
        }
    }
    if (statment != null) {
        try {
            statment.close();
        } catch (SQLException e) {
        }
    }
    if (conn != null) {
        try {
            conn.close();
        } catch (SQLException e) {
        }
    }

}