Java調用預存程序的2種方法

來源:互聯網
上載者:User

creat  proc proc_select
    @pid varchar(20)
    @address varchar(20) output
    as
    select @address=address from userinfo where pid=@pid
    go

用java調用:
    class.forName(/"sun.jdbc.odbc.JdbcOdbcDriver/");   //載入驅動
    Connection con=DriverManager.getConnection(/"Jdbc:Odbc:test/",/"sa/",/"/");    //獲得串連
    String call=/"{call proc_select(?,?)};/"          //調用語句
    CallableStatement proc=con.preparecall(call);     //調用預存程序
    proc.setString(1,/"12345678/");                  //給輸入參數傳值
    proc.registerOutParameter(2,Type.varchar);       //聲明輸出參數是什麼類型的
    proc.execute();                                  //執行
    String address=proc.getString(2);                //獲得輸出參數

    java調用預存程序2

      預存程序可以有傳回值,所以CallableStatement類有類似getResultSet這樣的方法來擷取傳回值。當預存程序返回一個值時,你必須使用registerOutParameter方法告訴JDBC磁碟機該值的SQL類型是什麼。你也必須調整預存程序調用來指示該過程返回一個值。
    下面接著上面的例子。這次我們查詢Dylan Thomas逝世時的年齡。這次的預存程序使用PostgreSQL的pl/pgsql:
    create function snuffed_it_when (VARCHAR) returns integer ’declare
    poet_id NUMBER;
    poet_age NUMBER;
    begin
    --first get the id associated with the poet.
    SELECT id INTO poet_id FROM poets WHERE name = $1;
    --get and return the age.
    SELECT age INTO poet_age FROM deaths WHERE mort_id = poet_id;  [Page]
    return age;
    end;’ language ’pl/pgsql’;
    另外,注意pl/pgsql參數名通過Unix和DOS指令碼的$n文法引用。同時,也注意嵌入的注釋,這是和Java代碼相比的另一個優越性。在Java中寫這樣的注釋當然是可以的,但是看起來很淩亂,並且和SQL語句脫節,必須嵌入到Java String中。

    下面是調用這個預存程序的Java代碼:
    connection.setAutoCommit(false);
    CallableStatement proc = connection.prepareCall(/"{ ? = call snuffed_it_when(?) }/");
    proc.registerOutParameter(1, Types.INTEGER);
    proc.setString(2, poetName);
    cs.execute();
    int age = proc.getInt(2);

    如果指定了錯誤的傳回值類型會怎樣?那麼,當調用預存程序時將拋出一個RuntimeException,正如你在ResultSet操作中使用了一個錯誤的類型所碰到的一樣。

    複雜的傳回值

    關於預存程序的知識,很多人好像就熟悉我們所討論的這些。如果這是預存程序的全部功能,那麼預存程序就不是其它遠程執行機制的替換方案了。預存程序的功能比這強大得多。
    當你執行一個SQL查詢時,DBMS建立一個叫做cursor(遊標)的資料庫物件,用於在返回結果中迭代每一行。ResultSet是目前時間點的遊標的一個表示。這就是為什麼沒有緩衝或者特定資料庫的支援,你只能在ResultSet中向前移動。
    某些DBMS允許從預存程序中返回遊標的一個引用。JDBC並不支援這個功能,但是Oracle、PostgreSQL和DB2的JDBC磁碟機都支援在ResultSet上開啟到遊標的指標(pointer)。
    設想列出所有沒有活到退休年齡的詩人,下面是完成這個功能的預存程序,返回一個開啟的遊標,同樣也使用PostgreSQL的pl/pgsql語言:
    create procedure list_early_deaths () return refcursor as ’declare
    toesup refcursor;
    begin
    open toesup for SELECT poets.name, deaths.age FROM poets, deaths
    -- all entries in deaths are for poets. -- but the table might become generic.
    WHERE poets.id = deaths.mort_id AND deaths.age < 60;
    return toesup;
    end;’ language ’plpgsql’;

    下面是調用該預存程序的Java方法,將結果輸出到PrintWriter:
    PrintWriter:
    static void sendEarlyDeaths(PrintWriter out){
    Connection con = null;

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.