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.concursive.connect.web.modules.documents.dao.FileItemList.java

/**
 * Returns the number of fileItems that match the module and itemid
 *
 * @param db           Description of the Parameter
 * @param linkModuleId Description of the Parameter
 * @param linkItemId   Description of the Parameter
 * @return Description of the Return Value
 * @throws SQLException Description of the Exception
 *///from  w w w.  j  a va2s  .  c o m
public static int retrieveRecordCount(Connection db, int linkModuleId, int linkItemId) throws SQLException {
    int count = 0;
    PreparedStatement pst = db.prepareStatement("SELECT COUNT(*) as filecount " + "FROM project_files pf "
            + "WHERE pf.link_module_id = ? and pf.link_item_id = ? ");
    pst.setInt(1, linkModuleId);
    pst.setInt(2, linkItemId);
    ResultSet rs = pst.executeQuery();
    if (rs.next()) {
        count = rs.getInt("filecount");
    }
    rs.close();
    pst.close();
    return count;
}

From source file:com.thoughtworks.go.server.database.DatabaseFixture.java

public static Object[][] query(String query, H2Database h2Database) {
    BasicDataSource source = h2Database.createDataSource();
    Connection con = null;//from www. ja va 2 s.co m
    Statement stmt = null;
    ResultSet rs = null;
    try {
        con = source.getConnection();
        stmt = con.createStatement();
        rs = stmt.executeQuery(query);
        int columnCount = rs.getMetaData().getColumnCount();
        List<Object[]> objects = new ArrayList<>();
        while (rs.next()) {
            Object[] values = new Object[columnCount];
            for (int i = 0; i < values.length; i++) {
                values[i] = rs.getObject(i + 1);
            }
            objects.add(values);
        }
        return objects.toArray(new Object[0][0]);
    } catch (SQLException e) {
        throw new RuntimeException(e);
    } finally {
        try {
            assert stmt != null;
            stmt.close();
            con.close();
            assert rs != null;
            rs.close();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}

From source file:net.codjo.dataprocess.server.treatmenthelper.TreatmentHelper.java

private static List<TreatmentFragment> checkIntegrityRepositoryContent(Connection con) throws SQLException {
    List<TreatmentFragment> treatmentFragmentList = new ArrayList<TreatmentFragment>();
    Statement stmt = con.createStatement();
    try {/* w ww. j  a  va2 s  .c o  m*/
        ResultSet rs = stmt.executeQuery("select TREATMENT_ID, CONTENT from PM_REPOSITORY_CONTENT "
                + " where CONTENT not like '%</" + DataProcessConstants.TREATMENT_ENTITY_XML + ">%'");
        try {
            while (rs.next()) {
                String content = rs.getString("CONTENT");
                String contentFragment = content.substring(content.length() - LENGTH)
                        .replace(DataProcessConstants.SPECIAL_CHAR_REPLACER_N, " ")
                        .replace(DataProcessConstants.SPECIAL_CHAR_REPLACER_R, " ");
                treatmentFragmentList.add(new TreatmentFragment(rs.getString("TREATMENT_ID"), contentFragment));
            }
            return treatmentFragmentList;
        } finally {
            rs.close();
        }
    } finally {
        stmt.close();
    }
}

From source file:gridool.db.helpers.GridDbUtils.java

/**
 * @return column position is provided in the returning foreign keys
 *///from   w  w  w.  j  av a  2s.  co m
@Nonnull
public static Collection<ForeignKey> getForeignKeys(@Nonnull final Connection conn,
        @Nullable final String fkTableName, final boolean setColumnPositions) throws SQLException {
    DatabaseMetaData metadata = conn.getMetaData();
    String catalog = conn.getCatalog();
    final Map<String, ForeignKey> mapping = new HashMap<String, ForeignKey>(4);
    final ResultSet rs = metadata.getImportedKeys(catalog, null, fkTableName);
    try {
        while (rs.next()) {
            final String fkName = rs.getString("FK_NAME");
            ForeignKey fk = mapping.get(fkName);
            if (fk == null) {
                //String fkTableName = rs.getString("FKTABLE_NAME");
                String pkTableName = rs.getString("PKTABLE_NAME");
                fk = new ForeignKey(fkName, fkTableName, pkTableName);
                mapping.put(fkName, fk);
            }
            fk.addColumn(rs, metadata);
        }
    } finally {
        rs.close();
    }
    final Collection<ForeignKey> fkeys = mapping.values();
    if (setColumnPositions) {
        for (ForeignKey fk : fkeys) {
            fk.setColumnPositions(metadata);
        }
    }
    return fkeys;
}

From source file:ExecSQL.java

/**
     * Prints a result set./*from   w w  w  .j  a  v a  2s.c  o m*/
     * @param stat the statement whose result set should be printed
     */
    public static void showResultSet(Statement stat) throws SQLException {
        ResultSet result = stat.getResultSet();
        ResultSetMetaData metaData = result.getMetaData();
        int columnCount = metaData.getColumnCount();

        for (int i = 1; i <= columnCount; i++) {
            if (i > 1)
                System.out.print(", ");
            System.out.print(metaData.getColumnLabel(i));
        }
        System.out.println();

        while (result.next()) {
            for (int i = 1; i <= columnCount; i++) {
                if (i > 1)
                    System.out.print(", ");
                System.out.print(result.getString(i));
            }
            System.out.println();
        }
        result.close();
    }

From source file:com.wso2.raspberrypi.Util.java

public static String getValue(String key) {
    String value = null;/*from   ww  w .j a  v  a  2s  .  c om*/
    BasicDataSource ds = getBasicDataSource();

    Connection dbConnection = null;
    PreparedStatement prepStmt = null;
    ResultSet rs = null;
    try {
        dbConnection = ds.getConnection();
        prepStmt = dbConnection.prepareStatement("SELECT * FROM KV_PAIR WHERE k='" + key + "'");
        rs = prepStmt.executeQuery();

        while (rs.next()) {
            value = rs.getString("v");
        }
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        try {
            if (dbConnection != null) {
                dbConnection.close();
            }
            if (prepStmt != null) {
                prepStmt.close();
            }
            if (rs != null) {
                rs.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    return value;
}

From source file:com.dangdang.ddframe.rdb.sharding.jdbc.adapter.AbstractResultSetAdapter.java

@Override
public final void close() throws SQLException {
    SQLUtil.safeInvoke(resultSets, new ThrowableSQLExceptionMethod<ResultSet>() {
        @Override//ww w.  ja va2s  . c o m
        public void apply(final ResultSet object) throws SQLException {
            object.close();
        }
    });
    closed = true;
}

From source file:com.springsource.insight.plugin.jdbc.PoolingConnectionTest.java

@Test
public void testOperationCollection() throws SQLException {
    DataSourceConnectionFactory connFactory = new DataSourceConnectionFactory(dataSource);
    ObjectPool connPool = new GenericObjectPool();
    PoolableConnectionFactory poolFactory = new PoolableConnectionFactory(connFactory, connPool, null, null,
            false, true);/*from  w  ww. j a v  a 2  s . c om*/
    PoolingDataSource poolDs = new PoolingDataSource(poolFactory.getPool());
    String sql = "select * from appointment where owner = 'Agim'";
    Connection c = poolDs.getConnection();
    try {
        PreparedStatement ps = c.prepareStatement(sql);
        try {
            System.out.println("Prepared statement=" + ps.getClass());

            ResultSet rs = ps.executeQuery();
            rs.close();
        } finally {
            ps.close();
        }
    } finally {
        c.close();
    }

    ArgumentCaptor<Operation> opCaptor = ArgumentCaptor.forClass(Operation.class);
    verify(spiedOperationCollector, times(3)).enter(opCaptor.capture());

    List<Operation> ops = opCaptor.getAllValues();
    assertEquals("Mismatched number of operations", 3, ops.size());
    assertSourceCodeLocation("top-op", ops.get(0),
            "org.apache.commons.dbcp.PoolingDataSource$PoolGuardConnectionWrapper", "prepareStatement");
    assertSourceCodeLocation("mid-op", ops.get(1), "org.apache.commons.dbcp.DelegatingConnection",
            "prepareStatement");
    assertSourceCodeLocation("bottom-op", ops.get(2), "org.hsqldb.jdbc.jdbcConnection", "prepareStatement");
}

From source file:ems.util.DataHandler.java

public static String getBoothName(String boothNumber) {
    String sqlQuery = "select ifnull(booth_no,'') || ' - ' || ifnull(booth_name,'') "
            + "from booth_master where booth_no=" + boothNumber;
    Connection con = getConnection();
    Statement s = null;//from   ww w .j  a  v a  2  s  .co  m
    ResultSet rs = null;
    try {
        s = con.createStatement();
        rs = s.executeQuery(sqlQuery);
        while (rs.next()) {
            return rs.getString(1);
        }
    } catch (Exception e) {
        log.error("getBoothList: " + e.getMessage());
    } finally {
        try {
            if (rs != null) {
                rs.close();
            }
            if (s != null) {
                s.close();
            }
            if (con != null) {
                con.close();
            }
        } catch (SQLException ex) {
            log.error("getBoothList: " + ex.getMessage());
        }
    }
    return null;
}

From source file:com.concursive.connect.web.modules.documents.dao.FileFolder.java

/**
 * Description of the Method/*  w ww.j  av  a2s .co m*/
 *
 * @param db        Description of the Parameter
 * @param hierarchy Description of the Parameter
 * @param currentId Description of the Parameter
 * @throws SQLException Description of the Exception
 */
public static void buildHierarchy(Connection db, Map hierarchy, int currentId) throws SQLException {
    PreparedStatement pst = db.prepareStatement(
            "SELECT parent_id, subject, display " + "FROM project_folders " + "WHERE folder_id = ? ");
    pst.setInt(1, currentId);
    ResultSet rs = pst.executeQuery();
    int parentId = 0;
    String subject = null;
    int display = -1;
    if (rs.next()) {
        parentId = DatabaseUtils.getInt(rs, "parent_id");
        subject = rs.getString("subject");
        display = DatabaseUtils.getInt(rs, "display");
    }
    rs.close();
    pst.close();
    hierarchy.put(new Integer(currentId), new String[] { subject, String.valueOf(display) });
    if (parentId > -1) {
        FileFolder.buildHierarchy(db, hierarchy, parentId);
    }
}