1:创建存储过程
此存储过程通过传入的参数(Name),返回一个参数(address)。
create or replace procedure demo_procedure(namedemo in varchar2,addressdemo out varchar2)
as
begin
select address into addressdemo from system.demo where name=namedemo;
end;
2:Java代码:
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; public class TestProcedureTwo { public TestProcedureTwo() { } public static void main(String[] args ){ String driver = "oracle.jdbc.driver.OracleDriver"; String strUrl = "jdbc:oracle:thin:@localhost:1521:myoracle"; Statement stmt = null; ResultSet rs = null; Connection conn = null; try { Class.forName(driver); conn = DriverManager.getConnection(strUrl, "system", "admin"); CallableStatement proc = null; proc = conn.prepareCall("{ call dem_procedure(?,?) }"); proc.setString(1, "kalision"); proc.registerOutParameter(2, Types.VARCHAR); proc.execute(); String testPrint = proc.getString(2); System.out.println("存储过程返回的值是:"+testPrint); } 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) { } } } }
注意:记得导入jdbc驱动的jar包。