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

public <T> T getObject(String columnLabel, Class<T> type) throws SQLException;

Source Link

Document

Retrieves the value of the designated column in the current row of this ResultSet object and will convert from the SQL type of the column to the requested Java data type, if the conversion is supported.

Usage

From source file:me.ronghai.sa.model.ModelMeta.java

public static Object get(ResultSet rs, String cname, Class<?> type) throws SQLException {
    if (type != null && type.equals(java.util.Date.class)) {
        type = java.sql.Date.class;
    }//from  www.  j  a va  2s  .  co m
    return rs.getObject(cname, type);
}

From source file:de.unibremen.informatik.tdki.combo.data.JdbcTemplate.java

public List queryForList(String query, final Class elementType) {
    try {//from w w  w  .j a  v a  2 s.c o m
        return qRunner.query(connection, query, new AbstractListHandler<Object>() {

            @Override
            protected Object handleRow(ResultSet rs) throws SQLException {
                return rs.getObject(1, elementType);
            }

        });
    } catch (SQLException ex) {
        if (ex.getErrorCode() == -204) {
            throw new DBObjectDoesNotExistException(ex);
        } else {
            throw new RuntimeException(ex);
        }
    }
}

From source file:be.bittich.dynaorm.maping.DynaRowProcessor.java

@Override
public <T> T toBean(ResultSet rs, Class<T> type) throws SQLException {
    T bean = null;/* w  w  w  .  j a v a  2  s.c o  m*/
    try {
        bean = type.newInstance();
        ColumnMapping mapping = IOCFacadGet.getColumnMapping();
        Map<String, Field> mapper = mapping.mapToSQLColumns(bean, tableColumn);
        for (String columnName : mapper.keySet()) {
            Field field = mapper.get(columnName);
            field.setAccessible(true);
            //get java Type
            String javaType = SQLTypeMap.convert(tableColumn.getColumns().get(columnName));
            Class<?> typeForValue = Class.forName(javaType);
            Object value = rs.getObject(columnName, typeForValue);
            field.set(bean, value);
        }
    } catch (InstantiationException | IllegalAccessException | ClassNotFoundException ex) {
        Logger.getLogger(DynaRowProcessor.class.getName()).log(Level.SEVERE, null, ex);
    }
    return bean;

}

From source file:com.micromux.cassandra.jdbc.JdbcRegressionTest.java

@Test
public void testObjectTimestamp() throws Exception {
    Statement stmt = con.createStatement();
    java.util.Date now = new java.util.Date();

    // Create the target Column family
    //String createCF = "CREATE COLUMNFAMILY t74 (id BIGINT PRIMARY KEY, col1 TIMESTAMP)";        
    String createCF = "CREATE COLUMNFAMILY t74 (id BIGINT PRIMARY KEY, col1 TIMESTAMP)";

    stmt.execute(createCF);/*  w ww  .j a v a  2  s  .co  m*/
    stmt.close();
    con.close();

    // open it up again to see the new CF
    con = DriverManager
            .getConnection(String.format("jdbc:cassandra://%s:%d/%s?%s", HOST, PORT, KEYSPACE, OPTIONS));

    Statement statement = con.createStatement();

    String insert = "INSERT INTO t74 (id, col1) VALUES (?, ?);";

    PreparedStatement pstatement = con.prepareStatement(insert);
    pstatement.setLong(1, 1L);
    pstatement.setObject(2, new Timestamp(now.getTime()), Types.TIMESTAMP);
    pstatement.execute();

    ResultSet result = statement.executeQuery("SELECT * FROM t74;");

    assertTrue(result.next());
    assertEquals(1L, result.getLong(1));

    // try reading Timestamp directly
    Timestamp stamp = result.getTimestamp(2);
    assertEquals(now, stamp);

    // try reading Timestamp as an object
    stamp = result.getObject(2, Timestamp.class);
    assertEquals(now, stamp);

    System.out.println(resultToDisplay(result, 74, "current date"));

}