原文地址: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

package net.roseindia;import javax.sql.DataSource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.support.rowset.SqlRowSet;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 (srs.next()) {System.out.println(srs.getString("id") + " - " + srs.getString("first_name")+ " - " + srs.getString("last_name")+ " - " + srs.getString("last_login"));rowCount++;}System.out.println("Number of records : "+rowCount);}}

DataTable.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd"><bean id="CreateTable" class="net.roseindia.CreateTable"><property name="dataSource" ref="dataSource" /></bean><bean id="sqlrowset" class="net.roseindia.sqlrowset"><property name="dataSource" ref="dataSource" /></bean><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://192.168.10.13:3306/ankdb" /><property name="user" value="root" /><property name="password" value="root" /></bean><context:property-placeholder location="jdbc.properties" />
</beans>

sqlrowsetMain.java

package net.roseindia;import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;public class sqlrowsetMain {public static void main(String[] args) {XmlBeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource("DataTable.xml"));sqlrowset myBean = (sqlrowset) beanFactory.getBean("sqlrowset");myBean.doExecute();}}

OUTPUT

Data in Sql Table :

After executing code output in Eclipse's console :

转载于:https://www.cnblogs.com/davidwang456/p/4540312.html

Spring SqlRowSet example--转载相关推荐

  1. Spring的国际化(转载)

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

  2. spring事物(转载:http://www.cnblogs.com/ysxlin/archive/2008/06/06/1215300.html)

    Chapter 1. Spring中的事务控制(Transacion Management with Spring) Table of Contents 1.1. 有关事务(Transaction)的 ...

  3. spring ioc原理(转载,侵删)

    参考博客: https://blog.csdn.net/smartbetter/article/details/100672806

  4. 使用spring initializr ( 4.快速创建springboot工程 )(入门结束)

    组织id  com.tabctrlshift 模块id  spring-boot-01-helloworld-quick 可以看看下面的东西 最终只选择web 选择我们需要的模块即可 wokao,就这 ...

  5. SSM框架——Spring+SpringMVC+Mybatis的搭建教程

    一:概述 SSM框架在项目开发中经常使用到,相比于SSH框架,它在仅几年的开发中运用的更加广泛. Spring作为一个轻量级的框架,有很多的拓展功能,最主要的我们一般项目使用的就是IOC和AOP. S ...

  6. Spring Batch 介绍

    在企业应用的关键环境中,通常有需要很多应用来来处理大量的应用.这商业操作包括了自动化,并且负责的处理程序来对大量数据进行高效的处理,通常这些程序不需要人工进行干预.这些事件包括有基于时间周期产生的操作 ...

  7. Spring 教程(三) 环境设置

    环境设置 本教程将指导你如何准备开发环境来使用 Spring 框架开始你的工作.本教程还将教你在安装 Spring 框架之前如何在你的机器上安装 JDK,Tomcat 和 Eclipse. 第 1 步 ...

  8. Spring 命名空间

    2019独角兽企业重金招聘Python工程师标准>>> <?xml version="1.0" encoding="UTF-8"?> ...

  9. spring实例教程

    1.配置好spring mvc发现访问无法匹配,很可能是文件放的位置或者相对目录不对. 2.实例大全:http://www.yiibai.com/spring/spring-collections-l ...

最新文章

  1. mysql外键写了会怎么样_mysql使用外键会影响性能吗
  2. 【数据平台】Eclipse+MapReduce开发环境(集群运行模式)
  3. 【Linux】一步一步学Linux——gcov命令(257)
  4. Nplayer本地文件拷到服务器,手把手教你简易NAS构建,手机/平板/智能电视随意调取,家庭存储云共享,有了自己的网络云盘后再也不用担心容量不够了!...
  5. 如何用python处理图片_推荐:10个Python图像处理工具
  6. DOM事件流三个阶段
  7. Jenkins学习之旅
  8. nginx 启动报错 “/var/run/nginx/nginx.pid failed” 解决方法
  9. C#之SqlDependency数据库缓存
  10. 火狐浏览器 附件组件 Xpath 使用
  11. background复合属性顺序_background(css复合写法)
  12. 基于AT89C51及Proteus仿真的室内火灾监测装置
  13. MOS管推挽电路设计及特性解析
  14. 34Speech SIMCLR: Combining Contrastive and Reconstruction Objective for Self-supervised Speech
  15. 玩转5G之--初探5G网络模型(OSI模型和TCP/IP模型)
  16. 开启cdn后导致websocket10秒直接断开连接 报错1006
  17. python编程计算器_Python3-用Python编写计算器的3种方法
  18. 面试难,应聘难,好工作,今安在?
  19. 黑白图片怎样上色?教你如何给黑白照片上色
  20. 操作系统原理模拟实验(基于C/C++模拟处理机调度、存储管理和文件系统)

热门文章

  1. 怎么转换html文件为mp3,如何把音频转换成mp3_音频文件怎么转mp3格式-系统城
  2. 用python计算pi的值_如何使用python中的series计算pi的值?
  3. python泰勒展开_如何利用sympy对未知函数$f(x)$进行符号泰勒展开
  4. @excel注解_惊了!如何通过阿里 EasyExcel 7 行代码, 优雅地实现 Excel 文件导出功能?...
  5. 树莓派python编程小车_python3实现网页版raspberry pi(树莓派)小车控制
  6. 计算机未来的储存装置,图说计算机存储设备
  7. php 微信 返回,php版微信返回用户text输入的方法
  8. Android:安卓APP开发显示一个美女,安卓APP开发显示两个美女
  9. Win10 插入耳机无声问题 解决办法
  10. 游戏维护关闭服务器,游戏服务器正在维护中