Example usage for java.sql ResultSet getObject

List of usage examples for java.sql ResultSet getObject

Introduction

In this page you can find the example usage for java.sql ResultSet getObject.

Prototype

Object getObject(String columnLabel) throws SQLException;

Source Link

Document

Gets the value of the designated column in the current row of this ResultSet object as an Object in the Java programming language.

Usage

From source file:com.enhype.photo.FlickrService.java

public List<String> getPhotoOwners() {

    String queryStr = "select img_owner from pictures p,quotes q where p.selected = true and q.selected = true and p.query = q.topic and p.query <> 'Hong_Kong';";
    List<String> userIds = new ArrayList<String>();

    java.sql.ResultSet result = db.execSelect(queryStr);

    try {/*  ww  w  . j  ava2s  .  co  m*/
        while (result.next()) {
            String user = (String) result.getObject("img_owner");
            userIds.add(user);
        }
    } catch (SQLException ex) {
        System.err.println("SQL Exception: ");
    } finally {
        db.closeResultSet(result);
    }
    return userIds;

}

From source file:gov.nih.nci.ncicb.cadsr.bulkloader.util.excel.DBExcelUtility.java

public static void debugResultSet(ResultSet rs) throws Exception {
    int maxcol = 20;
    StringBuffer sb = new StringBuffer();
    ResultSetMetaData mdata = rs.getMetaData();

    sb.append("\n");
    maxcol = mdata.getColumnCount();//from w  w w  .  ja v a 2  s . co  m
    System.out.println("Total Columns : " + maxcol);

    for (int i = 1; i <= maxcol; i++) {
        sb.append(mdata.getColumnName(i) + "\t");
    }

    int rowcount = 0;

    while (rs.next()) {
        sb.append("\n");
        ++rowcount;
        for (int i = 1; i <= maxcol; i++) {
            try {
                sb.append(rs.getObject(i) + "\t");
            } catch (Exception e) {
                System.out.println("Exception " + e);
            }
        }
    }
    System.out.println(sb.toString());
    System.out.println("Total Rows : " + rowcount);
}

From source file:dk.nsi.haiba.lprimporter.importer.ImportExecutorTest.java

@Test
public void test3800() {
    // simulate fgr importer
    haibaJdbc.update(/*from www  .  ja  va  2s  .  c  o m*/
            "INSERT INTO Class_SHAK (Nummer, Navn, Organisationstype, CreatedDate, ValidFrom, ValidTo) VALUES ('3800999', 'TST Testafdeling', 'test', '2009-01-01', '2009-01-01', '2045-01-01')");
    haibaJdbc.update(
            "INSERT INTO Class_SHAK (Nummer, Organisationstype, Ejerforhold,Institutionsart,Regionskode, CreatedDate, ValidFrom, ValidTo) VALUES ('3800', 'test', 'Ejerforhold','Institutionsart','Regionskode', '2009-01-01', '2009-01-01', '2045-01-01')");
    // then carecom
    lprJdbc.update(
            "INSERT INTO T_ADM (V_RECNUM, C_SGH, C_AFD, C_PATTYPE, V_CPR, D_INDDTO, D_UDDTO) VALUES (12345, '3800', '999', '1', '1111111111', '2013-01-10', '2013-01-14')");
    assertTrue(haibaJdbc.queryForInt("select count(*) from Class_dynamic_SHAK") == 0);

    executor.doProcess(true);

    assertTrue(haibaJdbc.queryForInt("select count(*) from Class_dynamic_SHAK") == 1);
    //        String ejerforhold = haibaJdbc.queryForObject(
    //                "select Ejerforhold from Class_dynamic_SHAK where sygehuskode='3800TST' AND afdelingskode='999'",
    //                String.class);
    haibaJdbc.query("select * from Class_dynamic_SHAK", new RowMapper<String>() {
        @Override
        public String mapRow(ResultSet rs, int rowNum) throws SQLException {
            System.out.println(rs.getObject("sygehuskode"));
            System.out.println(rs.getObject("afdelingskode"));
            System.out.println(rs.getObject("Ejerforhold"));
            return "e";
        }
    });
    //        assertEquals("Ejerforhold", ejerforhold);
}

From source file:net.orpiske.ssps.common.dependencies.cache.MultiRsHandler.java

@Override
protected DependencyCacheDto handleRow(ResultSet rs) throws SQLException {
    DependencyCacheDto dto = new DependencyCacheDto();

    ResultSetMetaData meta = rs.getMetaData();

    for (int i = 1; i <= meta.getColumnCount(); i++) {
        Object value = rs.getObject(i);
        String name = meta.getColumnName(i);

        try {//from  w  ww  .j av a2s  .com
            /*
             * We convert the column name to a more appropriate and java like name 
             * because some columns are usually named as some_thing whereas Java 
             * properties are named someThing. This call does this conversion.
             */
            String javaProperty = NameConverter.sqlToProperty(name);

            if (javaProperty.equals("version")) {
                Version version = Version.toVersion((String) value);

                PropertyUtils.setSimpleProperty(dto, javaProperty, version);
            } else {
                PropertyUtils.setSimpleProperty(dto, javaProperty, value);
            }
        } catch (Exception e) {
            throw new SQLException("Unable to set property " + name + " for bean" + dto.getClass(), e);
        }
    }

    return dto;
}

From source file:com.linkage.community.schedule.dbutils.CustomScalarHandler.java

/**
 * Returns one <code>ResultSet</code> column as an object via the
 * <code>ResultSet.getObject()</code> method that performs type
 * conversions./*from w w w  .  ja va2 s. c  o m*/
 * @param rs <code>ResultSet</code> to process.
 * @return The column or <code>null</code> if there are no rows in
 * the <code>ResultSet</code>.
 *
 * @throws SQLException if a database access error occurs
 * @throws ClassCastException if the class datatype does not match the column type
 *
 * @see org.apache.commons.dbutils.ResultSetHandler#handle(java.sql.ResultSet)
 */
// We assume that the user has picked the correct type to match the column
// so getObject will return the appropriate type and the cast will succeed.

@SuppressWarnings("unchecked")
//@Override
public T handle(ResultSet rs) throws SQLException {
    Object obj = null;
    if (rs.next()) {
        if (this.columnName == null) {
            obj = rs.getObject(this.columnIndex);
            if (obj instanceof Integer)
                return (T) (obj = rs.getInt(columnIndex));
            else if (obj instanceof Long)
                return (T) (obj = (new Long(rs.getLong(columnIndex)).intValue()));
            else if (obj instanceof Boolean)
                return (T) (obj = rs.getBoolean(columnIndex));
            else if (obj instanceof Double)
                return (T) (obj = rs.getDouble(columnIndex));
            else if (obj instanceof Float)
                return (T) (obj = rs.getFloat(columnIndex));
            else if (obj instanceof Short)
                return (T) (obj = rs.getShort(columnIndex));
            else if (obj instanceof Byte)
                return (T) (obj = rs.getByte(columnIndex));
            else
                return (T) obj;
        } else {
            obj = rs.getObject(this.columnName);
            if (obj instanceof Integer)
                return (T) (obj = rs.getInt(columnName));
            else if (obj instanceof Long)
                return (T) (obj = rs.getLong(columnName));
            else if (obj instanceof Boolean)
                return (T) (obj = rs.getBoolean(columnName));
            else if (obj instanceof Double)
                return (T) (obj = rs.getDouble(columnName));
            else if (obj instanceof Float)
                return (T) (obj = rs.getFloat(columnName));
            else if (obj instanceof Short)
                return (T) (obj = rs.getShort(columnName));
            else if (obj instanceof Byte)
                return (T) (obj = rs.getByte(columnName));
            else
                return (T) obj;
        }
    }
    return null;
}

From source file:guru.bubl.module.neo4j_user_repository.FriendManagerNeo4j.java

@Override
public FriendStatus getStatusWithUser(User otherUser) {
    return NoEx.wrap(() -> {
        String query = String.format("START user=node:node_auto_index('uri:%s') " + "with user "
                + "START friend=node:node_auto_index('uri:%s') "
                + "OPTIONAL MATCH (user)-[friendRequest:friend]->(friend) "
                + "OPTIONAL MATCH (friend)-[friendInvitation:friend]->(user) "
                + "RETURN friendRequest.status as friendRequestStatus, "
                + "friendInvitation.status as friendInvitationStatus", user.id(), otherUser.id());
        ResultSet rs = connection.createStatement().executeQuery(query);
        if (!rs.next()) {
            return FriendStatus.none;
        }/*from   w w w . j a v  a 2  s  . c o m*/
        Boolean isRequestUser = rs.getObject("friendRequestStatus") != null;

        if (!isRequestUser && rs.getObject("friendInvitationStatus") == null) {
            return FriendStatus.none;
        }

        FriendStatus friendStatus = isRequestUser ? FriendStatus.valueOf(rs.getString("friendRequestStatus"))
                : FriendStatus.valueOf(rs.getString("friendInvitationStatus"));

        if (friendStatus == FriendStatus.waiting && !isRequestUser) {
            return FriendStatus.waitingForYourAnswer;
        }
        return friendStatus;
    }).get();
}

From source file:net.orpiske.ssps.common.registry.MultiRsHandler.java

@Override
protected SoftwareInventoryDto handleRow(ResultSet rs) throws SQLException {
    SoftwareInventoryDto dto = new SoftwareInventoryDto();

    ResultSetMetaData meta = rs.getMetaData();

    for (int i = 1; i <= meta.getColumnCount(); i++) {
        Object value = rs.getObject(i);
        String name = meta.getColumnName(i);

        try {//from www  . j a  va 2 s  .  c  o m
            /*
             * We convert the column name to a more appropriate and java like name 
             * because some columns are usually named as some_thing whereas Java 
             * properties are named someThing. This call does this conversion.
             */
            String javaProperty = NameConverter.sqlToProperty(name);

            if (javaProperty.equals("version")) {
                Version version = Version.toVersion((String) value);

                PropertyUtils.setSimpleProperty(dto, javaProperty, version);
            } else {
                PropertyUtils.setSimpleProperty(dto, javaProperty, value);
            }
        } catch (Exception e) {
            throw new SQLException("Unable to set property " + name + " for bean" + dto.getClass(), e);
        }
    }

    return dto;
}

From source file:com.splicemachine.derby.impl.sql.execute.operations.CallStatementOperationIT.java

@Test
public void testCallSQLTABLES() throws Exception {
    CallableStatement cs = methodWatcher.prepareCall(
            "call SYSIBM.SQLTABLES(null,'SYS',null,'SYSTEM TABLE',null)", ResultSet.TYPE_FORWARD_ONLY,
            ResultSet.CONCUR_READ_ONLY);
    ResultSet rs = cs.executeQuery();
    int count = 0;
    while (rs.next()) {
        Object data = rs.getObject(2);
        count++;/*from w ww. ja  v  a2s . co  m*/
    }
    Assert.assertTrue("Incorrect rows returned!", count > 0);
    DbUtils.closeQuietly(rs);
}

From source file:com.splicemachine.derby.impl.sql.execute.operations.CallStatementOperationIT.java

@Test
@Category(SlowTest.class)
public void testCallSQLTABLESInAppSchema() throws Exception {
    CallableStatement cs = methodWatcher.prepareCall("call SYSIBM.SQLTABLES(null,'"
            + CallStatementOperationIT.class.getSimpleName().toUpperCase() + "',null,null,null)",
            ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
    ResultSet rs = cs.executeQuery();
    int count = 0;
    while (rs.next()) {
        Object data = rs.getObject(2);
        count++;//from w w  w  .j  a v  a  2  s.  c  om
    }
    Assert.assertTrue("Incorrect rows returned!", count > 0);
    DbUtils.closeQuietly(rs);
}

From source file:com.marvinformatics.hibernate.json.JsonUserType.java

@Override
public Object nullSafeGet(ResultSet rs, String[] names, SessionImplementor session, Object owner)
        throws HibernateException, SQLException {
    Object result = rs.getObject(names[0]);
    if (result instanceof PGobject)
        return convertJsonToObject(((PGobject) result).getValue());

    return null;//  w w  w .  ja v  a  2  s.co m
}