Share on Google+Share on Google+

Spring SqlRowSet example

This section is about SqlRowSet use in the Spring class to fetch data from table.

Spring SqlRowSet example

The 'SqlRowSet' is used to handle the result fetched (very similar to ResultSet in core java). The main difference is that SQL exception is never thrown by it. The 'SqlRowSet' throws 'org.springframework.jdbc.InvalidResultSetAccessException' if needed. For using it in your class file you need to import ' ' package. You can use next() and getString() method as you are using with 'ResultSet' of core java. Given below is example related to it :

package net.roseindia;

import javax.sql.DataSource;
import org.springframework.jdbc.core.JdbcTemplate;

public class sqlrowset {
	private JdbcTemplate jdbcTemplate;

	public void setDataSource(DataSource dataSource) {
		this.jdbcTemplate = new JdbcTemplate(dataSource);

	public void doExecute() {
		SqlRowSet srs = jdbcTemplate.queryForRowSet("select * from customer");
		int rowCount = 0;
		while ( {
			System.out.println(srs.getString("id") + " - "
					+ srs.getString("first_name") + " - "
					+ srs.getString("last_name") + " - "
					+ srs.getString("last_login"));
		System.out.println("Number of records : " + rowCount);



<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""
xmlns:xsi="" xmlns:context=""

<bean id="sqlrowset" class="net.roseindia.sqlrowset">
<property name="dataSource" ref="dataSource" />
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"destroy-method="close">
<property name="driverClass" value="com.mysql.jdbc.Driver" />
<property name="jdbcUrl" value="jdbc:mysql://" />
<property name="user" value="root" />
<property name="password" value="root" />
<context:property-placeholder location="" />

package net.roseindia;

import org.springframework.beans.factory.xml.XmlBeanFactory;

public class sqlrowsetMain {

	public static void main(String[] args) {

		XmlBeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource(

		sqlrowset myBean = (sqlrowset) beanFactory.getBean("sqlrowset");





Data in Sql Table :

After executing code output in Eclipse's console :

Download Source Code


Posted on: August 21, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.


Advertisement null