|
create or replace procedure p--創(chuàng)建過程
(v_a in number, v_b number, v_ret out number, v_temp in out number ) is --v_temp即做輸出又做輸入,輸出就是服務(wù)器根據(jù)用戶輸入向客戶端輸出 begin
if (v_a > v_b) then v_ret := v_a; else v_ret := v_b; end if; v_temp := v_temp + 1; end; 以下是java中的代碼
import java.sql.*;
public class TestProc {
/**
* @param args */ public static void main(String[] args) { // TODO Auto-generated method stub String url = "jdbc:oracle:thin:@127.0.0.1:1521:ORCL"; Connection conn = null; CallableStatement cstmt = null; try { Class.forName("oracle.jdbc.driver.OracleDriver");//lomboz_eclipse是我設(shè)置scott用戶的密碼而已 conn = DriverManager.getConnection(url,"scott","lomboz_eclipse"); System.out.println("aa"); cstmt = conn.prepareCall("{call p(?,?,?,?)}"); cstmt.setInt(1, 5);//輸入?yún)?shù),直接設(shè)置參數(shù)值就可以了 cstmt.setInt(2, 6); cstmt.registerOutParameter(3, Types.INTEGER); //輸出參數(shù),要顯示出來需注冊和打印 cstmt.registerOutParameter(4, Types.INTEGER); cstmt.setInt(4, 8); cstmt.execute(); System.out.println(cstmt.getInt(3)); System.out.println(cstmt.getInt(4)); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block System.out.println("class should be in the correct adress"); e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } finally { try { if(cstmt!=null) { cstmt.close();//這里還可以加一步,將cstmt設(shè)置為null,雖然結(jié)果一樣但效果好那么一絲絲 } if(conn!=null) { conn.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
|
|
|