Example usage for org.springframework.jdbc.core RowCallbackHandler processRow

List of usage examples for org.springframework.jdbc.core RowCallbackHandler processRow

Introduction

In this page you can find the example usage for org.springframework.jdbc.core RowCallbackHandler processRow.

Prototype

void processRow(ResultSet rs) throws SQLException;

Source Link

Document

Implementations must implement this method to process each row of data in the ResultSet.

Usage

From source file:com.oracle2hsqldb.spring.MetaDataJdbcTemplate.java

public void query(RowCallbackHandler rowHandler) {
    Connection connection = getConnection();

    ResultSet results = null;// w  w w. jav  a2s  . co  m
    try {
        results = getResults(connection.getMetaData());
        while (results.next()) {
            rowHandler.processRow(results);
        }
    } catch (SQLException e) {
        throw new MetaDataAccessException(e);
    } finally {
        try {
            if (results != null)
                results.close();
        } catch (SQLException e) {
            log.warn("could not close the ResultSet", e);
        }
        try {
            if (connection != null)
                connection.close();
        } catch (SQLException e) {
            throw new CleanupFailureDataAccessException("could not close Connection", e);
        }
    }
}

From source file:org.apache.ctakes.ytex.kernel.KernelUtilImpl.java

/**
 * this can be very large - avoid loading the entire jdbc ResultSet into
 * memory/*from  w w w  .j a v  a  2s  .  c o m*/
 */
@Override
public InstanceData loadInstances(String strQuery) {
    final InstanceData instanceLabel = new InstanceData();
    PreparedStatement s = null;
    Connection conn = null;
    ResultSet rs = null;
    try {
        // jdbcTemplate.query(strQuery, new RowCallbackHandler() {
        RowCallbackHandler ch = new RowCallbackHandler() {

            @Override
            public void processRow(ResultSet rs) throws SQLException {
                String label = "";
                int run = 0;
                int fold = 0;
                boolean train = true;
                long instanceId = rs.getLong(1);
                String className = rs.getString(2);
                if (rs.getMetaData().getColumnCount() >= 3)
                    train = rs.getBoolean(3);
                if (rs.getMetaData().getColumnCount() >= 4) {
                    label = rs.getString(4);
                    if (label == null)
                        label = "";
                }
                if (rs.getMetaData().getColumnCount() >= 5)
                    fold = rs.getInt(5);
                if (rs.getMetaData().getColumnCount() >= 6)
                    run = rs.getInt(6);
                // get runs for label
                SortedMap<Integer, SortedMap<Integer, SortedMap<Boolean, SortedMap<Long, String>>>> runToInstanceMap = instanceLabel
                        .getLabelToInstanceMap().get(label);
                if (runToInstanceMap == null) {
                    runToInstanceMap = new TreeMap<Integer, SortedMap<Integer, SortedMap<Boolean, SortedMap<Long, String>>>>();
                    instanceLabel.getLabelToInstanceMap().put(label, runToInstanceMap);
                }
                // get folds for run
                SortedMap<Integer, SortedMap<Boolean, SortedMap<Long, String>>> foldToInstanceMap = runToInstanceMap
                        .get(run);
                if (foldToInstanceMap == null) {
                    foldToInstanceMap = new TreeMap<Integer, SortedMap<Boolean, SortedMap<Long, String>>>();
                    runToInstanceMap.put(run, foldToInstanceMap);
                }
                // get train/test set for fold
                SortedMap<Boolean, SortedMap<Long, String>> ttToClassMap = foldToInstanceMap.get(fold);
                if (ttToClassMap == null) {
                    ttToClassMap = new TreeMap<Boolean, SortedMap<Long, String>>();
                    foldToInstanceMap.put(fold, ttToClassMap);
                }
                // get instances for train/test set
                SortedMap<Long, String> instanceToClassMap = ttToClassMap.get(train);
                if (instanceToClassMap == null) {
                    instanceToClassMap = new TreeMap<Long, String>();
                    ttToClassMap.put(train, instanceToClassMap);
                }
                // set the instance class
                instanceToClassMap.put(instanceId, className);
                // add the class to the labelToClassMap
                SortedSet<String> labelClasses = instanceLabel.getLabelToClassMap().get(label);
                if (labelClasses == null) {
                    labelClasses = new TreeSet<String>();
                    instanceLabel.getLabelToClassMap().put(label, labelClasses);
                }
                if (!labelClasses.contains(className))
                    labelClasses.add(className);
            }
        };
        conn = this.jdbcTemplate.getDataSource().getConnection();
        s = conn.prepareStatement(strQuery, java.sql.ResultSet.TYPE_FORWARD_ONLY,
                java.sql.ResultSet.CONCUR_READ_ONLY);
        if ("MySQL".equals(conn.getMetaData().getDatabaseProductName())) {
            s.setFetchSize(Integer.MIN_VALUE);
        } else if (s.getClass().getName().equals("com.microsoft.sqlserver.jdbc.SQLServerStatement")) {
            try {
                BeanUtils.setProperty(s, "responseBuffering", "adaptive");
            } catch (IllegalAccessException e) {
                log.warn("error setting responseBuffering", e);
            } catch (InvocationTargetException e) {
                log.warn("error setting responseBuffering", e);
            }
        }
        rs = s.executeQuery();
        while (rs.next()) {
            ch.processRow(rs);
        }
    } catch (SQLException j) {
        log.error("loadInstances failed", j);
        throw new RuntimeException(j);
    } finally {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
            }
        }
        if (s != null) {
            try {
                s.close();
            } catch (SQLException e) {
            }
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
            }
        }
    }
    return instanceLabel;
}

From source file:org.sipfoundry.sipxconfig.cdr.CdrManagerImplTest.java

public void testCdrsCsvWriterNullConnectTime() throws Exception {
    TimeZone tz = DateUtils.UTC_TIME_ZONE;
    Calendar calendar = Calendar.getInstance(tz);

    Timestamp timestamp = new Timestamp(System.currentTimeMillis());
    String dateStr = String.format("\"%s\",", DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT.format(timestamp));

    ResultSet rs = createMock(ResultSet.class);
    for (int i = 0; i < ColumnInfo.FIELDS.length; i++) {
        rs.findColumn((String) anyObject());
        expectLastCall().andReturn(i);/*from ww  w. j  a v  a  2 s.  co  m*/
    }

    rs.getString(0);
    expectLastCall().andReturn("caller");
    rs.getString(1);
    expectLastCall().andReturn("callee");

    rs.getTimestamp(eq(2), eqTimeZone(calendar));
    expectLastCall().andReturn(timestamp);
    rs.getTimestamp(eq(3), eqTimeZone(calendar));
    expectLastCall().andReturn(null);
    rs.getTimestamp(eq(4), eqTimeZone(calendar));
    expectLastCall().andReturn(timestamp);

    rs.getString(5);
    expectLastCall().andReturn("404");

    rs.getString(6);
    expectLastCall().andReturn("I");

    rs.getString(7);
    expectLastCall().andReturn("0000-0000");

    replay(rs);

    StringWriter writer = new StringWriter();

    ColumnInfoFactory columnInforFactory = new DefaultColumnInfoFactory(tz);
    RowCallbackHandler handler = new CdrsCsvWriter(writer, columnInforFactory);
    handler.processRow(rs);

    assertEquals("\"caller\",\"callee\"," + dateStr + "\"\"," + dateStr + "\"404\",\"I\",\"0000-0000\"\n",
            writer.toString());

    verify(rs);
}