You can use CallableStatement to perform JDBC operations on stored procedures. The SQL statement for calling the stored procedure is {callprocedure_name [(arg1), (arg2)]}, and CallableStatement can be obtained through the prepareCall () method of the database connection object, for example, conn. prepareCall (SQL); If
You can use CallableStatement to perform JDBC operations on stored procedures. The SQL statement for calling the stored procedure is {call procedure_name [(arg1), (arg2)]}, and CallableStatement can be obtained through the prepareCall () method of the database connection object, for example, conn. prepareCall (SQL); If
You can use CallableStatement to perform JDBC operations on stored procedures.
The SQL statement for calling the stored procedure is: {call procedure_name [(arg1), (arg2)]}
CallableStatement can be obtained through the prepareCall () method of the database connection object.
For example:
conn.prepareCall(sql);
If the stored procedure has an output parameter, you can use its registerOutParameter () method to register the output parameter as the JDBC Type. For example:
registerOutParameter(int parameterIndex,int sqlType)
The following is an example of counting the number of students. First, we will look at the records in the database:
It is not hard to see that there are seven public records in the student table.
Write a stored procedure to count the number of students in the student table.
create or replace procedure getStudentCount(v_student out number) isbegin select count(*) into v_student from student;end getStudentCount;
Then test the stored procedure:
declare v_count number;begin getstudentCount(v_count); dbms_output.put_line(v_count);end;
The output is 7. There is no problem with the stored procedure.
Then you can see how to call the stored procedure using JDBC.
import java.sql.CallableStatement;import java.sql.Connection;import java.sql.DriverManager;import java.sql.SQLException;public class ProcedureDemo {public static void main(String[] args) {Connection conn = null;int studentCount = 0;try {Class.forName("oracle.jdbc.driver.OracleDriver");conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORCL", "myhr", "myhr");} catch (ClassNotFoundException e) {e.printStackTrace();} catch (SQLException e) {e.printStackTrace();}String sql = "{call getStudentCount(?)}";try {CallableStatement proc = conn.prepareCall(sql);proc.registerOutParameter(1, java.sql.Types.INTEGER);proc.execute();studentCount = proc.getInt(1);} catch (SQLException e) {e.printStackTrace();}System.out.println(studentCount);}}
In this case, output 7 on the console to prove that the stored procedure is successfully called.