1. 程式人生 > >java調用Oracle中的存儲過程與存儲函數

java調用Oracle中的存儲過程與存儲函數

div edr oid rac gist acl ble ava exception

 1 //調用存儲過程
 2     public static void testPro(){
 3         String driver = "oracle.jdbc.OracleDriver";
 4         String url = "jdbc:oracle:thin:@localhost:1521:orcl";
 5         Connection conn = null;
 6         CallableStatement cs = null;
 7         
 8         try {
 9             Class.forName(driver);
10 conn = DriverManager.getConnection(url, "scott", "tiger"); 11 cs = conn.prepareCall("{call countyearsal(?,?)}"); 12 cs.setInt(1, 7369); 13 //註冊oracle輸出參數的類型 14 cs.registerOutParameter(2, OracleTypes.NUMBER); 15 cs.execute(); 16 int
ysal = cs.getInt(2); 17 System.out.println(ysal); 18 } catch (Exception e) { 19 e.printStackTrace(); 20 }finally { 21 try { 22 if(cs != null){ 23 cs.close(); 24 } 25 if(conn != null
){ 26 conn.close(); 27 } 28 } catch (SQLException e) { 29 e.printStackTrace(); 30 } 31 } 32 } 33 //調用存儲函數 34 public static void testFun(){ 35 String driver = "oracle.jdbc.OracleDriver"; 36 String url = "jdbc:oracle:thin:@localhost:1521:orcl"; 37 Connection conn = null; 38 CallableStatement cs = null; 39 40 try { 41 Class.forName(driver); 42 conn = DriverManager.getConnection(url, "scott", "tiger"); 43 cs = conn.prepareCall("{?=call countysal(?)}"); 44 cs.setInt(2, 7369); 45 //註冊oracle輸出參數的類型 46 cs.registerOutParameter(1, OracleTypes.NUMBER); 47 cs.execute(); 48 int ysal = cs.getInt(1); 49 System.out.println(ysal); 50 } catch (Exception e) { 51 e.printStackTrace(); 52 }finally { 53 try { 54 if(cs != null){ 55 cs.close(); 56 } 57 if(conn != null){ 58 conn.close(); 59 } 60 } catch (SQLException e) { 61 e.printStackTrace(); 62 } 63 } 64 }

java調用Oracle中的存儲過程與存儲函數