kooyee ‘s blog

          開源軟件, 眾人努力的結晶, 全人類的共同財富
          posts - 103, comments - 55, trackbacks - 0, articles - 66
             :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          [JDBC]調用Database function - Oracle

          Posted on 2007-07-17 22:00 kooyee 閱讀(759) 評論(0)  編輯  收藏 所屬分類: Java Database數據庫技術
          import java.sql.*;

          public class StProcExample {
           
          public static void main(String[] args)
           
          throws SQLException {
          int ret_code;
          Connection conn 
          = null;
           
          try {
            
          //Load and register Oracle driver
            DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
           
          //Establish a connection

           conn 
          = DriverManager.getConnection("jdbc:oracle:thin:@training:1521:
           Oracle""oratest""oratest");
           int i_deptno = 10;
           CallableStatement pstmt 
          = conn.prepareCall("{call p_highest_
           paid_emp(?,?,?,?)}
          ");
           pstmt.setInt(1, i_deptno);
           pstmt.registerOutParameter(
          2, Types.INTEGER);
           pstmt.registerOutParameter(
          3, Types.VARCHAR);
           pstmt.registerOutParameter(
          4, Types.FLOAT);
           pstmt.executeUpdate();

           
          int o_empno = pstmt.getInt(2);
           String o_ename 
          = pstmt.getString(3);
           
          float o_sal = pstmt.getFloat(4);
           System.out.print(
          "The highest paid employee in dept "
           
          +i_deptno+" is: "+o_empno+" "+o_ename+" "+o_sal);
           pstmt.close();
           conn.close();
            }
           catch (SQLException e) {ret_code = e.getErrorCode();
             System.err.println(ret_code 
          + e.getMessage()); conn.close();}

           }

          }



          在database中調用function

          Select ValidWorkingDay(param1,prasm2) as IsWorking from dual 
          Select 
          {t '14:20:50'} as ThisDate from dual
          Select UCA_VALIDATE(
          '123456','WW','1') as Answer from dual
          或者
          SELECT ename FROM emp WHERE hiredate 
          = {t '12:00:00'}"

          Update語句
          For example, instead of using the fn keyword in embedded SQL92 syntax:

          Statement stmt 
          = conn.createStatement ();
          stmt.executeUpdate(
          "UPDATE emp SET ename = {fn CONCAT('My', 'Name')}");


          Use Oracle SQL syntax:

          stmt.executeUpdate(
          "UPDATE emp SET ename = CONCAT('My', 'Name')");


           

          Function Call Syntax

          Oracle's JDBC drivers support the following procedure and function call syntax:

          Procedure calls (without a return value):

          { call procedure_name (argument1, argument2,...) }
          
          

          Function calls (with a return value):

          { ? = call procedure_name (argument1, argument2,...) }
          當用于call Function時,在等號= 前的?是statment的第一個parameter. 是out出來的值
          
          主站蜘蛛池模板: 天峻县| 海伦市| 汶川县| 将乐县| 阜新市| 乌兰察布市| 忻州市| 镇平县| 伊通| 北流市| 恭城| 营口市| 德惠市| 朔州市| 定兴县| 五大连池市| 临泉县| 高唐县| 巢湖市| 财经| 乌拉特中旗| 栾川县| 明溪县| 固安县| 金川县| 巩义市| 星座| 东阳市| 温宿县| 顺昌县| 花莲市| 上虞市| 韩城市| 舒城县| 郸城县| 腾冲县| 博湖县| 灵台县| 深州市| 扎赉特旗| 东明县|