java調(diào)用Oracle有返回值的存儲過程

package testcase;

import java.sql.CallableStatement;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

import java.sql.Types;

/**

* 調(diào)用有返回值的存儲過程

* @author study_monkey

*? 數(shù)據(jù)庫新建表

* create table Test_tbl

(

i_id? INTEGER,

i_name VARCHAR2(20)

)

*新建存儲過程

* CREATE OR REPLACE PROCEDURE Test_ProduceOut(PARA1 IN VARCHAR2,PARA2 OUT VARCHAR2) AS

BEGIN

select i_name into PARA2 from TEST_pro where i_id = PARA1;

END Test_ProduceOut;

* @param args

*/

public class TestCallProduceOut {

public TestCallProduceOut() {

}

private static final String DRIVER="oracle.jdbc.driver.OracleDriver";

private static final String URL="jdbc:oracle:thin:@127.0.0.1:1521:testdb";

private String name="username";

private static String pwd="password";

Connection conn = null;

Statement stmt = null;

ResultSet rs = null;

CallableStatement proc = null;

public void callProduce(){

try {

Class.forName(DRIVER);

conn = DriverManager.getConnection(URL,name,pwd);

proc = conn.prepareCall("{ call Test_ProduceOut(?,?) }"); //調(diào)用存儲過程

proc.setString(1, "1"); //第一個輸入?yún)?shù)

proc.registerOutParameter(2, Types.VARCHAR);//第二個參數(shù)輸出參數(shù),是VARCHAR類型的

proc.execute();//執(zhí)行

String getOut= proc.getString(2);//獲得輸出參數(shù)

System.out.println("存儲過程返回的值是:"+getOut);

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

}

}

}

public static void main(String[] args) {

TestCallProduceOut test = new TestCallProduceOut();

test.callProduce();

}

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容