create or replace procedure p--创建过程
(v_a in number, v_b number, v_ret out number, v_temp in out number ) is --v_temp即做输出又做输入,输出就是服务器根据用户输入向客户端输出 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是我设置scott用户的密码而已 conn = DriverManager.getConnection(url,"scott","lomboz_eclipse"); System.out.println("aa"); cstmt = conn.prepareCall("{call p(?,?,?,?)}"); cstmt.setInt(1, 5);//输入参数,直接设置参数值就可以了 cstmt.setInt(2, 6); cstmt.registerOutParameter(3, Types.INTEGER); //输出参数,要显示出来需注册和打印 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设置为null,虽然结果一样但效果好那么一丝丝 } if(conn!=null) { conn.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
|
|