原文地址:http://www.roseindia.net/tutorial/spring/spring3/jdbc/sqlrowset.html

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 ' org.springframework.jdbc.support.rowset.SqlRowSet ' package. You can use next() and getString() method as you are using with 'ResultSet' of core java. Given below is example related to it :

sqlrowset.java

  1. package net.roseindia;
  2.  
  3. import javax.sql.DataSource;
  4. import org.springframework.jdbc.core.JdbcTemplate;
  5. import org.springframework.jdbc.support.rowset.SqlRowSet;
  6.  
  7. public class sqlrowset {
  8. private JdbcTemplate jdbcTemplate;
  9.  
  10. public void setDataSource(DataSource dataSource) {
  11. this.jdbcTemplate = new JdbcTemplate(dataSource);
  12. }
  13.  
  14. public void doExecute() {
  15. SqlRowSet srs =jdbcTemplate.queryForRowSet("select * from customer");
  16. int rowCount = 0;
  17. while (srs.next()) {
  18. System.out.println(srs.getString("id") + " - " + srs.getString("first_name")+ " - " + srs.getString("last_name")+ " - " + srs.getString("last_login"));
  19. rowCount++;
  20. }
  21. System.out.println("Number of records : "+rowCount);
  22. }
  23.  
  24. }

DataTable.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
  4. xsi:schemaLocation="
  5. http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  7. http://www.springframework.org/schema/context
  8. http://www.springframework.org/schema/context/spring-context-3.0.xsd">
  9. <bean id="CreateTable" class="net.roseindia.CreateTable">
  10. <property name="dataSource" ref="dataSource" />
  11. </bean>
  12. <bean id="sqlrowset" class="net.roseindia.sqlrowset">
  13. <property name="dataSource" ref="dataSource" />
  14. </bean>
  15. <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
  16. destroy-method="close">
  17. <property name="driverClass" value="com.mysql.jdbc.Driver" />
  18. <property name="jdbcUrl" value="jdbc:mysql://192.168.10.13:3306/ankdb" />
  19. <property name="user" value="root" />
  20. <property name="password" value="root" />
  21. </bean>
  22. <context:property-placeholder location="jdbc.properties" />
  23. </beans>

sqlrowsetMain.java

  1. package net.roseindia;
  2.  
  3. import org.springframework.beans.factory.xml.XmlBeanFactory;
  4. import org.springframework.core.io.ClassPathResource;
  5.  
  6. public class sqlrowsetMain {
  7.  
  8. public static void main(String[] args) {
  9.  
  10. XmlBeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource("DataTable.xml"));
  11.  
  12. sqlrowset myBean = (sqlrowset) beanFactory.getBean("sqlrowset");
  13.  
  14. myBean.doExecute();
  15.  
  16. }
  17.  
  18. }

OUTPUT

Data in Sql Table :

After executing code output in Eclipse's console :

Spring SqlRowSet example--转载的更多相关文章

  1. Spring IOC(转载)

    学习过Spring框架的人一定都会听过Spring的IoC(控制反转) .DI(依赖注入)这两个概念,对于初学Spring的人来说,总觉得IoC .DI这两个概念是模糊不清的,是很难理解的,今天和大家 ...

  2. Spring的国际化(转载)

    1:在MyEclipse下面创建一个test的Web  Project,然后添加Spring相关的文件,在src根目录下创建applicationContext.xml文件. applicationC ...

  3. Spring学习(3):Spring架构(转载)

    1. Spring架构图 核心容器:包括Core.Beans.Context.EL模块. ●Core模块:封装了框架依赖的最底层部分,包括资源访问.类型转换及一些常用工具类. ●Beans模块:提供了 ...

  4. Spring学习(3):Spring概述(转载)

    1. Spring是什么? Spring是一个开源的轻量级Java SE(Java 标准版本)/Java EE(Java 企业版本)开发应用框架,其目的是用于简化企业级应用程序开发. 在面向对象思想中 ...

  5. @Transactional spring 事务(转载)

    原文链接: http://www.cnblogs.com/sweetchildomine/p/6978037.html?utm_source=itdadao&utm_medium=referr ...

  6. Spring AOP(转载)

    此前对于AOP的使用仅限于声明式事务,除此之外在实际开发中也没有遇到过与之相关的问题.最近项目中遇到了以下几点需求,仔细思考之后,觉得采用AOP 来解决.一方面是为了以更加灵活的方式来解决问题,另一方 ...

  7. 玩转单元测试之Testing Spring MVC Controllers

    玩转单元测试之 Testing Spring MVC Controllers 转载注明出处:http://www.cnblogs.com/wade-xu/p/4311657.html The Spri ...

  8. 就是这么简单(续)!使用 RestAssuredMockMvc 测试 Spring MVC Controllers

    就是这么简单(续)!使用 RestAssuredMockMvc 测试 Spring MVC Controllers 转载注明出处:http://www.cnblogs.com/wade-xu/p/43 ...

  9. Dive into Spring framework -- 了解基本原理(一)

    在继续我们的分析之前,推荐各位静心来读一下<<Expert_OneOne_J2EE_Design_and_Development>> 第四章, 正如spring BeanFac ...

随机推荐

  1. HDU ACM 3177 Crixalis's Equipment

    Crixalis's Equipment Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  2. android学习笔记---发送状态栏通知

    发送消息的代码如下: //获取通知管理器 NotificationManager mNotificationManager = (NotificationManager) getSystemServi ...

  3. Java之对象池

    单例模式是限制了一个类只能有一个实例,对象池模式则是限制一个类实例的个数.对象池类就像是一个对象管理员,它以Static列表(也就是装对象的池子)的形式存存储某个实例数受限的类的实例,每一个实例还要加 ...

  4. C#中字符串与byte[]相互转换

    字符串转换为byte[] 给定一个string,转换为byte[],有以下几种方法. 方法1: static byte[] GetBytes(string str) { byte[] bytes = ...

  5. Jenkins+Gitlab搭建持续集成(CI)环境

    利用Jenkins+Gitlab搭建持续集成(CI)环境 Permalink: 2013-09-08 22:04:00 by hyhx2008in intern tags: jenkins gitla ...

  6. 数据库连接池 c3p0 demo 代码和分析

    import java.sql.Connection; import java.sql.SQLException; import java.beans.PropertyVetoException; i ...

  7. 获取父进程ID

    本程序主要功能是:获取某程序的ParentProcessID 直接上代码: // parent.cpp (Windows NT/2000) // // This example will show t ...

  8. [转]vector iterator not incrementable 的问题

    转自:http://blog.csdn.net/kuaile123/article/details/11105115 vector::erase误使用问题: 暂时使用经验: 不能在循环中使用,否则会报 ...

  9. vs2008 release下调试状态设置[转]

    这是一个老生常谈的话题,但还是有时候会漏洞一些设置.总结一些,总共需要三个地方设置, 分别是1)c\c++-> General->Debug Information Format. 2) ...

  10. 面试过程中javascript原型链与作用域的问题

    现在校招也基本结束了,所以有时间把这段时间遇到的问题做个总结.在很多的笔试题目中,发现有很多对JS作用域方面的考察,所以查阅资料总结一下. 众所周知,js不像其他OOP语言那样,他是一种弱类型的语言, ...