/*
使用JDBC程序去調用並執行ORACLE數據庫中的存儲過程。
*/
import java.sql.*;
public class Test
{
public static void main(String[] args){
Connection conn = null;
CallableStatement callstmt = null;
try{
//1.註冊驅動
Class.forName("oracle.jdbc.driver.OracleDriver");
//2.獲取連接
conn = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:name","scott","tiger");
//3.獲取數據庫操作對象(專門執行存儲過程的數據庫操作對象)
String procedureSql = "{call sumPro(?,?)}";
callstmt = conn.prepareCall(procedureSql);
//給?賦值
callstmt.setInt(1,100);
callstmt.setInt(2,0);
callstmt.registerOutParameter(2,Types.INTEGER);
//4.執行存儲過程
callstmt.execute();
//獲取存儲過程返回的數據
int retValue = callstmt.getInt(2);
System.out.println(retValue);
}catch(Exception e){
e.printStackTrace();
}finally{
//5.釋放資源
if(callstmt!=null){
try{
callstmt.close();
}catch(Exception e){
e.printStackTrace();
}
}
if(conn!=null){
try{
conn.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
}
}
使用JDBC調用存儲過程
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.