Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
   * Copyright 2006-2009 the original author or authors.
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
 package org.springframework.batch.item.database;

Simple item reader implementation that opens a JDBC cursor and continually retrieves the next row in the ResultSet.

The statement used to open the cursor is created with the 'READ_ONLY' option since a non read-only cursor may unnecessarily lock tables or rows. It is also opened with 'TYPE_FORWARD_ONLY' option. By default the cursor will be opened using a separate connection which means that it will not participate in any transactions created as part of the step processing.

Each call to will call the provided RowMapper, passing in the ResultSet.

Lucas Ward
Peter Zozom
Robert Kasanicky
Thomas Risberg
 public class JdbcCursorItemReader<T> extends AbstractCursorItemReader<T> {
 	public JdbcCursorItemReader() {

Set the RowMapper to be used for all calls to read().

 	public void setRowMapper(RowMapper rowMapper) {
 		this. = rowMapper;

Set the SQL statement to be used when creating the cursor. This statement should be a complete and valid SQL statement, as it will be run directly without any modification.

 	public void setSql(String sql) {
 		this. = sql;

Set the PreparedStatementSetter to use if any parameter values that need to be set in the supplied query.

 	public void setPreparedStatementSetter(PreparedStatementSetter preparedStatementSetter) {
 		this. = preparedStatementSetter;

Assert that mandatory properties are set.

java.lang.IllegalArgumentException if either data source or sql properties not set.
	public void afterPropertiesSet() throws Exception {
		Assert.notNull("The SQL query must be provided");
		Assert.notNull("RowMapper must be provided");
	protected void openCursor(Connection con) {	
		try {
			else {
			if (this. != null) {
		catch (SQLException se) {
			throw getExceptionTranslator().translate("Executing query"getSql(), se);
	protected T readCursor(ResultSet rsint currentRowthrows SQLException {
		return (T) .mapRow(rscurrentRow);

Close the cursor and database connection.
	protected void cleanupOnClose() throws Exception {
	public String getSql() {
		return this.;
New to GrepCode? Check out our FAQ X