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.microsoft.sqlserver.jdbc.connection.PoolingTest.java

/**
 * setup connection, get connection from pool, and test threads
 * //ww w  . j  a va 2 s.c o m
 * @param ds
 * @throws SQLException
 */
private static void connect(DataSource ds) throws SQLException {
    Connection con = null;
    PreparedStatement pst = null;
    ResultSet rs = null;

    try {
        con = ds.getConnection();
        pst = con.prepareStatement("SELECT SUSER_SNAME()");
        pst.setQueryTimeout(5);
        rs = pst.executeQuery();

        // TODO : we are commenting this out due to AppVeyor failures. Will investigate later.
        // assertTrue(countTimeoutThreads() >= 1, "Timeout timer is missing.");

        while (rs.next()) {
            rs.getString(1);
        }
    } finally {
        if (rs != null) {
            rs.close();
        }

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

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

From source file:org.springside.modules.test.data.H2Fixtures.java

/**
 * ,excludeTables.disable.//from   w  w w  . j av  a  2s .  co m
 */
public static void deleteAllTable(DataSource dataSource, String... excludeTables) {

    List<String> tableNames = new ArrayList<String>();
    ResultSet rs = null;
    try {
        rs = dataSource.getConnection().getMetaData().getTables(null, null, null, new String[] { "TABLE" });
        while (rs.next()) {
            String tableName = rs.getString("TABLE_NAME");
            if (Arrays.binarySearch(excludeTables, tableName) < 0) {
                tableNames.add(tableName);
            }
        }

        deleteTable(dataSource, tableNames.toArray(new String[tableNames.size()]));
    } catch (SQLException e) {
        Exceptions.unchecked(e);
    } finally {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

}

From source file:com.jaspersoft.jasperserver.api.engine.common.virtualdatasourcequery.VirtualSQLDataSource.java

public static Set discoverCatalogs(Connection conn) throws SQLException {
    DatabaseMetaData dbMetaData = conn.getMetaData();
    ResultSet rs = null;
    try {//from w  w w  .ja v  a2s.  c o m
        Set set = new LinkedHashSet();
        rs = dbMetaData.getCatalogs();
        while (rs.next()) {
            String catalog = rs.getString(TABLE_CAT);
            set.add(catalog);
        }
        return set;
    } catch (SQLException ex) {
        log.error("Cannot get catalogs", ex);
        throw ex;
    } finally {
        if (rs != null) {
            try {
                rs.close();
            } catch (Exception ex) {
            }
        }
    }
}

From source file:org.zenoss.zep.dao.impl.DaoUtils.java

/**
 * Returns a map of column names to their JDBC type in the specified table. The map is returned in the order
 * returned by the getColumns query./*from   ww w. ja  va 2 s  . c  o m*/
 *
 * @param dataSource DataSource to use.
 * @param tableName Table name.
 * @return A map of column names to the column types in the specified table.
 * @throws MetaDataAccessException If an exception occurs.
 */
public static Map<String, Integer> getColumnNamesAndTypes(final DataSource dataSource, final String tableName)
        throws MetaDataAccessException {
    final Map<String, Integer> columnNamesToTypes = new LinkedHashMap<String, Integer>();
    JdbcUtils.extractDatabaseMetaData(dataSource, new DatabaseMetaDataCallback() {
        @Override
        public Object processMetaData(DatabaseMetaData dbmd) throws SQLException, MetaDataAccessException {
            ResultSet rs = dbmd.getColumns(null, null, tableName, null);
            while (rs.next()) {
                String columnName = rs.getString("COLUMN_NAME");
                int columnType = rs.getInt("DATA_TYPE");
                columnNamesToTypes.put(columnName, columnType);
            }
            rs.close();
            return null;
        }
    });
    return columnNamesToTypes;
}

From source file:com.stratelia.webactiv.util.DBUtil.java

public static void close(ResultSet rs, Statement st) {
    if (rs != null) {
        try {//from www .  ja v a  2 s  .com
            rs.close();
        } catch (SQLException e) {
            SilverTrace.error("util", "DBUtil.close", "util.CAN_T_CLOSE_RESULTSET", e);
        }
    }
    if (st != null) {
        try {
            st.close();
        } catch (SQLException e) {
            SilverTrace.error("util", "DBUtil.close", "util.CAN_T_CLOSE_STATEMENT", e);
        }
    }
}

From source file:com.winit.vms.base.db.mybatis.support.SQLHelp.java

/**
 * //  ww  w .ja  v  a 2  s . co  m
 *
 * @param sql             SQL?
 * @param mappedStatement mapped
 * @param parameterObject ?
 * @param boundSql        boundSql
 * @param dialect         database dialect
 * @return 
 * @throws java.sql.SQLException sql
 */
public static int getCount(final String sql, final MappedStatement mappedStatement,
        final Object parameterObject, final BoundSql boundSql, Dialect dialect) throws SQLException {
    final String count_sql = dialect.getCountString(sql);
    logger.debug("Total count SQL [{}] ", count_sql);
    logger.debug("Total count Parameters: {} ", parameterObject);

    DataSource dataSource = mappedStatement.getConfiguration().getEnvironment().getDataSource();
    Connection connection = DataSourceUtils.getConnection(dataSource);
    PreparedStatement countStmt = null;
    ResultSet rs = null;
    try {
        countStmt = connection.prepareStatement(count_sql);
        //Page SQLCount SQL???boundSql
        DefaultParameterHandler handler = new DefaultParameterHandler(mappedStatement, parameterObject,
                boundSql);
        handler.setParameters(countStmt);

        rs = countStmt.executeQuery();
        int count = 0;
        if (rs.next()) {
            count = rs.getInt(1);
        }
        logger.debug("Total count: {}", count);
        return count;
    } finally {
        try {
            if (rs != null) {
                rs.close();
            }
        } finally {
            try {
                if (countStmt != null) {
                    countStmt.close();
                }
            } finally {
                DataSourceUtils.releaseConnection(connection, dataSource);
            }
        }
    }
}

From source file:com.concursive.connect.web.modules.profile.utils.ProjectUtils.java

public static int retrieveWebcastIdFromProjectId(Connection db, int projectId) throws SQLException {
    int webcastId = -1;
    PreparedStatement pst = db// w w w .j  ava2 s  . c  o  m
            .prepareStatement("SELECT webcast_id " + "FROM project_webcast " + "WHERE project_id = ? ");
    pst.setInt(1, projectId);
    ResultSet rs = pst.executeQuery();
    if (rs.next()) {
        webcastId = rs.getInt("webcast_id");
    }
    rs.close();
    pst.close();

    return webcastId;
}

From source file:dbcount.DBCountPageView.java

private static boolean verify(final DBMapReduceJobConf jobConf, final long totalPageview) throws SQLException {
    //check total num pageview
    String dbUrl = jobConf.getReduceOutputDbUrl();
    final Connection conn;
    try {//from   www.j  a v a  2 s .  co  m
        conn = jobConf.getConnection(dbUrl, true);
    } catch (ClassNotFoundException e) {
        throw new IllegalStateException(e);
    }
    String sumPageviewQuery = "SELECT SUM(pageview) FROM Pageview";
    Statement st = null;
    ResultSet rs = null;
    try {
        st = conn.createStatement();
        rs = st.executeQuery(sumPageviewQuery);
        rs.next();
        long sumPageview = rs.getLong(1);

        LOG.info("totalPageview=" + totalPageview);
        LOG.info("sumPageview=" + sumPageview);

        return totalPageview == sumPageview && totalPageview != 0;
    } finally {
        if (st != null) {
            st.close();
        }
        if (rs != null) {
            rs.close();
        }
        conn.close();
    }
}

From source file:marytts.util.io.FileUtils.java

/**
 *  Close a PreparedStatement and a series of result sets.
 * Use this in a finally clause./* w  w  w .ja va2s . c o  m*/
 * While closing the PreparedStatement supposedly closes it's resultsets,
 * i was told that some buggy implementations don't.
 * Exists because PreparedStatement and Resultet are only closeable on jdk 1.7
 */
public static void close(PreparedStatement ps, ResultSet... rs) {
    for (ResultSet c : rs) {
        if (c != null) {
            try {
                c.close();
            } catch (Exception ex) {
                MaryUtils.getLogger(FileUtils.class.getName()).log(Level.WARN, "Couldn't close ResultSet.", ex);
            }
        }
    }
    if (ps != null) {
        try {
            ps.close();
        } catch (Exception ex) {
            MaryUtils.getLogger(FileUtils.class.getName()).log(Level.WARN, "Couldn't close PreparedStatement.",
                    ex);
        }
    }
}

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

public static void assertRowCount(long expected, String tableName, Connection connection) {
    Statement stmt = null;//from w  w  w  .  j a va 2 s  .  c  o  m
    ResultSet rs = null;
    try {
        stmt = connection.createStatement();
        rs = stmt.executeQuery("SELECT count(*) FROM " + tableName);
        rs.next();
        assertEquals(expected, rs.getLong(1));
    } catch (SQLException e) {
        LOG.error("Can't verify number of rows", 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.");
        }
    }
}