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.manpowergroup.cn.icloud.util.Case0.java

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

    for (int i = 0; i < COUNT; ++i) {
        Connection conn = dataSource.getConnection();
        Statement stmt = conn.createStatement();
        ResultSet rs = stmt.executeQuery("SELECT 1");
        rs.close();
        stmt.close();/* ww w.j  ava2  s  . c  om*/
        conn.close();
    }
    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:com.alibaba.druid.pool.dbcp.TestIdleForKylin.java

public void test_idle() throws Exception {
    MockDriver driver = MockDriver.instance;

    // BasicDataSource dataSource = new BasicDataSource();
    DruidDataSource dataSource = new DruidDataSource();
    dataSource.setUrl("jdbc:mock:xxx");
    dataSource.setDriverClassName("com.alibaba.druid.mock.MockDriver");
    dataSource.setInitialSize(1);/*from w  w w.j  av a 2  s . c o  m*/
    dataSource.setMaxActive(10);
    dataSource.setMaxIdle(10);
    dataSource.setMinIdle(0);
    dataSource.setMinEvictableIdleTimeMillis(50000 * 1);
    dataSource.setTimeBetweenEvictionRunsMillis(500);
    dataSource.setTestWhileIdle(true);
    dataSource.setTestOnBorrow(false);
    dataSource.setValidationQuery("SELECT 1");

    {
        Connection conn = dataSource.getConnection();

        // Assert.assertEquals(dataSource.getInitialSize(), driver.getConnections().size());
        System.out.println("raw size : " + driver.getConnections().size());

        PreparedStatement stmt = conn.prepareStatement("SELECT 1");
        ResultSet rs = stmt.executeQuery();
        rs.close();
        stmt.close();

        conn.close();
        System.out.println("raw size : " + driver.getConnections().size());
    }

    {
        Connection conn = dataSource.getConnection();

        // Assert.assertEquals(dataSource.getInitialSize(), driver.getConnections().size());
        System.out.println("raw size : " + driver.getConnections().size());

        conn.close();
        System.out.println("raw size : " + driver.getConnections().size());
    }

    {
        int count = 4;
        Connection[] connections = new Connection[4];
        for (int i = 0; i < count; ++i) {
            connections[i] = dataSource.getConnection();
        }
        System.out.println("raw size : " + driver.getConnections().size());
        for (int i = 0; i < count; ++i) {
            connections[i].close();
        }
        System.out.println("raw size : " + driver.getConnections().size());

        System.out.println("----------sleep for evict");
        Thread.sleep(dataSource.getMinEvictableIdleTimeMillis() * 2);
        System.out.println("raw size : " + driver.getConnections().size());
    }

    System.out.println("----------raw close all connection");
    for (MockConnection rawConn : driver.getConnections()) {
        rawConn.close();
    }

    Thread.sleep(dataSource.getMinEvictableIdleTimeMillis() * 2);
    System.out.println("raw size : " + driver.getConnections().size());
    {
        Connection conn = dataSource.getConnection();
        System.out.println("raw size : " + driver.getConnections().size());
        conn.close();
        System.out.println("raw size : " + driver.getConnections().size());
    }

    dataSource.close();
}

From source file:ems.util.DataHandler.java

public static List<MyModel> getDashboardData(String reportType) {
    List<MyModel> myModels = new LinkedList<>();
    String sqlQuery = "";
    switch (reportType) {
    case "1":
        sqlQuery = Q_S_DASHBOARD_CAST_WISE;
        break;/*  w w w. ja  v a  2  s  .c om*/
    case "2":
        sqlQuery = Q_S_DASHBOARD_GENDER_WISE;
        break;
    case "3":
        sqlQuery = Q_S_DASHBOARD_COLOR_WISE;
        break;
    }
    Connection con = getConnection();
    Statement s = null;
    ResultSet rs = null;
    try {
        s = con.createStatement();
        rs = s.executeQuery(sqlQuery);
        while (rs.next()) {
            myModels.add(new MyModel(rs.getString(1), rs.getString(2)));
        }
    } catch (Exception e) {
        log.error("getDashboardData: " + e.getMessage());
    } finally {
        try {
            if (rs != null) {
                rs.close();
            }
            if (s != null) {
                s.close();
            }
            if (con != null) {
                con.close();
            }
        } catch (SQLException ex) {
            log.error("getDashboardData: " + ex.getMessage());
        }
    }
    return myModels;
}

From source file:com.modelmetrics.cloudconverter.engine.PicklistProviderSqlImpl.java

public List<String> getPicklistValues() throws Exception {

    log.debug("dirtconnection? " + connection.getClass().getName());

    Statement statement = connection.createStatement();

    //      String sql = (String) migrationContext.getPicklistFields()
    //            .get(current.getName());

    log.info("Picklist sql: " + sql);

    ResultSet rs = statement.executeQuery(sql);

    if (rs.getMetaData().getColumnCount() != 1) {
        rs.close();
        statement.close();//  www  . j av  a2s .co  m
        throw new RuntimeException(
                "Column count not right on picklist; should be 1 but is " + rs.getMetaData().getColumnCount());
    }
    ArrayList<String> values = new ArrayList<String>();

    while (rs.next()) {
        log.debug("picklist value is: " + rs.getString(1));
        values.add(rs.getString(1));
    }

    rs.close();
    statement.close();

    return values;

}

From source file:example.mysql.MySqlApplication.java

@PostConstruct
private void postConstruct() throws Exception {

    System.out.println("##########################");

    try (Connection connection = dataSource.getConnection();
            Statement statement = connection.createStatement()) {

        ResultSet resultSet = statement.executeQuery("SELECT CURRENT_USER();");
        resultSet.next();/*from w ww  .  j a  va2  s  .  c o  m*/

        System.out.println("Connection works with User: " + resultSet.getString(1));

        resultSet.close();
    }

    System.out.println("##########################");
}

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

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

    final int COUNT = 1000 * 1;
    for (int i = 0; i < COUNT; ++i) {
        Connection conn = dataSource.getConnection();
        PreparedStatement stmt = conn.prepareStatement("SELECT 1 FROM DUAL");
        ResultSet rs = stmt.executeQuery();
        rs.next();//from  w  w w .  j a  v  a  2  s  .c o  m
        rs.close();
        stmt.close();
        conn.close();
    }
    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:com.softberries.klerk.dao.GenericDao.java

/**
 * Closes all resources after execution//w w w .ja  v a  2 s.c o  m
 * 
 * @param con {@code Connection} object
 * @param stm {@code PreparedStatement} object
 * @param gk {@code ResultSet} object
 * @throws SQLException
 */
protected void close(Connection con, PreparedStatement stm, ResultSet gk) throws SQLException {
    if (gk != null) {
        gk.close();
    }
    if (stm != null) {
        stm.close();
    }
    if (con != null) {
        con.close();
    }
}

From source file:kr.co.bitnine.octopus.testutils.MemoryDatabaseTest.java

@Test
public void testImport() throws Exception {
    MemoryDatabase memDb = new MemoryDatabase("sample");
    memDb.start();/*from ww w .j  a v a  2 s. co  m*/

    final String jsonFileName = "/sample.json";
    memDb.importJSON(getClass(), jsonFileName);

    JSONParser jsonParser = new JSONParser();
    JSONArray tables = (JSONArray) jsonParser
            .parse(new InputStreamReader(getClass().getResourceAsStream(jsonFileName)));
    assertEquals(2, tables.size());

    Connection conn = memDb.getConnection();
    Statement stmt = conn.createStatement();

    JSONObject employee = (JSONObject) tables.get(0);
    ResultSet rs = stmt.executeQuery("SELECT * FROM \"employee\"");
    verifyTableEquals(employee, rs);
    rs.close();

    JSONObject team = (JSONObject) tables.get(1);
    rs = stmt.executeQuery("SELECT * FROM \"team\"");
    verifyTableEquals(team, rs);
    rs.close();

    stmt.close();
    conn.close();

    memDb.stop();
}

From source file:com.hangum.tadpole.rdb.core.util.bander.cubrid.CubridExecutePlanUtils.java

/**
 * cubrid execute plan//from  w  w  w . j a va2s. c  o m
 * 
 * @param userDB
 * @param sql
 * @return
 * @throws Exception
 */
public static String plan(UserDBDAO userDB, String sql) throws Exception {
    if (!sql.toLowerCase().startsWith("select")) {
        logger.error("[cubrid execute plan ]" + sql);
        throw new Exception("This statment not select. please check.");
    }
    Connection conn = null;
    ResultSet rs = null;
    PreparedStatement pstmt = null;

    try {
        Class.forName("cubrid.jdbc.driver.CUBRIDDriver");
        conn = DriverManager.getConnection(userDB.getUrl(), userDB.getUsers(), userDB.getPasswd());
        conn.setAutoCommit(false); //     auto commit? false  .      

        sql = StringUtils.trim(sql).substring(6);
        if (logger.isDebugEnabled())
            logger.debug("[qubrid modifying query]" + sql);
        sql = "select " + RECOMPILE + sql;

        pstmt = conn.prepareStatement(sql);
        ((CUBRIDStatement) pstmt).setQueryInfo(true);
        rs = pstmt.executeQuery();

        String plan = ((CUBRIDStatement) pstmt).getQueryplan(); //  ?    .
        conn.commit();

        if (logger.isDebugEnabled())
            logger.debug("cubrid plan text : " + plan);

        return plan;

    } finally {
        if (rs != null)
            rs.close();
        if (pstmt != null)
            pstmt.close();
        if (conn != null)
            conn.close();
    }
}

From source file:com.dynamobi.ws.util.DB.java

@SuppressWarnings(value = { "unchecked" })
public static <T extends DBLoader> void execute(String query, T obj, List<T> list) {
    Connection conn = null;// w  w  w  .j  av  a 2 s .c o m
    PreparedStatement ps = null;
    ResultSet rs = null;

    try {
        conn = getConnection();
        ps = conn.prepareStatement(query);
        ps.setMaxRows(0);
        if (ps.execute()) {
            rs = ps.getResultSet();
        }

        while (rs != null && rs.next()) {
            obj.loadRow(rs);
            if (list != null) {
                list.add((T) obj.copy());
            }
        }
        obj.finalize();
    } catch (SQLException ex) {
        obj.exception(ex);
    } catch (Exception ex) {
        ex.printStackTrace();
    } finally {
        if (conn != null) {
            releaseConnection();
        }
        try {
            if (ps != null) {
                ps.close();
            }
        } catch (SQLException ex1) {
            ex1.printStackTrace();
        }
        try {
            if (rs != null) {
                rs.close();
            }
        } catch (SQLException ex3) {
            ex3.printStackTrace();
        }
    }
}