-- Build Tables sql> CREATE TABLE Tbook (bookId number (8), BookName varchar2 (50), Publishhouse VARCHAR2 (()); -- writing process -in: input parameters; Begin INSERT INTO Tbook values (spbookid,spname,sppublishhost); end;
--Called in Java (Odbcdriver to be loaded in the project)
1 PackageOracle;2 3 Importjava.sql.CallableStatement;4 Importjava.sql.Connection;5 ImportJava.sql.DriverManager;6 Importjava.sql.SQLException;7 8 9 Ten //call a procedure with no return value One Public classTest01 { A - Public Static voidMain (string[] args) { - //TODO auto-generated Method Stub theConnection conn =NULL; -CallableStatement cs =NULL; - Try { - //1. Load Driver +Class.forName ("Oracle.jdbc.driver.OracleDriver"); - + //2. Connect AString url = "jdbc:oracle:thin:@127.0.0.1:1521:oracle12c"; atString userName = "sys as SYSDBA"; -String password = "fairy6280"; -conn =drivermanager.getconnection (Url,username,password); - //3. Create CallableStatement -String sql = "{call Sp_pro7 (?,?,?)}"; -CS =conn.preparecall (SQL); in //give '? ' Assign Value -Cs.setint (1, 1001); toCs.setstring (2, "The History of the Three Kingdoms"); +Cs.setstring (3, "People ' s publishing house"); - //Execution the Cs.execute (); * $}Catch(Exception e) {Panax Notoginseng //TODO auto-generated Catch block - e.printstacktrace (); the } + finally{ A //Close the Try { + cs.close (); - conn.close (); $}Catch(SQLException e) { $ //TODO auto-generated Catch block - e.printstacktrace (); - } the } - }Wuyi the}
Java calls a stored procedure in Oracle