摘要:
create or replace procedure sp_value(
id1 in number,
id2 out number
)
as
begin
id2 := id1*200;
end;
-----------------调用(有简单返回值)----------------
Connection conn = null;
CallableStatement cstmt = null;
conn = DriverManager.getConnection(url, user, password);
String procedure = "{call sp_value(?,?)}";
cstmt = conn.prepareCall(procedure);
--------------------JDBC获取存储过程中的值-------------------
//学号、姓名、班级编号
cstmt.setString(1, "2000");
//注册输出参数
cstmt.registerOutParameter(2, java.sql.Types.INTEGER);
cstmt.executeUpdate();
//从输出参数中获取值
int value = cstmt.getInt(2);
System.out.println("返回: "+value);
利用spring的jdbcTemplate调用存储过程
假如我们有P_GET_TOPIC_NUM这一个存储过程,有两个参数,第一个参数userId是传进去的,第二个参数是传出来的,举例如下:
Java代码
1. public int getUserTopicNum(final int userId) {
2. String sql = "{call P_GET_TOPIC_NUM(?,?)}";
3. Object execute(String callString, CallableStatementCallback action)接口l//使用
4. Object obj = getJdbcTemplate().execute(sql,new CallableStatementCallback(){
5. public Object doInCallableStatement(CallableStatement cs) throws SQLException, DataAccessException {
6. cs.setInt(1,userId);
7. cs.registerOutParameter(2, Types.INTEGER);
8. cs.execute();
9. return new Integer(cs.getInt(2));
10. }
11. });
12. return ((Integer)obj).intValue();
} |