表名字:wawa
表结构:
ID | UserName | UserAge | UserNumber |
1 | hello | 0 | 100 |
2 | world | 1 | 101 |
3 | vvin | 2 | 102 |
4 | ggth | 3 | 103 |
5 | gg | 4 | 104 |
创建存储过程:
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