隨筆 - 6  文章 - 129  trackbacks - 0
          <2025年6月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          常用鏈接

          留言簿(14)

          隨筆檔案(6)

          文章分類(467)

          文章檔案(423)

          相冊

          收藏夾(18)

          JAVA

          搜索

          •  

          積分與排名

          • 積分 - 827222
          • 排名 - 49

          最新評論

          閱讀排行榜

          評論排行榜

           

          調用無入參的函數

          函數定義

          CREATE OR REPLACE Function f_getstring Return Varchar2 Is
          Begin
           Return ''String value'';
          End f_getstring;

          調用函數的Java片斷

             CallableStatement cstmt = con.prepareCall("{?=call f_getstring}");
             cstmt.registerOutParameter(1, Types.VARCHAR);
             cstmt.execute();
             String strValue = cstmt.getString(1);
             System.out.println("The return value is:" + strValue);
             cstmt.close();

          調用有一個入參,一個輸出參數以及一個字符串返回值的函數

          函數定義

          CREATE OR REPLACE Function f_Getinfo(Id Integer, Age Out Integer) Return Varchar2 Is
          Begin
           Age := 10;
           Return ''The age is:'' || Id;
          End f_Getinfo;

          調用函數的Java代碼片斷

             CallableStatement cstmt = con
               .prepareCall("{?=call f_getinfo(?,?)}");
             cstmt.registerOutParameter(1, Types.VARCHAR);
             cstmt.setInt(2, 11);
             cstmt.registerOutParameter(3, Types.INTEGER);
             cstmt.execute();
             String strValue = cstmt.getString(1);
             int age = cstmt.getInt(3);
             System.out.println("The return value is:" + strValue
               + " and age is:" + age);
             cstmt.close();



          posted on 2009-05-12 10:27 Ke 閱讀(6370) 評論(2)  編輯  收藏 所屬分類: oraclejava

          FeedBack:
          # re: Java調用Oracle函數 2013-05-12 15:02 wms
          package com;

          import java.sql.CallableStatement;
          import java.sql.Connection;
          import java.sql.DriverManager;
          import java.sql.SQLException;
          import java.sql.Types;

          public class TestProcedureOne {

          public TestProcedureOne() {

          }

          public static void main(String[] args) {

          String driver = "oracle.jdbc.driver.OracleDriver";
          String strUrl = "jdbc:oracle:thin:@localhost:1521:orcl";
          Connection conn = null;
          CallableStatement cstmt = null;

          try {

          Class.forName(driver);
          conn = DriverManager.getConnection(strUrl, "wms", "wms");

          cstmt = conn.prepareCall("{?=call add_three_numbers(?,?,?)}");

          cstmt.registerOutParameter(1, Types.INTEGER);

          cstmt.setInt(2,4);
          cstmt.setInt(3,5);
          cstmt.setInt(4,6);

          cstmt.execute();
          System.out.println("調用函數add_three_numbers:"+cstmt.getInt(1));

          } catch (SQLException ex2) {
          ex2.printStackTrace();
          } catch (Exception ex2) {
          ex2.printStackTrace();
          }

          finally {

          try {

          if (cstmt != null) {
          cstmt.close();
          if (conn != null) {
          conn.close();
          }
          }
          } catch (SQLException e) {
          System.out.println("SQL state" + e.getSQLState());
          System.out.println("錯誤消息" + e.getMessage());
          System.out.println("錯誤代碼" + e.getErrorCode());
          e.printStackTrace();
          }

          }

          }

          }
            回復  更多評論
            
          # re: Java調用Oracle函數 2013-05-12 15:04 wms
          補充:上例使用的函數(oracle 11g中編寫)
          代碼:

          create or replace FUNCTION add_three_numbers ( a NUMBER:=0, b NUMBER:=0, c NUMBER:=0 )
          RETURN NUMBER
          AS
          Result1 NUMBER:=0;
          BEGIN
          Result1:=(a+b+c);
          RETURN Result1;
          END add_three_numbers;

            回復  更多評論
            
          主站蜘蛛池模板: 闸北区| 合作市| 绥江县| 都安| 阳泉市| 什邡市| 东城区| 梅河口市| 北碚区| 稻城县| 西乡县| 崇左市| 泌阳县| 罗源县| 鹤岗市| 青龙| 马尔康县| 临沭县| 巴林左旗| 大兴区| 五家渠市| 佛山市| 靖安县| 巴楚县| 汝城县| 和顺县| 玉门市| 阜平县| 五华县| 伊宁县| 枣强县| 肇东市| 博爱县| 丘北县| 政和县| 扶绥县| 茶陵县| 嘉义县| 千阳县| 彰化市| 丰原市|