Spring SqlRowSet example--转载
原文地址: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 :
Spring SqlRowSet example--转载的更多相关文章
- Spring IOC(转载)
学习过Spring框架的人一定都会听过Spring的IoC(控制反转) .DI(依赖注入)这两个概念,对于初学Spring的人来说,总觉得IoC .DI这两个概念是模糊不清的,是很难理解的,今天和大家 ...
- Spring的国际化(转载)
1:在MyEclipse下面创建一个test的Web Project,然后添加Spring相关的文件,在src根目录下创建applicationContext.xml文件. applicationC ...
- Spring学习(3):Spring架构(转载)
1. Spring架构图 核心容器:包括Core.Beans.Context.EL模块. ●Core模块:封装了框架依赖的最底层部分,包括资源访问.类型转换及一些常用工具类. ●Beans模块:提供了 ...
- Spring学习(3):Spring概述(转载)
1. Spring是什么? Spring是一个开源的轻量级Java SE(Java 标准版本)/Java EE(Java 企业版本)开发应用框架,其目的是用于简化企业级应用程序开发. 在面向对象思想中 ...
- @Transactional spring 事务(转载)
原文链接: http://www.cnblogs.com/sweetchildomine/p/6978037.html?utm_source=itdadao&utm_medium=referr ...
- Spring AOP(转载)
此前对于AOP的使用仅限于声明式事务,除此之外在实际开发中也没有遇到过与之相关的问题.最近项目中遇到了以下几点需求,仔细思考之后,觉得采用AOP 来解决.一方面是为了以更加灵活的方式来解决问题,另一方 ...
- 玩转单元测试之Testing Spring MVC Controllers
玩转单元测试之 Testing Spring MVC Controllers 转载注明出处:http://www.cnblogs.com/wade-xu/p/4311657.html The Spri ...
- 就是这么简单(续)!使用 RestAssuredMockMvc 测试 Spring MVC Controllers
就是这么简单(续)!使用 RestAssuredMockMvc 测试 Spring MVC Controllers 转载注明出处:http://www.cnblogs.com/wade-xu/p/43 ...
- Dive into Spring framework -- 了解基本原理(一)
在继续我们的分析之前,推荐各位静心来读一下<<Expert_OneOne_J2EE_Design_and_Development>> 第四章, 正如spring BeanFac ...
随机推荐
- 浅谈Spark(1) - Overview
Spark是UC Berkeley AMP lab所开源的类Hadoop MapReduce的通用并行框架,Spark,拥有Hadoop MapReduce所具有的优点:但不同于MapReduce的是 ...
- Error when running Swift3 in REPL
Traceback (most recent call last): File "", line 1, in NameError: name 'run_one_line' is n ...
- accordion data-options iconCls
<div id="aa" class="easyui-accordion" style="width:500px;height:300px;&q ...
- 在VS2013中使用水晶报表
又遇到了在B/S系统中打印,打印格式要求比较高,打印出的效果要求高大上.用VS2013中微软自带的报表,实在难以实现应用的效果,主要问题表现在: 1.不能插入用Word做好的打印模板,自己按照模板来做 ...
- MAT(1) 小样
一.内存溢出时生成hprof文件 运行参数: -Xms40m -Xmx40m -Xmn20m -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=E:\J ...
- XML与DataSet相互转换,DataSet查询
以FileShare.Read形式读XML文件: string hotspotXmlStr = string.Empty; try { Stream fileStream = new FileStre ...
- JS:中文GB2312编码
今天开发遇到了个问题,有点纳闷.... 在ajax的时候要传递一个中文值,不管我在js中是否使用了encodeURI.encodeURIComponent编码,但是在后台request获取的值是始终是 ...
- missing artifact com.oracle:ojdbc14:jar:10.2.0.2.0解决办法
下载jar,导入到maven中 下载:http://download.csdn.net/detail/spring123tt/6991897 cmd中输入: mvn install:install-f ...
- MongoDB下载与安装
本节只针对MONGODB的安装进行介绍,具体mongodb的特点及优势可参考其他文件. 注意32位操作系统支持的最大文件为2GB,所以做大文件海量储存的朋友要选择64位的系统安装.开始我们的下载安装之 ...
- VC++ 网络编程总结(二)
2.基本的Windows Socket API编程 需要在程序中添加下面的包含语句:#include <winsock2.h> #pragma comment( lib, " ...