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.thoughtworks.go.server.database.H2Database.java

public void startDatabase() {
    if (systemEnvironment.inDbDebugMode()) {
        if (tcpServer != null) {
            return;
        }//ww w.  ja v  a2  s. c  om
        try {
            DataSource ds = createDataSource();
            Connection con = ds.getConnection();
            ResultSet set = con.getMetaData().getTables(null, null, null, null);
            set.next();
            set.close();
            con.close();
            LOG.info("Database is already running.");
            return;
        } catch (Exception e) {
            LOG.info("Database is not running - starting a new one.");
        }
        try {
            LOG.info("Starting h2 server in debug mode : " + "port=" + configuration.getPort() + " baseDir="
                    + systemEnvironment.getDbPath().getCanonicalPath());
            String[] args = { "-tcp", "-tcpAllowOthers", "-tcpPort", String.valueOf(configuration.getPort()),
                    "-baseDir", systemEnvironment.getDbPath().getCanonicalPath() };
            tcpServer = Server.createTcpServer(args);
            tcpServer.start();
        } catch (Exception e) {
            bomb("Could not create database server.", e);
        }
    }
}

From source file:com.taobao.tddl.jdbc.group.integration.CRUDTest.java

    () throws Exception { //
   DataSource ds1 = DataSourceFactory.getMySQLDataSource(1);
   DataSource ds2 = DataSourceFactory.getMySQLDataSource(2);
   DataSource ds3 = DataSourceFactory.getMySQLDataSource(3);
      /*from   w  w w. j av a2  s  .  co m*/
   //db3db2db1
   TGroupDataSource ds = new TGroupDataSource();
   DataSourceWrapper dsw1 = new DataSourceWrapper("db1", "w", ds1, DBType.MYSQL);
   DataSourceWrapper dsw2 = new DataSourceWrapper("db2", "r20", ds2, DBType.MYSQL);
   DataSourceWrapper dsw3 = new DataSourceWrapper("db3", "r30", ds3, DBType.MYSQL);
   ds.init(dsw1, dsw2, dsw3);
   Connection conn = ds.getConnection();

   Statement stmt = conn.createStatement();
   assertEquals(stmt.executeUpdate("insert into crud(f1,f2) values(100,'str')"), 1);
      
   //
   //
   //db2,db3rs.next()false
   ResultSet rs = stmt.executeQuery("select f1,f2 from crud where f1=100");
   assertFalse(rs.next());
   rs.close();

   assertEquals(stmt.executeUpdate("delete from crud where f1=100"), 1);
   stmt.close();

   conn.close();
}

From source file:com.mtgi.analytics.jmx.StatisticsMBeanEventPersisterTest.java

private int countRecords() throws SQLException {
    Statement stmt = conn.createStatement();
    try {//w ww  .  j  a v  a2 s  . c  o m
        ResultSet rs = stmt.executeQuery("select count(*) from Data");
        try {
            rs.next();
            return rs.getInt(1);
        } finally {
            rs.close();
        }
    } finally {
        stmt.close();
    }
}

From source file:com.taobao.tddl.jdbc.group.integration.SpringTest.java

@Test
public void springTest() throws Exception {

    Connection conn = ds.getConnection();

    //Statementcrud
    Statement stmt = conn.createStatement();
    assertEquals(stmt.executeUpdate("insert into crud(f1,f2) values(10,'str')"), 1);
    assertEquals(stmt.executeUpdate("update crud set f2='str2'"), 1);
    ResultSet rs = stmt.executeQuery("select f1,f2 from crud");
    rs.next();/*from w ww .jav a2s. c o  m*/
    assertEquals(rs.getInt(1), 10);
    assertEquals(rs.getString(2), "str2");
    assertEquals(stmt.executeUpdate("delete from crud"), 1);
    rs.close();
    stmt.close();

    //PreparedStatementcrud
    String sql = "insert into crud(f1,f2) values(10,'str')";
    PreparedStatement ps = conn.prepareStatement(sql);
    assertEquals(ps.executeUpdate(), 1);
    ps.close();

    sql = "update crud set f2='str2'";
    ps = conn.prepareStatement(sql);
    assertEquals(ps.executeUpdate(), 1);
    ps.close();

    sql = "select f1,f2 from crud";
    ps = conn.prepareStatement(sql);
    rs = ps.executeQuery();
    rs.next();
    assertEquals(rs.getInt(1), 10);
    assertEquals(rs.getString(2), "str2");
    rs.close();
    ps.close();

    sql = "delete from crud";
    ps = conn.prepareStatement(sql);
    assertEquals(ps.executeUpdate(), 1);
    ps.close();

    conn.close();
}

From source file:net.mms_projects.copy_it.api.http.pages.v1.UserProfile.java

public FullHttpResponse onGetRequest(HttpRequest request, Database database, HeaderVerifier headerVerifier)
        throws Exception {
    PreparedStatement statement = database.getConnection().prepareStatement(SELECT_USER);
    statement.setInt(1, headerVerifier.getUserId());
    ResultSet result = statement.executeQuery();
    if (result.first()) {
        final JSONObject json = new JSONObject();
        json.put(ID, result.getInt(ID));
        json.put(EMAIL, result.getString(EMAIL));
        json.put(SIGNED_UP, result.getInt(SIGNED_UP));
        result.close();
        return new DefaultFullHttpResponse(request.getProtocolVersion(), OK,
                Unpooled.copiedBuffer(json.toString(), CharsetUtil.UTF_8));
    }/* w w w . j av a2s.c om*/
    result.close();
    return null;
}

From source file:com.mtgi.analytics.sql.BehaviorTrackingDataSourceTest.java

@Test
public void testPreparedStatement() throws Exception {
    //test tracking through the prepared statement API, which should also
    //log parameters in the events.
    PreparedStatement stmt = conn.prepareStatement("insert into TEST_TRACKING values (?, ?, ?)");
    stmt.setLong(1, 1);/*from w  ww  . j  a v  a 2s  . c  o m*/
    stmt.setString(2, "hello");
    stmt.setObject(3, null, Types.VARCHAR);
    assertEquals(1, stmt.executeUpdate());

    //test support for batching.  each batch should log 1 event.
    stmt.setLong(1, 3);
    stmt.setString(2, "batch");
    stmt.setObject(3, "1", Types.VARCHAR);
    stmt.addBatch();

    stmt.setLong(1, 4);
    stmt.setString(2, "batch");
    stmt.setObject(3, "2", Types.VARCHAR);
    stmt.addBatch();
    stmt.executeBatch();

    //back to a regular old update.
    stmt.setLong(1, 2);
    stmt.setObject(2, "goodbye", Types.VARCHAR);
    stmt.setNull(3, Types.VARCHAR);
    assertEquals(1, stmt.executeUpdate());

    stmt = conn.prepareStatement("update TEST_TRACKING set DESCRIPTION = 'world'");
    assertEquals(4, stmt.executeUpdate());

    stmt = conn.prepareStatement("select ID from TEST_TRACKING order by ID");
    ResultSet rs = stmt.executeQuery();
    int index = 0;
    long[] keys = { 1L, 2L, 3L, 4L };
    while (rs.next())
        assertEquals(keys[index++], rs.getLong(1));
    rs.close();
    assertEquals(4, index);

    manager.flush();
    assertEventDataMatches("BehaviorTrackingDataSourceTest.testPreparedStatement-result.xml");
}

From source file:example.propertysource.MySqlPropertySourceApplicationTests.java

@Test
public void shouldProvideConfigurationThroughBean() throws SQLException {

    Connection connection = DriverManager.getConnection("jdbc:mysql://localhost?useSSL=false",
            databaseConfiguration.getUsername(), databaseConfiguration.getPassword());
    Statement statement = connection.createStatement();

    ResultSet resultSet = statement.executeQuery("SELECT CURRENT_USER();");

    assertThat(resultSet.next()).isTrue();
    log.info("Database user: Config({}), Reported by MySQL({})", databaseConfiguration.getUsername(),
            resultSet.getString(1));//w  w  w.ja v  a 2  s.c o  m

    resultSet.close();
    statement.close();
    connection.close();
}

From source file:ke.co.tawi.babblesms.server.persistence.accounts.StatusDAO.java

/**
* @see ke.co.tawi.babblesms.server.persistence.accounts.BabbleStatusDAO#getStatus(java.lang.String)
*//*  ww  w.j av  a  2 s . c  o m*/
@Override
public Status getStatus(String uuid) {
    Status status = null;

    try (Connection conn = dbCredentials.getConnection();
            PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM Status WHERE Uuid = ?;");) {
        pstmt.setString(1, uuid);
        ResultSet rset = pstmt.executeQuery();

        if (rset.next()) {
            status = beanProcessor.toBean(rset, Status.class);
        }

        rset.close();

    } catch (SQLException e) {
        logger.error("SQLException when getting Status with uuid: " + uuid);
        logger.error(ExceptionUtils.getStackTrace(e));
    }

    return status;
}

From source file:ke.co.tawi.babblesms.server.persistence.accounts.StatusDAO.java

/**
*
* @param description//w  ww  .j a  va 2s. com
* @return network
*
*/
@Override
public Status getStatusByName(String description) {
    Status status = null;

    try (Connection conn = dbCredentials.getConnection();
            PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM Status WHERE description = ?;");) {

        pstmt.setString(1, description);
        ResultSet rset = pstmt.executeQuery();

        if (rset.next()) {
            status = beanProcessor.toBean(rset, Status.class);
        }

        rset.close();

    } catch (SQLException e) {
        logger.error("SQLException when getting Status with description: " + description);
        logger.error(ExceptionUtils.getStackTrace(e));
    }

    return status;
}

From source file:com.searchcode.app.util.Helpers.java

public void closeQuietly(ResultSet resultSet) {
    try {/*w  w  w .  j  a  va 2 s.c o m*/
        resultSet.close();
    } catch (Exception ex) {
    }
}