测试spring的存储过程
来源:互联网 发布:debug python脚本 编辑:程序博客网 时间:2024/04/27 22:42
测试spring的存储过程
spring对存储过程进行封装.它的实现细节与jdbc类似
下面进行测试
1):写存储过程执行类:
package jdbc;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import javax.sql.DataSource;
import org.springframework.jdbc.core.SqlParameter;
import org.springframework.jdbc.object.StoredProcedure;
import org.springframework.jdbc.core.SqlOutParameter;
import org.springframework.jdbc.core.SqlParameter;
public class StoreTemplate
extends StoredProcedure {
HashMap map = new HashMap();
public StoreTemplate() {
super();
}
public void setValue(String key, Object obj) {
map.put(key, obj);
}
public StoreTemplate(DataSource ds) {
setDataSource(ds);
}
public Map execute() {
if(this.getSql()==null || this.getSql().equals("")) return null;
this.compile();
return execute(map);
}
public void setVarcharParam(String param) {
this.declareParameter(new SqlParameter(param, Types.VARCHAR));
}
public void setDoubleParam(String param) {
this.declareParameter(new SqlParameter(param, Types.DOUBLE));
}
public void setIntegerParam(String param) {
this.declareParameter(new SqlParameter(param, Types.INTEGER));
}
public void setVarcharOutParam(String param) {
this.declareParameter(new SqlOutParameter(param, Types.VARCHAR));
}
public void setDoubleOutParam(String param) {
this.declareParameter(new SqlOutParameter(param, Types.DOUBLE));
}
public void setIntegerOutParam(String param) {
this.declareParameter(new SqlOutParameter(param, Types.INTEGER));
}
}
2):用spring配置数据源:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName"><value>com.microsoft.jdbc.sqlserver.SQLServerDriver</value></property>
<property name="url"><value>jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=pubs;SelectMethod=Cursor</value></property>
<property name="username"><value>sa</value></property>
<property name="password"><value></value></property>
</bean>
</beans>
3):写junit:
package jdbc;
import junit.framework.*;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import java.util.List;
import java.util.Map;
import javax.sql.DataSource;
public class TestStoreTemplate extends TestCase {
ApplicationContext ctx=null;
protected void setUp() throws Exception {
ctx= new FileSystemXmlApplicationContext("D://work//jpetstore//src//jdbc//Context-jdbc.xml");
}
public void testStore(){
DataSource datasource=(DataSource)ctx.getBean("dataSource");
StoreTemplate qry=new StoreTemplate(datasource);
qry.setSql("testsp");
qry.setIntegerParam("count");
qry.setIntegerOutParam("ret");
qry.setValue("count",new Integer(1));
Map map=qry.execute();
if(map!=null){
System.out.println(map.get("ret"));
}
}
protected void tearDown() throws Exception {
}
}
4):写测试存储过程
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
create procedure testsp(@count int,@ret int out)
as
begin
select @ret=@count+1
end
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
5):编译运行 ok.
6):附jdbc调用存储过程的方法
package jdbc;
import junit.framework.*;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import javax.sql.DataSource;
import java.sql.*;
public class TestJdbcCallStore
extends TestCase {
ApplicationContext ctx = null;
protected void setUp() throws Exception {
ctx = new FileSystemXmlApplicationContext("D://work//jpetstore//src//jdbc//Context-jdbc.xml");
}
public void testStore() {
DataSource datasource = (DataSource) ctx.getBean("dataSource");
CallableStatement cstmt = null;
try {
cstmt = datasource.getConnection().prepareCall(
"{call testsp(?,?)}");
cstmt.setInt(1, 1);
cstmt.registerOutParameter(2, Types.INTEGER);
cstmt.executeUpdate();
Object obj = cstmt.getObject(2);
if (obj != null) {
System.out.println(obj.toString());
}
}
catch (SQLException es) {
es.printStackTrace(System.out);
}
finally {
}
}
protected void tearDown() throws Exception {
}
}
- 测试spring的存储过程
- 测试用的存储过程
- 存储过程的复制测试
- 测试存储过程的调用
- spring jdbc 操作存储过程或者函数测试
- spring 定时任务调用存储过程配置示例(未测试)
- 全面的存储过程 测试可用
- 我的测试存储过程代码
- PL/SQL 存储过程的测试
- Oracle存储过程的编写与测试
- 一个简单存储过程的测试
- plsql中存储过程的测试方法
- Jemeter 测试存储过程的并发调用
- Spring 调用ORACLE存储过程的结果
- spring中对存储过程的调用
- spring调用oracle的存储过程
- spring的jdbcTemplate调用存储过程
- 关于SPRING调用ORACLE的存储过程
- 分享一下中国老韩很牛的解说
- 我对系统分析的简单化理解-读"做自己的系统分析师"一文
- New 和 GetMem 的不同之处
- 经典软件工程对照现代软件工程
- 百度天生八宗罪
- 测试spring的存储过程
- 如何让客户接受高价位产品(技巧)
- 可对任意类型数据进行编码的Base64编解码源码
- FreeBSD内核配置文件详解 (转)
- 软考之路[数据库系统工程师]
- 随便
- COTS应用程序开发框架简介(三)
- 书
- Money is not everthing