Java 调用Oracle中的存储过程
--建表 SQL> create table TBook(bookId number(8),bookName varchar2(50),publishHouse varchar2(50)); --编写过程 --in:输入参数;out:输出参数 SQL> create or replace procedure sp_pro7(spBookId in number,spName in Varchar2,spPublishHost in varchar2) is begin insert into TBook values(spBookId,spName,spPublishHost); end;
--在Java中调用(工程中要加载odbcDriver)
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 9 10 //调用一个无返回值的过程 11 public class Test01 { 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 //3.创建CallableStatement 27 String sql = "{call sp_pro7(?,?,?)}"; 28 cs = conn.prepareCall(sql); 29 //给'?'赋值 30 cs.setInt(1, 1001); 31 cs.setString(2, "The History of the Three Kingdoms"); 32 cs.setString(3, "People's Publishing House"); 33 //执行 34 cs.execute(); 35 36 } catch (Exception e) { 37 // TODO Auto-generated catch block 38 e.printStackTrace(); 39 } 40 finally{ 41 //关闭 42 try { 43 cs.close(); 44 conn.close(); 45 } catch (SQLException e) { 46 // TODO Auto-generated catch block 47 e.printStackTrace(); 48 } 49 } 50 } 51 52 }
I don't extravagant hope to change the world, only hope this world will not change me.