Use JDBC to operate stored procedures

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.