--有输入和输出的存储过程 SQL> create or replace procedure sp_pro8( spNo in varchar2, spName out varchar2) is begin select sname into spName from student where sid= spNo; end;
1 package oracle; 2 3 import java.sql.CallableStatement; 4 import java.sql.Connection; 5 import java.sql.DriverManager; 6 import java.sql.SQLException; 7 8 import oracle.jdbc.OracleTypes; 9 10 11 public class Test02 { 12 13 public static void main(String[] args) { 14 // TODO Auto-generated method stub 15 Connection conn = null; 16 CallableStatement cs = null; 17 try { 18 //1.加载驱动 19 Class.forName("oracle.jdbc.driver.OracleDriver"); 20 21 //2.连接 22 String url = "jdbc:oracle:thin:@127.0.0.1:1521:ORACLE12C"; 23 String userName = "sys as SYSDBA"; 24 String password = "fairy6280"; 25 conn = DriverManager.getConnection(url,userName,password); 26 27 //3.创建CallableStatement 28 String procedure = "{call sp_pro8(?,?)}"; 29 cs = conn.prepareCall(procedure); 30 31 //赋值 32 cs.setInt(1, 1010); 33 cs.registerOutParameter(2, OracleTypes.VARCHAR); 34 cs.execute(); 35 36 //取出返回值,要注意?的顺序 37 String name = cs.getString(2); 38 System.out.println("学号1010的学生的名字:"+name); 39 40 } catch (Exception e) { 41 // TODO Auto-generated catch block 42 e.printStackTrace(); 43 } 44 finally{ 45 //关闭 46 try { 47 cs.close(); 48 conn.close(); 49 } catch (SQLException e) { 50 // TODO Auto-generated catch block 51 e.printStackTrace(); 52 } 53 } 54 } 55 56 }