Oralce存储过程的创建和调用

3 篇文章 0 订阅
3 篇文章 0 订阅
本文介绍了如何在Oracle数据库中创建和调用存储过程,通过示例展示了针对名为'wawa'的表操作的存储过程,并展示了调用存储过程后的结果变量'resultStr'。
摘要由CSDN通过智能技术生成

表名字:wawa

表结构:

IDUserNameUserAgeUserNumber
1hello0100
2world1101
3vvin2102
4ggth3103
5gg4104

创建存储过程:

create or replace procedure spStr(str out clob)
is
begin
  select userName into str from wawa where ID=2;
end;
/


存储过程调用:

import java.sql.CallableStatement;
import java.sql.Clob;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Types;
import org.hibernate.Session;
import dao.HibernateSessionFactory;

public class OracleProcedureTest {
	public static void main(String[] args) throws SQLException {

		//******************************存储过程调用****************************************
		Session session = null;
		Connection conn = null;
		CallableStatement csmt = null;
		Clob resultClob = null;
		String resultStr = null;

		try {
			String sql = "{call spStr(?)}";
			session = HibernateSessionFactory.getSession();
			conn = session.connection();
			csmt = conn.prepareCall(sql);
			csmt.registerOutParameter(1, Types.CLOB);
			csmt.execute();
			resultClob = csmt.getClob(1);
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			if(session!=null) session.close();
			if(conn!=null) conn.close();
			if(csmt!=null) csmt.close();
		}

		resultStr = resultClob.getSubString((long) 1, (int) resultClob.length());

		System.out.println("resultStr = " + resultStr);	
	
		/*****************************存储过程调用结束**************************************/
	}
			
}

调用结果:

resultStr = world





  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值