Spring如何使用JdbcTemplate调用存储过程的三种情况
2015-04-20 22:50
751 查看
Spring的SimpleJdbcTemplate将存储过程的调用进行了良好的封装,下面列出使用JdbcTemplate调用Oracle存储过程的三种情况:
一、无返回值的存储过程调用
1、存储过程代码:
[html] view
plaincopy
create or replace procedure sp_insert_table(param1 in varchar2,param2 in varchar2) as
begin
insert into table MyTable (id,name) values ('param1 ','param2');
end sp_insert_table;
2、JdbcTemplate调用该存储过程代码:
[html] view
plaincopy
package com.dragon.test;
import org.springframework.jdbc.core.JdbcTemplate;
public class JdbcTemplateTest {
private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
public void test(){
this.jdbcTemplate.execute("call sp_insert_table('100001')");
}
}
二、有返回值的存储过程(非结果集)
1、存储过程代码:
[html] view
plaincopy
create or replace procedure sp_select_table (param1 in varchar2,param2 out varchar2) as
begin select into param2 from MyTable where ID = param1 ;
end sp_insert_table ;
2、JdbcTemplate调用该存储过程代码:
[html] view
plaincopy
public void test() {
String param2Value = (String) jdbcTemplate.execute(
new CallableStatementCreator() {
public CallableStatement createCallableStatement(Connection con) throws SQLException {
String storedProc = "{call sp_select_table (?,?)}";// 调用的sql
CallableStatement cs = con.prepareCall(storedProc);
cs.setString(1, "p1");// 设置输入参数的值
cs.registerOutParameter(2,OracleTypes.Varchar);// 注册输出参数的类型
return cs;
}
}, new CallableStatementCallback() {
public Object doInCallableStatement(CallableStatement cs) throws SQLException, DataAccessException {
cs.execute();
return cs.getString(2);// 获取输出参数的值
}
});
}
三、有返回值的存储过程(结果集)
1、存储过程代码:先创建程序包,因为Oracle存储过程所有返回值都是通过out参数返回的,列表同样也不例外,但由于是集合,所以不能用一般的参数,必须要用package:
[html] view
plaincopy
create or replace package mypackage as
type my_cursor is ref cursor;
end mypackage;
2、存储过程代码:可以看到,列表是通过把游标作为一个out参数来返回的。
[html] view
plaincopy
create or replace procedure sp_list_table(param1 in varchar2,param2 out mypackage.my_cursor) is
begin
open my_cursor for select * from myTable;
end sp_list_table;
3、JdbcTemplate调用该存储过程代码:
[html] view
plaincopy
public void test() {
List resultList = (List) jdbcTemplate.execute(
new CallableStatementCreator() {
public CallableStatement createCallableStatement(Connection con) throws SQLException {
String storedProc = "{call sp_list_table(?,?)}";// 调用的sql
CallableStatement cs = con.prepareCall(storedProc);
cs.setString(1, "p1");// 设置输入参数的值
cs.registerOutParameter(2, OracleTypes.CURSOR);// 注册输出参数的类型
return cs;
}
}, new CallableStatementCallback() {
public Object doInCallableStatement(CallableStatement cs) throws SQLException,DataAccessException {
List resultsMap = new ArrayList();
cs.execute();
ResultSet rs = (ResultSet) cs.getObject(2);// 获取游标一行的值
while (rs.next()) {// 转换每行的返回值到Map中
Map rowMap = new HashMap();
rowMap.put("id", rs.getString("id"));
rowMap.put("name", rs.getString("name"));
resultsMap.add(rowMap);
}
rs.close();
return resultsMap;
}
});
for (int i = 0; i < resultList.size(); i++) {
Map rowMap = (Map) resultList.get(i);
String id = rowMap.get("id").toString();
String name = rowMap.get("name").toString();
System.out.println("id=" + id + ";name=" + name);
}
}
作者:伫望碧落 出处:http://blog.csdn.net/cl05300629
[java] view
plaincopy
<pre code_snippet_id="190768" snippet_file_name="blog_20140217_3_4197233" name="code" class="java" style="font-size: 14px; line-height: 25.200000762939453px;"><pre code_snippet_id="190768" snippet_file_name="blog_20140217_3_4197233">
一、无返回值的存储过程调用
1、存储过程代码:
[html] view
plaincopy
create or replace procedure sp_insert_table(param1 in varchar2,param2 in varchar2) as
begin
insert into table MyTable (id,name) values ('param1 ','param2');
end sp_insert_table;
2、JdbcTemplate调用该存储过程代码:
[html] view
plaincopy
package com.dragon.test;
import org.springframework.jdbc.core.JdbcTemplate;
public class JdbcTemplateTest {
private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
public void test(){
this.jdbcTemplate.execute("call sp_insert_table('100001')");
}
}
二、有返回值的存储过程(非结果集)
1、存储过程代码:
[html] view
plaincopy
create or replace procedure sp_select_table (param1 in varchar2,param2 out varchar2) as
begin select into param2 from MyTable where ID = param1 ;
end sp_insert_table ;
2、JdbcTemplate调用该存储过程代码:
[html] view
plaincopy
public void test() {
String param2Value = (String) jdbcTemplate.execute(
new CallableStatementCreator() {
public CallableStatement createCallableStatement(Connection con) throws SQLException {
String storedProc = "{call sp_select_table (?,?)}";// 调用的sql
CallableStatement cs = con.prepareCall(storedProc);
cs.setString(1, "p1");// 设置输入参数的值
cs.registerOutParameter(2,OracleTypes.Varchar);// 注册输出参数的类型
return cs;
}
}, new CallableStatementCallback() {
public Object doInCallableStatement(CallableStatement cs) throws SQLException, DataAccessException {
cs.execute();
return cs.getString(2);// 获取输出参数的值
}
});
}
三、有返回值的存储过程(结果集)
1、存储过程代码:先创建程序包,因为Oracle存储过程所有返回值都是通过out参数返回的,列表同样也不例外,但由于是集合,所以不能用一般的参数,必须要用package:
[html] view
plaincopy
create or replace package mypackage as
type my_cursor is ref cursor;
end mypackage;
2、存储过程代码:可以看到,列表是通过把游标作为一个out参数来返回的。
[html] view
plaincopy
create or replace procedure sp_list_table(param1 in varchar2,param2 out mypackage.my_cursor) is
begin
open my_cursor for select * from myTable;
end sp_list_table;
3、JdbcTemplate调用该存储过程代码:
[html] view
plaincopy
public void test() {
List resultList = (List) jdbcTemplate.execute(
new CallableStatementCreator() {
public CallableStatement createCallableStatement(Connection con) throws SQLException {
String storedProc = "{call sp_list_table(?,?)}";// 调用的sql
CallableStatement cs = con.prepareCall(storedProc);
cs.setString(1, "p1");// 设置输入参数的值
cs.registerOutParameter(2, OracleTypes.CURSOR);// 注册输出参数的类型
return cs;
}
}, new CallableStatementCallback() {
public Object doInCallableStatement(CallableStatement cs) throws SQLException,DataAccessException {
List resultsMap = new ArrayList();
cs.execute();
ResultSet rs = (ResultSet) cs.getObject(2);// 获取游标一行的值
while (rs.next()) {// 转换每行的返回值到Map中
Map rowMap = new HashMap();
rowMap.put("id", rs.getString("id"));
rowMap.put("name", rs.getString("name"));
resultsMap.add(rowMap);
}
rs.close();
return resultsMap;
}
});
for (int i = 0; i < resultList.size(); i++) {
Map rowMap = (Map) resultList.get(i);
String id = rowMap.get("id").toString();
String name = rowMap.get("name").toString();
System.out.println("id=" + id + ";name=" + name);
}
}
作者:伫望碧落 出处:http://blog.csdn.net/cl05300629
[java] view
plaincopy
<pre code_snippet_id="190768" snippet_file_name="blog_20140217_3_4197233" name="code" class="java" style="font-size: 14px; line-height: 25.200000762939453px;"><pre code_snippet_id="190768" snippet_file_name="blog_20140217_3_4197233">
相关文章推荐
- [原]Spring如何使用JdbcTemplate调用存储过程的三种情况
- Spring如何使用JdbcTemplate调用存储过程的三种情况
- Spring如何使用JdbcTemplate调用存储过程的三种情况
- Spring如何使用JdbcTemplate调用存储过程的三种情况
- Spring如何使用JdbcTemplate调用存储过程的三种情况
- 使用Spring的JdbcTemplate调用Oracle的存储过程
- spring 中使用jdbctemplate调用Oracle的存储过程
- 使用Spring的JdbcTemplate调用Oracle的存储过程
- 使用Spring的JdbcTemplate调用Oracle的存储过程
- 在SSH中通过spring的jdbctemplate接口调用oracle数据库的函数与存储过程
- 轮询程序中使用的各种方法:spring调用;http调用;存储过程调用;
- 轮询程序中使用的各种方法:spring调用;http调用;存储过程调用;
- 轮询程序中使用的各种方法:spring调用;http调用;存储过程调用;
- 基于Spring Boot,使用JPA调用Sql Server数据库的存储过程并返回记录集合
- spring的jdbcTemplate调用存储过程
- 如何使用 ADO.NET 和 Visual C# .NET 调用带参数的存储过程
- 使用 Spring 框架调用 DB2 存储过程
- 如何使用 ADO.NET 和 Visual C# .NET 调用带参数的存储过程
- 如何调用 Oracle 存储过程在 Visual Basic.NET 使用 Microsoft Oracle 托管提供程序
- 如何调用 Oracle 存储过程在 Visual Basic.NET 使用 Microsoft Oracle 托管提供程序