1. Create an Add stored procedure
CREATE OR REPLACE PROCEDURE inch Number inch VARCHAR2 inch Number as BEGIN INSERT into Values (v_id, V_name, v_age); Commit ; END;
Java calls to add stored procedures
Packagecom.ljq.test;Importjava.sql.CallableStatement;Importjava.sql.Connection;Importjava.sql.SQLException; Public classProcetest { Public Static voidMain (string[] args)throwsException {Connection conn=NULL; CallableStatement Statement=NULL; String SQL= "{Call Stu_proc (?,?,?)}"; Try{conn=connutils.getconnection (); Statement=conn.preparecall (SQL); Statement.setint (1, 6); Statement.setstring (2, "Laoli"); Statement.setint (3, 45); //Returns true if the first result is a ResultSet object, or False if the first result is an update, add, modify, or no result Booleanissuccess=Statement.execute (); //Success Returns True, Failure returns falseSystem.out.println (issuccess); } Catch(SQLException e) {e.printstacktrace (); } finally{Connutils.free (NULL, statement, Conn); } }}
Create a Delete stored procedure statement
CREATE OR REPLACE PROCEDUREStu_proc (v_idinch Number, v_msg outVARCHAR2) isV_flag Number:=1;BEGIN SELECTO.id intoV_flag fromStudent OWHEREO.id=v_id; DELETE fromStudent OWHEREO.id=V_flag; Commit; V_msg:='Delete succeeded'; EXCEPTION whenOTHERS ThenV_msg:='Delete Failed';END;
Java calls to delete stored procedures
Packagecom.ljq.test;Importjava.sql.CallableStatement;Importjava.sql.Connection;Importjava.sql.SQLException;Importjava.sql.Types; Public classProcetest { Public Static voidMain (string[] args)throwsException {Connection conn=NULL; CallableStatement Statement=NULL; String SQL= "{Call Stu_proc (?,?)}"; Try{conn=connutils.getconnection (); Statement=conn.preparecall (SQL); Statement.setint (1, 4); Statement.registeroutparameter (2, Types.varchar); Statement.execute (); String msg=statement.getstring (2); SYSTEM.OUT.PRINTLN (msg); } Catch(SQLException e) {e.printstacktrace (); } finally{Connutils.free (NULL, statement, Conn); } }}
To create a modify stored procedure
CREATE OR REPLACE PROCEDUREStu_proc (v_idinch Number, V_nameinch VARCHAR2, v_msg outVARCHAR2) asV_flag Number;BEGIN SELECTO.id intoV_flag fromStudent OWHEREO.id=v_id; UPDATEStudent OSETO.sname=V_nameWHEREO.id=v_id; Commit; V_msg:='Modification succeeded'; EXCEPTION whenOTHERS ThenV_msg:='Modification Failed';END;
Java calls modify stored procedures
Packagecom.ljq.test;Importjava.sql.CallableStatement;Importjava.sql.Connection;Importjava.sql.SQLException;Importjava.sql.Types; Public classProcetest { Public Static voidMain (string[] args)throwsException {Connection conn=NULL; CallableStatement Statement=NULL; String SQL= "{Call Stu_proc (?,?,?)}"; Try{conn=connutils.getconnection (); Statement=conn.preparecall (SQL); Statement.setint (1, 3); Statement.setstring (2, "Laoli"); Statement.registeroutparameter (3, Types.varchar); Statement.execute (); String msg=statement.getstring (3); SYSTEM.OUT.PRINTLN (msg); } Catch(SQLException e) {e.printstacktrace (); } finally{Connutils.free (NULL, statement, Conn); } }}
Java calls and additions to stored procedures