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.nabla.dc.server.handler.fixed_asset.Asset.java

static public void dispose(final Connection conn, final Integer assetId, final IDisposal disposal)
        throws SQLException, DispatchException {
    final PreparedStatement redo = conn
            .prepareStatement("INSERT INTO fa_transaction_redo (fa_asset_id, command) VALUES(?,?);");
    try {/*from   w ww .ja v a  2 s  . co  m*/
        redo.setInt(1, assetId);
        // backup transaction after disposal if any
        if (log.isDebugEnabled())
            log.debug("backing up transactions after disposal date");
        // charge monthly depreciation in disposal month if disposal is after 15
        final Calendar dt = Util.dateToCalendar(disposal.getDate());
        if (dt.get(GregorianCalendar.DAY_OF_MONTH) >= dt.getActualMaximum(GregorianCalendar.DAY_OF_MONTH) / 2)
            dt.add(GregorianCalendar.MONTH, 1);
        dt.set(GregorianCalendar.DAY_OF_MONTH, 1);
        final Date from = Util.calendarToSqlDate(dt);
        // get list of transactions to backup before we delete them
        final IntegerSet transIds = new IntegerSet();
        final PreparedStatement stmt = StatementFormat.prepare(conn,
                "SELECT t.*" + " FROM fa_transaction AS t INNER JOIN period_end AS p ON t.period_end_id=p.id"
                        + " WHERE t.fa_asset_id=? AND p.end_date>?;",
                assetId, from);
        try {
            final ResultSet rs = stmt.executeQuery();
            try {
                while (rs.next()) {
                    transIds.add(rs.getInt("id"));
                    final String command = MessageFormat.format("INSERT INTO fa_transaction"
                            + " (id,fa_asset_id,period_end_id,amount,class,type,depreciation_period)"
                            + " VALUES({0,number,0},{1,number,0},{2,number,0},{3,number,0},''{4}'',''{5}'',{6,number,0});",
                            rs.getInt("id"), rs.getInt("fa_asset_id"), rs.getInt("period_end_id"),
                            rs.getInt("amount"), rs.getString("class"), rs.getString("type"),
                            Database.getInteger(rs, "depreciation_period"));
                    if (log.isTraceEnabled())
                        log.trace("redo = " + command);
                    redo.setString(2, command);
                    redo.addBatch();
                }
            } finally {
                rs.close();
            }
        } finally {
            stmt.close();
        }
        // remove any transaction after disposal date
        if (log.isDebugEnabled())
            log.debug("removing transactions after disposal date");
        Database.executeUpdate(conn, "DELETE FROM fa_transaction WHERE id IN (?);", transIds);
        // add disposal transactions
        if (log.isDebugEnabled())
            log.debug("adding transactions for disposal");
        final TransactionList transactions = new TransactionList(assetId);
        // closing cost
        transactions.add(new Transaction(TransactionClasses.COST, TransactionTypes.CLOSING, disposal.getDate(),
                -1 * getAssetCostBeforeDisposal(conn, assetId)));
        // closing accumulated depreciation
        transactions.add(new Transaction(TransactionClasses.DEP, TransactionTypes.CLOSING, disposal.getDate(),
                -1 * getAssetDepreciationBeforeDisposal(conn, assetId)));
        for (Integer newTransId : transactions.save(conn, true)) {
            redo.setString(2,
                    MessageFormat.format("DELETE FROM fa_transaction WHERE id={0,number,0};", newTransId));
            redo.addBatch();
        }
        if (!Database.isBatchCompleted(redo.executeBatch()))
            throw new InternalErrorException("failed to save disposal transactions");
    } finally {
        redo.close();
    }
}

From source file:com.l2jfree.gameserver.datatables.SkillSpellbookTable.java

private SkillSpellbookTable() {
    _skillSpellbooks = new FastMap<Integer, Integer>().setShared(true);

    if (!Config.ALT_SP_BOOK_NEEDED)
        return;/*w ww  .ja va 2s  .c om*/

    Connection con = null;

    try {
        con = L2DatabaseFactory.getInstance().getConnection(con);
        PreparedStatement statement = con.prepareStatement("SELECT skill_id, item_id FROM skill_spellbooks");
        ResultSet spbooks = statement.executeQuery();

        while (spbooks.next())
            _skillSpellbooks.put(spbooks.getInt("skill_id"), spbooks.getInt("item_id"));

        spbooks.close();
        statement.close();

        _log.info("SkillSpellbookTable: Loaded " + _skillSpellbooks.size() + " Spellbooks.");
    } catch (Exception e) {
        _log.warn("Error while loading spellbook data: ", e);
    } finally {
        L2DatabaseFactory.close(con);
    }
}

From source file:id.go.kemdikbud.tandajasa.dao.PegawaiDaoTest.java

@Test
public void testHapus() throws Exception {
    DataSource ds = ctx.getBean(DataSource.class);
    Connection koneksiDatabase = ds.getConnection();

    PreparedStatement ps = koneksiDatabase.prepareStatement("select count(*) as jumlah from pegawai");
    ResultSet rsSebelum = ps.executeQuery();
    Assert.assertTrue(rsSebelum.next());
    Long jumlahRecordSebelum = rsSebelum.getLong(1);
    rsSebelum.close();

    PegawaiDao pd = (PegawaiDao) ctx.getBean("pegawaiDao");
    Pegawai p = pd.cariById(100);//from ww w  .j  av a  2 s.  c  om
    Assert.assertNotNull(p);
    pd.delete(p);

    ResultSet rsSetelah = ps.executeQuery();
    Assert.assertTrue(rsSetelah.next());
    Long jumlahRecordSetelah = rsSetelah.getLong("jumlah");
    rsSetelah.close();

    koneksiDatabase.close();
    Assert.assertEquals(new Long(jumlahRecordSebelum - 1), new Long(jumlahRecordSetelah));
}

From source file:com.alibaba.druid.benckmark.pool.Case_Concurrent_50.java

private void p0(final DataSource dataSource, String name) throws Exception {
    long startMillis = System.currentTimeMillis();
    long startYGC = TestUtil.getYoungGC();
    long startFullGC = TestUtil.getFullGC();

    final CountDownLatch endLatch = new CountDownLatch(THREAD_COUNT);
    for (int i = 0; i < THREAD_COUNT; ++i) {
        Thread thread = new Thread() {

            public void run() {
                try {

                    for (int i = 0; i < COUNT; ++i) {
                        Connection conn = dataSource.getConnection();
                        Statement stmt = conn.createStatement();
                        ResultSet rs = stmt.executeQuery("SELECT 1");
                        Thread.sleep(0, 1000 * 100);
                        rs.close();
                        stmt.close();//  ww  w .  ja va  2s. c o m
                        conn.close();
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    endLatch.countDown();
                }
            }
        };
        thread.start();
    }
    endLatch.await();

    long millis = System.currentTimeMillis() - startMillis;
    long ygc = TestUtil.getYoungGC() - startYGC;
    long fullGC = TestUtil.getFullGC() - startFullGC;

    System.out.println(name + " millis : " + NumberFormat.getInstance().format(millis) + ", YGC " + ygc
            + " FGC " + fullGC);
}

From source file:net.bitnine.agensgraph.test.ReturnTest.java

public void testReturn() throws Exception {
    ResultSet rs = st.executeQuery("RETURN 'be' || ' happy!', 1+1");
    while (rs.next()) {
        assertEquals("be happy!", rs.getString(1));
        assertEquals(2, rs.getInt(2));/*from w ww  . ja va2 s .c om*/
    }
    rs.close();
}

From source file:com.l2jfree.gameserver.datatables.RecordTable.java

private void load() {
    Connection con = null;/*from w w w  .  jav  a 2 s. co  m*/
    try {
        con = L2DatabaseFactory.getInstance().getConnection();

        PreparedStatement statement = con
                .prepareStatement("SELECT maxplayer, date FROM record ORDER BY maxplayer DESC LIMIT 1");
        ResultSet rset = statement.executeQuery();

        if (rset.next()) {
            _record = rset.getInt("maxplayer");
            _date = rset.getString("date");
        }

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

From source file:id.go.kemdikbud.tandajasa.dao.PegawaiDaoTest.java

@Test
public void testInsert() throws Exception {
    Golongan g = new Golongan();
    g.setId(99);//from   w ww.  ja  v a2s  . c  o  m

    Pegawai p = new Pegawai();
    p.setNip("123");
    p.setNama("Endy Muhardin");
    p.setGolongan(g);

    DataSource ds = ctx.getBean(DataSource.class);
    Connection koneksiDatabase = ds.getConnection();

    PreparedStatement ps = koneksiDatabase.prepareStatement("select count(*) as jumlah from pegawai");
    ResultSet rsSebelum = ps.executeQuery();
    Assert.assertTrue(rsSebelum.next());
    Long jumlahRecordSebelum = rsSebelum.getLong(1);
    rsSebelum.close();

    PegawaiDao pd = (PegawaiDao) ctx.getBean("pegawaiDao");
    pd.save(p);

    ResultSet rsSetelah = ps.executeQuery();
    Assert.assertTrue(rsSetelah.next());
    Long jumlahRecordSetelah = rsSetelah.getLong("jumlah");
    rsSetelah.close();

    koneksiDatabase.close();
    Assert.assertEquals(new Long(jumlahRecordSebelum + 1), new Long(jumlahRecordSetelah));
}

From source file:me.eccentric_nz.plugins.FatPort.FatPortCmdUtils.java

public boolean hasCommand(int pid) {
    boolean bool = false;
    try {/*  www. j  a va 2  s.  c om*/
        Connection connection = service.getConnection();
        Statement statement = connection.createStatement();
        String queryCmd = "SELECT c_id FROM commands WHERE p_id = " + pid;
        ResultSet rsCmd = statement.executeQuery(queryCmd);
        if (rsCmd.isBeforeFirst()) {
            bool = true;
        }
        rsCmd.close();
        statement.close();
    } catch (SQLException e) {
        plugin.debug("Could not check for command! " + e);
    }
    return bool;
}

From source file:com.hangum.tadpole.engine.sql.util.OracleObjectCompileUtils.java

/**
 * package compile//w  ww  .  j  a  v a  2  s.c  om
 * 
 * @param objectName
 * @param userDB
 */
public static String packageCompile(ProcedureFunctionDAO packageDao, UserDBDAO userDB, boolean isDebug)
        throws Exception {
    String withDebugOption = "";
    if (isDebug)
        withDebugOption = "DEBUG";

    String sqlQuery = "ALTER PACKAGE " + packageDao.getFullName(true/*isPackage*/) + " COMPILE " //$NON-NLS-1$//$NON-NLS-2$
            + withDebugOption + " SPECIFICATION "; //$NON-NLS-1$
    String sqlBodyQuery = "ALTER PACKAGE " + packageDao.getFullName(true/*isPackage*/) + " COMPILE " //$NON-NLS-1$//$NON-NLS-2$
            + withDebugOption + " BODY "; //$NON-NLS-1$

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

        statement = javaConn.createStatement();
        statement.execute(sqlQuery);
        statement.execute(sqlBodyQuery);

        sqlQuery = "Select * From all_Errors where owner = nvl('" + packageDao.getSchema_name() //$NON-NLS-1$
                + "', user) and name='" + packageDao.getName() //$NON-NLS-1$
                + "' and type in ('PACKAGE', 'PACKAGE BODY') order by type, sequence ";
        rs = statement.executeQuery(sqlQuery);
        StringBuffer result = new StringBuffer();
        while (rs.next()) {
            result.append(prettyMsg(rs.getString("line"), rs.getString("position"), rs.getString("text"))); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ 
        }
        return result.toString();
    } finally {
        try {
            rs.close();
        } catch (Exception e) {
        }
        try {
            statement.close();
        } catch (Exception e) {
        }
        try {
            javaConn.close();
        } catch (Exception e) {
        }

    }
}

From source file:br.edu.claudivan.controledegastos.dao.utils.DatabaseUtils.java

public void closeConnection(Connection dbConnection, ResultSet queryResultSet, PreparedStatement queryStatement)
        throws SQLException {
    if (queryStatement != null) {
        queryStatement.close();//from w ww  .ja  v a 2 s  .  c  o m
    }

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

    closeConnection(dbConnection);
}