Oracle 在JDBC中使用 存储过程,包
Posted 阿肯新
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Oracle 在JDBC中使用 存储过程,包相关的知识,希望对你有一定的参考价值。
前提:
在Oracle中已经定义 存储过程 和 存储函数 和 包
导入了Oracle的JDBC jar 包
1 package demo; 2 3 import java.sql.Connection; 4 import java.sql.DriverManager; 5 import java.sql.ResultSet; 6 import java.sql.SQLException; 7 import java.sql.Statement; 8 9 public class JDBCUtils { 10 private static String driver = "oracle.jdbc.OracleDriver"; 11 private static String url = "jdbc:oracle:thin:@192.168.56.101:1521:orcl"; 12 private static String user = "scott"; 13 private static String password = "tiger"; 14 15 static{ 16 try { 17 Class.forName(driver); 18 } catch (ClassNotFoundException e) { 19 throw new ExceptionInInitializerError(e); 20 } 21 //DriverManager.registerDriver(driver) 22 } 23 24 public static Connection getConnection(){ 25 try { 26 return DriverManager.getConnection(url, user, password); 27 } catch (SQLException e) { 28 e.printStackTrace(); 29 } 30 return null; 31 } 32 33 /* 34 * 运行Java程序 35 * java -Xms100M -Xmx200M HelloWorld 36 * 37 * 技术方向: 38 * 1. 性能调优 ---> tomcat 39 * 2. 故障诊断 ---> 死锁 40 */ 41 public static void release(Connection conn,Statement st,ResultSet rs){ 42 if(rs != null){ 43 try { 44 rs.close(); 45 } catch (SQLException e) { 46 e.printStackTrace(); 47 }finally{ 48 rs = null;// ----> java GC 49 } 50 } 51 if(st != null){ 52 try { 53 st.close(); 54 } catch (SQLException e) { 55 e.printStackTrace(); 56 }finally{ 57 st = null; 58 } 59 } 60 if(conn != null){ 61 try { 62 conn.close(); 63 } catch (SQLException e) { 64 e.printStackTrace(); 65 }finally{ 66 conn = null; 67 } 68 } 69 } 70 }
1 package demo; 2 3 import java.sql.CallableStatement; 4 import java.sql.Connection; 5 import java.sql.ResultSet; 6 7 import oracle.jdbc.OracleCallableStatement; 8 import oracle.jdbc.OracleTypes; 9 10 import org.junit.Test; 11 12 public class TestOracle { 13 /* 14 * create or replace procedure queryempinfo(eno in number, 15 pename out varchar2, 16 psal out number, 17 pjob out varchar2) 18 */ 19 @Test 20 public void testProcedure(){ 21 //{call <procedure-name>[(<arg1>,<arg2>, ...)]} 22 String sql = "{call queryempinfo(?,?,?,?)}"; 23 24 Connection conn = null; 25 CallableStatement call = null; 26 try { 27 conn = JDBCUtils.getConnection(); 28 call = conn.prepareCall(sql); 29 30 //对于in参数,赋值 31 call.setInt(1, 7839); 32 33 //对于out参数,申明 34 call.registerOutParameter(2, OracleTypes.VARCHAR); 35 call.registerOutParameter(3, OracleTypes.NUMBER); 36 call.registerOutParameter(4, OracleTypes.VARCHAR); 37 38 //执行 39 call.execute(); 40 41 //取出结果 42 String name = call.getString(2); 43 double sal = call.getDouble(3); 44 String job = call.getString(4); 45 System.out.println(name+"\t"+sal+"\t"+job); 46 } catch (Exception e) { 47 e.printStackTrace(); 48 }finally{ 49 JDBCUtils.release(conn, call, null); 50 } 51 } 52 53 /* 54 * create or replace function queryempincome(eno in number) 55 return number 56 */ 57 @Test 58 public void testFunction(){ 59 //{?= call <procedure-name>[(<arg1>,<arg2>, ...)]} 60 String sql = "{?=call queryempincome(?)}"; 61 62 Connection conn = null; 63 CallableStatement call = null; 64 try { 65 conn = JDBCUtils.getConnection(); 66 call = conn.prepareCall(sql); 67 68 //第一个是out参数 69 call.registerOutParameter(1, OracleTypes.NUMBER); 70 //第二个是in参数 71 call.setInt(2, 7839); 72 73 call.execute(); 74 75 //取出年收入 76 double income = call.getDouble(1); 77 System.out.println(income); 78 } catch (Exception e) { 79 e.printStackTrace(); 80 }finally{ 81 JDBCUtils.release(conn, call, null); 82 } 83 } 84 85 86 @Test 87 public void testCursor(){ 88 String sql = "{call MYPAKCAGE.queryEmpList(?,?)}"; 89 90 Connection conn = null; 91 CallableStatement call = null; 92 ResultSet rs = null; 93 try { 94 conn = JDBCUtils.getConnection(); 95 call = conn.prepareCall(sql); 96 97 //对于in参数,赋值 98 call.setInt(1, 20); 99 //对于out参数 申明 100 call.registerOutParameter(2, OracleTypes.CURSOR); 101 102 call.execute(); 103 104 //取出结果 105 rs = ((OracleCallableStatement)call).getCursor(2); 106 while(rs.next()){ 107 //取出一个员工 108 String name = rs.getString("ename"); 109 double sal = rs.getDouble("sal"); 110 System.out.println(name+"\t"+sal); 111 } 112 } catch (Exception e) { 113 e.printStackTrace(); 114 }finally{ 115 JDBCUtils.release(conn, call, rs); 116 } 117 } 118 }
以上是关于Oracle 在JDBC中使用 存储过程,包的主要内容,如果未能解决你的问题,请参考以下文章
在某些参数类型是用户定义的情况下,如何使用 JDBC/Spring 调用 Oracle 存储过程?
在 Oracle JDBC 中是不是可以批量存储过程调用和检索 OUT 参数?