java调用Oracle中的存储过程与存储函数

 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     }

 

posted @ 2018-08-22 00:25  cat_fish  阅读(427)  评论(0编辑  收藏  举报