九色国产,午夜在线视频,新黄色网址,九九色综合,天天做夜夜做久久做狠狠,天天躁夜夜躁狠狠躁2021a,久久不卡一区二区三区

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
用java調(diào)用oracle存儲(chǔ)過程總

一:無返回值的存儲(chǔ)過程

存儲(chǔ)過程為:

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;

然后呢,在java里調(diào)用時(shí)就用下面的代碼:

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: crm";

    Statement stmt = null;

    ResultSet rs = null;

    Connection conn = null;

    CallableStatement cstmt = null;

    try {

      Class.forName(driver);

      conn =  DriverManager.getConnection(strUrl, " crm", " crm");

      CallableStatement proc = null;

      proc = conn.prepareCall("{ call HYQ.TESTA(?,?) }");

      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) {

      }

    }

  }

}

當(dāng)然了,這就先要求要建張表TESTTB,里面兩個(gè)字段(I_ID,I_NAME)。

二:有返回值的存儲(chǔ)過程(非列表)

存儲(chǔ)過程為:

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里調(diào)用時(shí)就用下面的代碼:

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:crm";

    Statement stmt = null;

    ResultSet rs = null;

    Connection conn = null;

    try {

      Class.forName(driver);

      conn =  DriverManager.getConnection(strUrl, " crm", " crm");

      CallableStatement proc = null;

      proc = conn.prepareCall("{ call HYQ.TESTB(?,?) }");

      proc.setString(1, "100");

      proc.registerOutParameter(2, Types.VARCHAR);

      proc.execute();

      String testPrint = proc.getString(2);

      System.out.println("=testPrint=is="+testPrint);

    }

    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) {

      }

    }

  }

}

}

注意,這里的proc.getString(2)中的數(shù)值2并非任意的,而是和存儲(chǔ)過程中的out列對(duì)應(yīng)的,如果out是在第一個(gè)位置,那就是proc.getString(1),如果是第三個(gè)位置,就是proc.getString(3),當(dāng)然也可以同時(shí)有多個(gè)返回值,那就是再多加幾個(gè)out參數(shù)了。

三:返回列表

由于oracle存儲(chǔ)過程沒有返回值,它的所有返回值都是通過out參數(shù)來替代的,列表同樣也不例外,但由于是集合,所以不能用一般的參數(shù),必須要用pagkage.所以要分兩部分,

1,  建一個(gè)程序包。如下:

CREATE OR REPLACE PACKAGE TESTPACKAGE AS

TYPE Test_CURSOR IS REF CURSOR;

end TESTPACKAGE;

2,建立存儲(chǔ)過程,存儲(chǔ)過程為:

CREATE OR REPLACE PROCEDURE TESTC(p_CURSOR out TESTPACKAGE.Test_CURSOR) IS

BEGIN

    OPEN p_CURSOR FOR SELECT * FROM HYQ.TESTTB;

END TESTC;

可以看到,它是把游標(biāo)(可以理解為一個(gè)指針),作為一個(gè)out 參數(shù)來返回值的。

java里調(diào)用時(shí)就用下面的代碼:

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:crm";

    Statement stmt = null;

    ResultSet rs = null;

    Connection conn = null;

    try {

      Class.forName(driver);

      conn =  DriverManager.getConnection(strUrl, "crm", "crm");

      CallableStatement proc = null;

      proc = conn.prepareCall("{ call hyq.testc(?) }");

      proc.registerOutParameter(1,oracle.jdbc.OracleTypes.CURSOR);

      proc.execute();

      rs = (ResultSet)proc.getObject(1);

      while(rs.next())

      {

          System.out.println("<tr><td>" + rs.getString(1) + "</td><td>"+rs.getString(2)+"</td></tr>");

      }

    }

    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) {

      }

    }

  }

}

在這里要注意,在執(zhí)行前一定要先把oracle的驅(qū)動(dòng)包放到class路徑里,否則會(huì)報(bào)錯(cuò)的。



補(bǔ)充一個(gè)使用oracle存儲(chǔ)過程分頁的小例子:
1, 建一個(gè)程序包。如下:

CREATE OR REPLACE PACKAGE TESTPACKAGE AS

TYPE Test_CURSOR IS REF CURSOR;

end TESTPACKAGE;

2,建立存儲(chǔ)過程,存儲(chǔ)過程為:


create or replace procedure TESTC(p_CURSOR out TESTPACKAGE.Test_CURSOR,lowerNum in numeric,higherNum in numeric) is
begin
OPEN p_CURSOR FOR select * from ( select row_.*, rownum rownum_ from (select id,title,status from HYQTEST) row_ where rownum <= higherNum) where rownum_ >lowerNum;
end TESTC;

使用plsql測試:
declare
lowerNum integer;
higherNum integer;
id varchar2(10);
title varchar2(500);
status numeric;
c testpackage.Test_CURSOR;
rownum_ integer;
begin
lowerNum:=1;
higherNum:=10;
TESTC(c,lowerNum,higherNum);
LOOP
FETCH c INTO id,title,status,rownum_;
EXIT WHEN c%NOTFOUND;
DBMS_OUTPUT.PUT_LINE('=行號(hào)='||rownum_||'=='||id||'==='||title||'===='||status||'==');
END LOOP;
CLOSE c;

end;


本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
用java調(diào)用oracle存儲(chǔ)過程總結(jié) 3 - 返回列表-偶愛老婆-搜狐博客
Jsp中調(diào)用Oracle存儲(chǔ)過程的小例子
Java的JDBC操作
[整理]Oracle面試題(基礎(chǔ)篇)
Oracle把Array類型作為參數(shù)傳入存儲(chǔ)過程示例
請(qǐng)教JDBC怎么連接ORACLE數(shù)據(jù)庫
更多類似文章 >>
生活服務(wù)
熱點(diǎn)新聞
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服