Invoking an Oracle stored procedure in Java
This article describes how to invoke Oracle's stored procedures through Java
1. Writing Stored Procedures
CREATEORPROCEDURE sp_pro3(sp_name VARCHAR2,sp_sal NUMBER )ISBEGIN --根据用户名修改工资 UPDATE emp SET sal=sp_sal WHERE ename=sp_name;END;
- Introduce Jdbc6.jar, write test class Test.java
PackageTestoraclepro;ImportJava.sql.CallableStatement;ImportJava.sql.Connection;ImportJava.sql.DriverManager; Public class Test { Public Static void Main(string[] args) {Try{//1. Load DriverClass.forName ("Oracle.jdbc.driver.OracleDriver");//2. Getting a connectionConnection Ct=drivermanager.getconnection ("Jdbc:oracle:thin:@127.0.0.1:1521:xe","sys as SYSDBA","123456");//3. Creating CallableStatementCallableStatement Cs=ct.preparecall ("{call Sp_pro3 (?,?)}");//4. Assigning ValuesCs.setstring (1,"Zxs"); Cs.setfloat (2,455f);//5. ExecutionCs.execute ();//6. Closing ResourcesCs.close (); Ct.close (); }Catch(Exception e) {E.printstacktrace (); } }}
Execute the above test class to invoke the stored procedure and execute the corresponding program.
Invoking an Oracle stored procedure in Java