Oralce存储过程的创建和调用

来源:互联网 发布:模拟经营手机游戏知乎 编辑:程序博客网 时间:2024/05/22 21:16

表名字:wawa

表结构:

IDUserNameUserAgeUserNumber1hello01002world11013vvin21024ggth31035gg4104

创建存储过程:

create or replace procedure spStr(str out clob)isbegin  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





原创粉丝点击