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:cosmos.sql.TestSql.java

@Test
public void testLimit() throws SQLException {
    loadDriverClass();/*from   ww w .j a  v  a2 s.  c o  m*/
    Connection connection = null;
    Statement statement = null;
    try {
        Properties info = new Properties();
        info.put("url", JDBC_URL);
        info.put("user", USER);
        info.put("password", PASSWORD);
        connection = DriverManager.getConnection("jdbc:accumulo:cosmos//localhost", info);
        statement = connection.createStatement();
        final ResultSet resultSet = statement.executeQuery("select \"PAGE_ID\" from \"" + CosmosDriver.COSMOS
                + "\".\"" + meataData.uuid() + "\"  limit 2 OFFSET 0");
        final ResultSetMetaData metaData = resultSet.getMetaData();
        final int columnCount = metaData.getColumnCount();

        assertEquals(columnCount, 1);

        int resultsFound = 0;
        while (resultSet.next()) {
            assertEquals(metaData.getColumnName(1), "PAGE_ID");
            @SuppressWarnings("unchecked")
            List<Entry<Column, RecordValue<?>>> sValues = (List<Entry<Column, RecordValue<?>>>) resultSet
                    .getObject("PAGE_ID");
            assertEquals(sValues.size(), 1);
            RecordValue<?> onlyValue = sValues.iterator().next().getValue();
            assertEquals(onlyValue.visibility().toString(), "[en]");

            assertEquals(onlyValue.value(), Integer.valueOf(resultsFound).toString());
            resultsFound++;

        }

        assertEquals(resultsFound, 2);
    } finally {
        close(connection, statement);
    }
}

From source file:cosmos.sql.TestSql.java

@Test
public void testJoin() throws SQLException {
    loadDriverClass();/*ww  w.  j a v a 2  s . c  om*/
    Connection connection = null;
    Statement statement = null;
    try {
        Properties info = new Properties();
        info.put("url", JDBC_URL);
        info.put("user", USER);
        info.put("password", PASSWORD);
        connection = DriverManager.getConnection("jdbc:accumulo:cosmos//localhost", info);
        statement = connection.createStatement();
        final ResultSet resultSet = statement.executeQuery("select \"PAGE_ID\" from \"" + CosmosDriver.COSMOS
                + "\".\"" + meataData.uuid() + "\"  limit 2 OFFSET 0");
        final ResultSetMetaData metaData = resultSet.getMetaData();
        final int columnCount = metaData.getColumnCount();

        assertEquals(columnCount, 1);

        int resultsFound = 0;
        while (resultSet.next()) {
            assertEquals(metaData.getColumnName(1), "PAGE_ID");
            @SuppressWarnings("unchecked")
            List<Entry<Column, RecordValue<?>>> sValues = (List<Entry<Column, RecordValue<?>>>) resultSet
                    .getObject("PAGE_ID");
            assertEquals(sValues.size(), 1);
            RecordValue<?> onlyValue = sValues.iterator().next().getValue();
            assertEquals(onlyValue.visibility().toString(), "[en]");

            assertEquals(onlyValue.value(), Integer.valueOf(resultsFound).toString());
            resultsFound++;

        }

        assertEquals(resultsFound, 2);
    } finally {
        close(connection, statement);
    }
}

From source file:de.innovationgate.webgate.api.jdbc.custom.JDBCSource.java

/**
 * @param resultSet//from  ww w  .ja v  a2s.com
 * @return
 */
private Map extractRowData(ResultSet resultSet) throws SQLException {

    Map row = new HashMap();
    ResultSetMetaData rsMeta = resultSet.getMetaData();
    for (int idx = rsMeta.getColumnCount(); idx > 0; idx--) {
        Object value = resultSet.getObject(idx);
        row.put(rsMeta.getColumnLabel(idx).toLowerCase(), value);
        row.put(rsMeta.getColumnName(idx).toLowerCase(), value);
    }
    return row;

}

From source file:cosmos.sql.TestSql.java

@Test
public void testDisjunctionPositive() throws SQLException {
    loadDriverClass();//w  w  w .j av  a2 s.c o  m
    Connection connection = null;
    Statement statement = null;
    try {
        Properties info = new Properties();
        info.put("url", JDBC_URL);
        info.put("user", USER);
        info.put("password", PASSWORD);
        connection = DriverManager.getConnection("jdbc:accumulo:cosmos//localhost", info);
        statement = connection.createStatement();
        final ResultSet resultSet = statement.executeQuery("select \"PAGE_ID\" from \"" + CosmosDriver.COSMOS
                + "\".\"" + meataData.uuid() + "\"  where PAGE_ID='9' or REVISION_ID='8'");
        final ResultSetMetaData metaData = resultSet.getMetaData();
        final int columnCount = metaData.getColumnCount();

        assertEquals(columnCount, 1);

        int resultsFound = 0;
        while (resultSet.next()) {
            assertEquals(metaData.getColumnName(1), "PAGE_ID");
            @SuppressWarnings("unchecked")
            List<Entry<Column, RecordValue<?>>> sValues = (List<Entry<Column, RecordValue<?>>>) resultSet
                    .getObject("PAGE_ID");
            assertEquals(sValues.size(), 1);
            RecordValue onlyValue = sValues.iterator().next().getValue();
            assertEquals(onlyValue.visibility().toString(), "[en]");

            assertTrue(onlyValue.value().equals(Integer.valueOf(9).toString())
                    || onlyValue.value().equals(Integer.valueOf(8).toString()));
            resultsFound++;

        }

        assertEquals(resultsFound, 2);
    } finally {
        close(connection, statement);
    }
}

From source file:com.cnd.greencube.server.dao.jdbc.JdbcDAO.java

@SuppressWarnings("rawtypes")
public List queryExt(String sql) {
    final List lstValue = new ArrayList();
    getJdbcTemplate().query(sql, new RowCallbackHandler() {
        @SuppressWarnings("unchecked")
        public void processRow(ResultSet rs) throws SQLException {
            Map mp = new HashMap();
            int columnSize = rs.getMetaData().getColumnCount();
            try {
                String content;/*from  w  w w  .  jav a  2 s  .c  om*/
                Object obj;
                String columnName;
                for (int i = 1; i <= columnSize; i++) {
                    columnName = rs.getMetaData().getColumnName(i);
                    content = "";
                    obj = rs.getObject(i);
                    if (obj != null) {
                        content = rs.getObject(i).toString().trim();
                    }

                    mp.put(columnName, content);
                }
                lstValue.add(mp);
            } catch (Exception e) {
                logger.error(e);
            }
        }
    });
    return lstValue;
}

From source file:cosmos.sql.TestSql.java

@Test
public void testConjunction() throws SQLException {
    loadDriverClass();/*from   www .j  a  v a2s .c o  m*/
    Connection connection = null;
    Statement statement = null;
    try {
        Properties info = new Properties();
        info.put("url", JDBC_URL);
        info.put("user", USER);
        info.put("password", PASSWORD);
        connection = DriverManager.getConnection("jdbc:accumulo:cosmos//localhost", info);
        statement = connection.createStatement();
        final ResultSet resultSet = statement.executeQuery("select \"PAGE_ID\" from \"" + CosmosDriver.COSMOS
                + "\".\"" + meataData.uuid() + "\"  where PAGE_ID='9' and REVISION_ID='9'");
        final ResultSetMetaData metaData = resultSet.getMetaData();
        final int columnCount = metaData.getColumnCount();

        assertEquals(columnCount, 1);

        int resultsFound = 0;
        while (resultSet.next()) {
            assertEquals(metaData.getColumnName(1), "PAGE_ID");
            @SuppressWarnings("unchecked")
            List<Entry<Column, RecordValue<?>>> sValues = (List<Entry<Column, RecordValue<?>>>) resultSet
                    .getObject("PAGE_ID");
            assertEquals(sValues.size(), 1);
            RecordValue<?> onlyValue = sValues.iterator().next().getValue();
            assertEquals(onlyValue.visibility().toString(), "[en]");

            assertEquals(onlyValue.value(), Integer.valueOf(9).toString());
            resultsFound++;
            break;

        }

        assertEquals(resultsFound, 1);
    } finally {
        close(connection, statement);
    }
}

From source file:atg.tools.dynunit.test.util.DBUtils.java

void dump(@NotNull ResultSet rs) throws SQLException {

    // the order of the rows in a cursor
    // are implementation dependent unless you use the SQL ORDER statement
    ResultSetMetaData meta = rs.getMetaData();
    int colmax = meta.getColumnCount();
    int i;/*from w ww. j  ava 2s .c o  m*/
    Object o = null;

    // the result set is a cursor into the data. You can only
    // point to one row at a time
    // assume we are pointing to BEFORE the first row
    // rs.next() points to next row and returns true
    // or false if there is no next row, which breaks the loop
    for (; rs.next();) {
        for (i = 0; i < colmax; ++i) {
            o = rs.getObject(i + 1); // Is SQL the first column is indexed

            // with 1 not 0
            logger.info(o);
        }
    }
}

From source file:net.sathis.export.sql.DocBuilder.java

public Map<String, Object> getFields(Map<String, Object> firstRow, ResultSet rs, Entity entity,
        Map<String, Object> entityMap, Map<String, Object> rootEntityMap) throws SQLException {

    entityMap = new HashMap<String, Object>();

    if (entity.allAttributes.get(MULTI_VALUED) != null
            && entity.allAttributes.get(MULTI_VALUED).equalsIgnoreCase("true")) {
        List<Object> fieldArray = new ArrayList<Object>();
        rs.beforeFirst();/*from  ww w .  j  a v  a 2 s.  co m*/
        while (rs.next()) {
            if (entity.fields.size() > 1) {
                Map<String, Object> entityFieldsMap = new HashMap<String, Object>();
                for (Iterator<Field> iterator = entity.fields.iterator(); iterator.hasNext();) {
                    Field field = (Field) iterator.next();
                    FieldType fieldType = FieldType.valueOf(field.allAttributes.get("type").toUpperCase());
                    entityFieldsMap.put(field.name,
                            convertFieldType(fieldType, rs.getObject(field.column)).get(0));
                }
                fieldArray.add(entityFieldsMap);
            } else if (entity.fields.size() == 1) {
                fieldArray.add(rs.getObject(entity.fields.get(0).column));
            }
        }
        rootEntityMap.put(entity.name, fieldArray);
    } else if (firstRow != null) {
        for (Iterator<Field> iterator = entity.fields.iterator(); iterator.hasNext();) {
            Field field = (Field) iterator.next();
            FieldType fieldType = FieldType.valueOf(field.allAttributes.get("type").toUpperCase());

            if (firstRow.get(field.column) != null) {
                if (entity.pk != null && entity.pk.equals(field.name)) {
                    if (importer.getDataStoreType().equals(DataStoreType.MONGO)) {
                        entityMap.put("_id", convertFieldType(fieldType, firstRow.get(field.column)).get(0));
                    } else if (importer.getDataStoreType().equals(DataStoreType.COUCH)) {
                        //couch db says document id must be string
                        entityMap.put("_id",
                                convertFieldType(FieldType.STRING, firstRow.get(field.column)).get(0));
                    }
                } else {
                    entityMap.put(field.getName(),
                            convertFieldType(fieldType, firstRow.get(field.column)).get(0));
                }

                params.put(entity.name + "." + field.name, firstRow.get(field.column).toString());
            }

        }
    }

    if (entity.entities != null) {
        Entity subEntity = null;
        String query = "", aparam = "";
        for (Iterator<Entity> iterator = entity.entities.iterator(); iterator.hasNext();) {
            subEntity = (Entity) iterator.next();
            subLevel = subConnection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
                    ResultSet.CONCUR_READ_ONLY);
            query = subEntity.allAttributes.get("query");

            m = p.matcher(query);
            aparam = "";
            try {
                log.info("Parameter Map is: " + params);
                while (m.find()) {
                    aparam = query.substring(m.start() + 2, m.end() - 1);
                    query = query.replaceAll("(\\$\\{" + aparam + "\\})",
                            Matcher.quoteReplacement(StringEscapeUtils.escapeSql(params.get(aparam))));
                    m = p.matcher(query);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            resultSet = subLevel.executeQuery(query);
            if (resultSet.next()) {
                subEntityData = getFields(processor.toMap(resultSet), resultSet, subEntity, null, entityMap);
                if (subEntityData.size() > 0)
                    entityMap.put(subEntity.name, subEntityData);
            }
            resultSet.close();
            subLevel.close();
        }
    }
    return entityMap;
}

From source file:com.hexin.core.dao.BaseDaoSupport.java

@Override
public <T> List<T> findListWithBlob(String sql, Class<T> dtoClass, Object... args)
        throws SQLException, InstantiationException, IllegalAccessException, SecurityException,
        IllegalArgumentException, NoSuchFieldException, IOException {

    long startTime = System.currentTimeMillis();
    long endTime;
    long durTime;

    debugSql(sql, args);//w ww.  j  a v a2s.c  o  m

    PreparedStatement ps = jdbcTemplate.getDataSource().getConnection().prepareStatement(sql);

    setPreparedStatementParameter(ps, args);

    List<T> list = new ArrayList<T>();
    ResultSet rs = ps.executeQuery();
    while (rs.next()) {
        ResultSetMetaData rsmd = rs.getMetaData();
        int colCount = rsmd.getColumnCount();

        T obj = dtoClass.newInstance();
        for (int i = 1; i <= colCount; i++) {
            String colName = rsmd.getColumnLabel(i); // ??
            String colTypeName = rsmd.getColumnTypeName(i);
            String beanFiledName = IcpObjectUtil.underlineToCamel(colName);

            if ("blob".equalsIgnoreCase(colTypeName)) {
                InjectValueUtil.setFieldValue(obj, beanFiledName, rs.getBlob(i));
            } else {
                InjectValueUtil.setFieldValue(obj, beanFiledName, rs.getObject(i));
            }
        }

        list.add(obj);
    }

    endTime = System.currentTimeMillis();
    durTime = endTime - startTime;
    logger.debug("This jdbc operation costs time: " + durTime);

    return list;
}

From source file:main.export.sql.DocBuilder.java

@SuppressWarnings("unchecked")
public Map<String, Object> getFields(Map<String, Object> firstRow, ResultSet rs, Entity entity,
        Map<String, Object> entityMap, Map<String, Object> rootEntityMap) throws SQLException {

    entityMap = new HashMap<String, Object>();

    if (entity.allAttributes.get(MULTI_VALUED) != null
            && entity.allAttributes.get(MULTI_VALUED).equalsIgnoreCase("true")) {
        List<Object> fieldArray = new ArrayList<Object>();
        rs.beforeFirst();/* w ww.j a v  a2s. co m*/
        while (rs.next()) {
            if (entity.fields.size() > 1) {
                Map<String, Object> entityFieldsMap = new HashMap<String, Object>();
                for (Iterator<Field> iterator = entity.fields.iterator(); iterator.hasNext();) {
                    Field field = (Field) iterator.next();
                    FieldType fieldType = FieldType.valueOf(field.allAttributes.get("type").toUpperCase());
                    entityFieldsMap.put(field.name,
                            convertFieldType(fieldType, rs.getObject(field.column)).get(0));
                }
                fieldArray.add(entityFieldsMap);
            } else if (entity.fields.size() == 1) {
                fieldArray.add(rs.getObject(entity.fields.get(0).column));
            }
        }
        rootEntityMap.put(entity.name, fieldArray);
    } else if (firstRow != null) {
        for (Iterator<Field> iterator = entity.fields.iterator(); iterator.hasNext();) {
            Field field = (Field) iterator.next();
            FieldType fieldType = FieldType.valueOf(field.allAttributes.get("type").toUpperCase());

            if (firstRow.get(field.column) != null) {
                if (entity.pk != null && entity.pk.equals(field.name)) {
                    if (importer.getDataStoreType().equals(DataStoreType.MONGO)) {
                        entityMap.put("_id", convertFieldType(fieldType, firstRow.get(field.column)).get(0));
                    } else if (importer.getDataStoreType().equals(DataStoreType.COUCH)) {
                        // couch db says document id must be string
                        entityMap.put("_id",
                                convertFieldType(FieldType.STRING, firstRow.get(field.column)).get(0));
                    }
                } else {
                    entityMap.put(field.getName(),
                            convertFieldType(fieldType, firstRow.get(field.column)).get(0));
                }

                params.put(entity.name + "." + field.name, firstRow.get(field.column).toString());
            }

        }
    }

    if (entity.entities != null) {
        Entity subEntity = null;
        String query = "", aparam = "";
        for (Iterator<Entity> iterator = entity.entities.iterator(); iterator.hasNext();) {
            subEntity = (Entity) iterator.next();
            subLevel = subConnection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
                    ResultSet.CONCUR_READ_ONLY);
            query = subEntity.allAttributes.get("query");

            m = p.matcher(query);
            aparam = "";
            try {
                log.info("Parameter Map is: " + params);
                while (m.find()) {
                    aparam = query.substring(m.start() + 2, m.end() - 1);
                    query = query.replaceAll("(\\$\\{" + aparam + "\\})",
                            Matcher.quoteReplacement(StringEscapeUtils.escapeSql(params.get(aparam))));
                    m = p.matcher(query);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            resultSet = subLevel.executeQuery(query);
            if (resultSet.next()) {
                subEntityData = getFields(processor.toMap(resultSet), resultSet, subEntity, null, entityMap);
                if (subEntityData.size() > 0)
                    entityMap.put(subEntity.name, subEntityData);
            }
            resultSet.close();
            subLevel.close();
        }
    }
    return entityMap;
}