How to call an existing stored procedure in a database using JDBC API?

JDBCJava 8Object Oriented ProgrammingProgramming

A. Stored procedures are sub routines, segment of SQL statements which are stored in SQL catalog. All the applications that can access Relational databases (Java, Python, PHP etc.), can access stored procedures.

Stored procedures contain IN and OUT parameters or both. They may return result sets in case you use SELECT statements. Stored procedures can return multiple result sets.

You can call a stored procedure using the following syntax:

CALL procedure_name ()

JDBC provides a standard stored procedure SQL escape syntax using which you can procedures in all RDBMSs

To call a stored procedure using a JDBC program you need to:

  • Register the driver: Register the driver class using the registerDriver() method of the DriverManager class. Pass the driver class name to it, as parameter.

  • Establish a connection: Connect ot the database using the method of the getConnection()DriverManager class. Passing URL (String), username (String), password (String) as parameters to it.

  • Create Statement: Create a CallableStatement object using the prepareCall() method of the Connection interface.

  • Execute the Query: Execute the query using the executeQuery() method of the Statement interface.

Example

Assume we have a table named customers in the database with the following contents:

+----+-----------+-----+---------+----------------+
| ID | NAME      | AGE | SALARY  | ADDRESS        |
+----+-----------+-----+---------+----------------+
| 1 | Amit       | 25  | 3000.00 | Hyderabad      |
| 2 | Kalyan     | 27  | 4000.00 | Vishakhapatnam |
| 3 | Renuka     | 30  | 5000.00 | Delhi          |
| 4 | Archana    | 24  | 1500.00 | Delhi          |
| 5 | Koushik    | 30  | 9000.00 | Delhi          |
| 6 | Hardik     | 45  | 6400.00 | Delhi          |
| 7 | Trupthi    | 33  | 4360.00 | Delhi          |
| 8 | Mithili    | 26  | 4100.00 | Vijayawada     |
| 9 | Maneesh    | 39  | 4000.00 | Hyderabad      |
| 10 | Rajaneesh | 30  | 6400.00 | Delhi          |
| 11 | Komal     | 29  | 8000.00 | Ahmedabad      |
| 12 | Manyata   | 25  | 5000.00 | Vijayawada     |
+----+-----------+-----+---------+----------------+

We have created a stored procedure named retrieveData in MySQL which retrieves the contents of this table, as shown below:

mysql> DELIMITER // ;
mysql> Create procedure retrieveData()
BEGIN
   Select * from customers;
END//
Query OK, 0 rows affected (0.00 sec)
mysql> DELIMITER ;

Following JDBC program establishes connection with MySQL database, calls the procedure named retrieveData, retrieves the ResultSet object returned by the procedure and, displays its contents.

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
public class CallngStoredProcedureExample {
   public static void main(String args[]) throws SQLException {
      //Registering the Driver
      DriverManager.registerDriver(new com.mysql.jdbc.Driver());
      //Getting the connection
      String mysqlUrl = "jdbc:mysql://localhost/mydatabase";
      Connection con = DriverManager.getConnection(mysqlUrl, "root", "password");
      System.out.println("Connection established......");
      //Preparing a CallableStatement to call the retrieveData procedure
      CallableStatement cstmt = con.prepareCall("{call retrieveData()}");
      //Executing the CallableStatement
      ResultSet rs = cstmt.executeQuery();
      //Displaying the result
      while(rs.next()) {
         System.out.print("ID: "+rs.getInt("ID")+", ");
         System.out.print("Name: "+rs.getString("Name")+", ");
         System.out.print("Age: "+rs.getInt("Age")+", ");
         System.out.print("Salary: "+rs.getInt("Salary")+", ");
         System.out.print("Address: "+rs.getString("Address"));
         System.out.println();
      }
   }
}

Output

Connection established......
ID: 1, Name: Amit, Age: 25, Salary: 3000, Address: Hyderabad
ID: 2, Name: Kalyan, Age: 27, Salary: 4000, Address: Vishakhapatnam
ID: 3, Name: Renuka, Age: 30, Salary: 5000, Address: Delhi
ID: 4, Name: Archana, Age: 24, Salary: 1500, Address: Delhi
ID: 5, Name: Koushik, Age: 30, Salary: 9000, Address: Delhi
ID: 6, Name: Hardik, Age: 45, Salary: 6400, Address: Delhi
ID: 7, Name: Trupthi, Age: 33, Salary: 4360, Address: Delhi
ID: 8, Name: Mithili, Age: 26, Salary: 4100, Address: Vijayawada
ID: 9, Name: Maneesh, Age: 39, Salary: 4000, Address: Hyderabad
ID: 10, Name: Rajaneesh, Age: 30, Salary: 6400, Address: Delhi
ID: 11, Name: Komal, Age: 29, Salary: 8000, Address: Ahmedabad
ID: 12, Name: Manyata, Age: 25, Salary: 5000, Address: Vijayawada
raja
Published on 02-Apr-2019 11:24:57
Advertisements