标签:package ... out mat stc into dex nvl 变量
public class JDBCUtils { private static String driver = "oracle.jdbc.OracleDriver"; private static String url = "jdbc:oracle:thin:@192.168.79.128:1521/orcl"; private static String user = "scott"; private static String password = "tiger"; static{ //注册驱动 //DriverManager.registerDriver(driver) try { Class.forName(driver); } catch (ClassNotFoundException e) { throw new ExceptionInInitializerError(e); } } public static Connection getConnection(){ try { return DriverManager.getConnection(url, user, password); } catch (SQLException e) { e.printStackTrace(); } return null; } public static void release(Connection conn,Statement st,ResultSet rs){ if(rs != null){ try { rs.close(); } catch (SQLException e) { e.printStackTrace(); }finally{ rs = null; ///-----> 原因:Java GC: Java的GC不受代码的控制 } } if(st != null){ try { st.close(); } catch (SQLException e) { e.printStackTrace(); }finally{ st = null; } } if(conn != null){ try { conn.close(); } catch (SQLException e) { e.printStackTrace(); }finally{ conn = null; } } } }
--存储过程 create or replace procedure queryEmpInformation(eno in number, pename out varchar2, psal out number, pjob out varchar2) is begin select ename,sal,job into pename,psal,pjob from emp where empno = eno; end queryEmpInformation;
@Test public void testProcedure(){ //jdbc调用存储过程sql语句: //{call <procedure-name>[(<arg1>,<arg2>, ...)]} String sql = "{call queryEmpInformation(?,?,?,?)}"; Connection conn = null; CallableStatement call = null; try{ conn = JDBCUtils.getConnection(); call = conn.prepareCall(sql); //输入参数赋值 call.setInt(1, 7839); //输出参数,声明为输出参数 call.registerOutParameter(2, OracleTypes.VARCHAR); call.registerOutParameter(3, OracleTypes.NUMBER); call.registerOutParameter(4, OracleTypes.VARCHAR); //执行 call.execute(); //获取值 String name = call.getString(2); double sal = call.getDouble(3); String job = call.getString(4); System.out.println(name+"\t"+sal+"\t"+job); }catch (Exception e){ e.printStackTrace(); }finally{ JDBCUtils.release(conn, call, null); } }
--存储函数 create or replace function queryEmpIncome(eno in number) return number is psal emp.sal%type; pcomm emp.comm%type; begin select sal,comm into psal,pcomm from emp where empno = eno; return psal*12+nvl(pcomm,0); end queryEmpIncome;
@Test public void testFuntion(){ //jdbc调用存储函数sql语句: //{?= call <procedure-name>[(<arg1>,<arg2>, ...)]} String sql = "{? = call queryEmpIncome(?)}"; Connection conn = null; CallableStatement call = null; try{ conn = JDBCUtils.getConnection(); call = conn.prepareCall(sql); //注册输出参数 call.registerOutParameter(1, OracleTypes.NUMBER); //输入参数 call.setInt(2, 7839); //执行 call.execute(); //取出返回值 double income = call.getDouble(1); System.out.println("年薪:"+income); }catch (Exception e){ e.printStackTrace(); }finally{ JDBCUtils.release(conn, call, null); } }
--创建包头 create or replace package mypackage is --定义一个cursor类型变量 type empcursor is ref cursor; procedure queryEmpList(dno in number,empList out empcursor); end mypackage; --创建包体 create or replace package body mypackage is procedure queryEmpList(dno in number,empList out empcursor) as begin open empList for select * from emp where deptno = dno; end; end mypackage;
@Test public void testCursor(){ String sql = "call mypackage.queryEmpList(?,?)"; Connection conn = null; CallableStatement call = null; ResultSet rs = null; try{ conn = JDBCUtils.getConnection(); call = conn.prepareCall(sql); call.setInt(1, 20); call.registerOutParameter(2, OracleTypes.CURSOR); call.execute(); rs = ((OracleCallableStatement)call).getCursor(2); while(rs.next()){ String name = rs.getString("ename"); double sal = rs.getDouble("sal"); System.out.println(name+"\t"+sal); } }catch (Exception e){ e.printStackTrace(); }finally{ JDBCUtils.release(conn, call, null); } }
标签:package ... out mat stc into dex nvl 变量
原文地址:https://www.cnblogs.com/zsh-wj/p/9402486.html