Oracle進階編程,oraclesql進階編程

來源:互聯網
上載者:User

Oracle進階編程,oraclesql進階編程

Oracle的進階編程主要是涉及到資料庫的十大對象的使用,在這裡,和大家分享一下我在學習這些技能時的一些筆記。希望對大家有所協助!

閱讀本文之前,您還可以查閱我之前關於資料庫方面的介紹:

Oracle DDL基本操作
Oracle基礎資料型別 (Elementary Data Type) 
OracleDatabase Backup與恢複 

接下來介紹一下 Oracle的進階編程。


1、預存程序返回單值參數
 
   *call + 包名 + 預存程序名(傳入、傳出值)
    String str = "{call smsbusiness.deleteZhZMember(?,?,?)}";


   *建立串連
    Connection conn = null;
    conn = DriverManager.getConnection();
  
   *使用java.sql.*類
    CallableStatement cs = conn.prepareCall(str);
  
   *傳入參數
    cs.setInt(1,id);
    cs.setInt(2,-2);

   *設定out值
    cs.registerOutputParameter(3,Types.NUMBERIC);

   *執行
    cs.excuse();

2、預存程序返回多值參數

   *包的建立   
    CREATE OR REPLACE PACKAGE TESTPACKAGE  AS
    TYPE Test_CURSOR IS REF CURSOR;
    END TESTPACKAGE;
 
   *遊標的建立
    CREATE OR REPLACE PROCEDURE TESTC(p_CURSOR out TESTPACKAGE.Test_CURSOR) IS
    BEGIN
      OPEN p_CURSOR FOR SELECT * FROM grade;
    END TESTC;


   *Java的調用
    CallableStatement cs = conn.prepareCall(sqlStr);
    ResultSet rs = (ResultSet)cs.getObject(2);


3、無返回值預存程序樣本

 CREATE OR REPLACE PROCEDURE TESTA(PARA1 IN VARCHAR2,PARA2 IN VARCHAR2) AS    BEGIN      INSERT INTO HYQ.B_ID (I_ID,I_NAME) VALUES (PARA1, PARA2);    END TESTA;
package com.hyq.src;    import java.sql.*;   import java.sql.ResultSet;    public class TestProcedureOne {     public TestProcedureOne() {     }     public static void main(String[] args ){       String driver = "oracle.jdbc.driver.OracleDriver";       String strUrl = "jdbc:oracle:thin:@127.0.0.1:1521: hyq ";       Statement stmt = null;       ResultSet rs = null;       Connection conn = null;       CallableStatement cstmt = null;        try {          Class.forName(driver);          conn = DriverManager.getConnection(strUrl, " zl ", "123456");          CallableStatement proc = null; //建立執行預存程序的對象          proc = conn.prepareCall("{ call HYQ.TESTA(?,?) }"); //設定預存程序 call為關鍵字.          proc.setString(1, "100"); //設定第一個輸入參數          proc.setString(2, "TestOne");//設定第二個輸入參數          proc.execute();//執行        }        catch (SQLException ex2) {          ex2.printStackTrace();        }        catch (Exception ex2) {          ex2.printStackTrace();        }        finally{          try {            if(rs != null){            rs.close();            if(stmt!=null){            stmt.close();          }          if(conn!=null){            conn.close();          }        }      }      catch (SQLException ex1) {      }     }    } 

4、基本返回值預存程序樣本
 CREATE OR REPLACE PROCEDURE TESTB(PARA1 IN VARCHAR2,PARA2 OUT VARCHAR2) AS   BEGIN       SELECT INTO PARA2 FROM TESTTB WHERE I_ID= PARA1;   END TESTB;

 //Java代碼:    package com.hyq.src;    public class TestProcedureTWO {        public TestProcedureTWO() {        }        public static void main(String[] args ){            String driver = "oracle.jdbc.driver.OracleDriver";            String strUrl = "jdbc:oracle:thin:@127.0.0.1:1521:zl";            Statement stmt = null;            ResultSet rs = null;            Connection conn = null;            try {                Class.forName(driver);                conn = DriverManager.getConnection(strUrl, " hyq ", " hyq ");                CallableStatement proc = null;                proc = conn.prepareCall("{ call zl.TESTB(?,?) }"); //設定預存程序                proc.setString(1, "100");//設定第一個參數輸入參數                proc.registerOutParameter(2, Types.VARCHAR);//第二個參數輸出參數,是VARCHAR類型的                proc.execute();//執行                String testPrint = proc.getString(2);//獲得輸出參數                System.out.println("=testPrint=is="+testPrint);            }catch(Exception e){            }finally{            }        }    }

5、集合返回值預存程序

//資料庫端:    CREATE OR REPLACE PACKAGE TESTPACKAGE  AS        TYPE Test_CURSOR IS REF CURSOR;    end TESTPACKAGE;    CREATE OR REPLACE PROCEDURE TESTC(p_CURSOR out TESTPACKAGE.Test_CURSOR) IS    BEGIN        OPEN p_CURSOR FOR SELECT * FROM HYQ.TESTTB;    END TESTC;

  //Java代碼:    package com.hyq.src;    import java.sql.*;    import java.io.OutputStream;    import java.io.Writer;    import java.sql.PreparedStatement;    import java.sql.ResultSet;    import oracle.jdbc.driver.*;       public class TestProcedureTHREE {        public TestProcedureTHREE() {        }    public static void main(String[] args ){        String driver = "oracle.jdbc.driver.OracleDriver";        String strUrl = "jdbc:oracle:thin:@127.0.0.1:1521:zl";        Statement stmt = null;        ResultSet rs = null;        Connection conn = null;         try {            Class.forName(driver);            conn = DriverManager.getConnection(strUrl, "hyq", "hyq");             CallableStatement proc = null;            proc = conn.prepareCall("{ call hyq.testc(?) }"); //預存程序 hyq包下的            proc.registerOutParameter(1,oracle.jdbc.OracleTypes.CURSOR);//設定輸出參數是一個遊標.第一個參數,遊標類型            proc.execute();//執行            rs = (ResultSet)proc.getObject(1); //獲得第一個參數是一個遊標,轉化成ResultSet類型             while(rs.next()) //獲得資料            {                System.out.println(rs.getString(1) +rs.getString(2));             }        }catch (Exception ex2) {            ex2.printStackTrace();        }finally{        }       }      }





著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.