Calling StoredProcedure Using CallableStatement

By | July 17, 2018

In this post we will learn how to call Stored Procedure using JDBC CallableStatement.

Project structure in eclipse:

First of all we are going to create MySQL database schema named jdbcdb

The MySQL jdbcdb database schema consists of the following tables:

  • Customers: stores customer’s data.
  • Employees: This table stores all employees information as well as the organization structure such as who reports to whom.
  • Offices: This table stores sales office data.
  • OrderDetails: stores sales order line items for each sales order
  • Orders: This table stores  sales orders placed by customers.
  • Payments: This table stores payments made by customers based on their accounts.
  • ProductLines:This table stores a list of product line categories.
  • Products: This table stores a list of scale model cars.

We have below db script we will create these tables and insert some records.

Now Let’s create below stored procedure in MYSQL schema jdbcdb

MySQL database after creating tables and stored procedure

DBUtil.java class which is responsible to connect with MySQL database.
This util class uses getConnection() method of java.sql.DriverManager class
public static Connection getConnection(String url,String user, String password) throws SQLException
This method takes three parameters database URL,username and password.
Here database username =”root”,password  = “root”  what password had supplied during MYSQL database installation time and finally database URL =“jdbc:mysql://localhost:3306/jdbcdb” Where jdbc is the API, mysql is the database, localhost is the server name on which mysql  database server is running, we may also use IP address instead machine name, 3306 is the port number and jdbcdb(Make you you have created this schema in MySQL database) is the database name. You may use any database name, in that case, You need to replace the jdbcdb with your database name.

ClientTest.java is a client progarm to call stored procedure

If you run ClientTest .java you will get below output on your IDE console:

Above output shows we have called stored procedure successfully using JDBC CallableStatement.

if you want to verify your output by direct calling your stored procedure from MySQL Workbench then you can try as shown in the below screen shot:

That’s all for this topic how to call stored procedure using JDBC CallableStatement.

You May Also Like:

Get ResultSet By Calling StoredProcedure Using CallableStatement
Calling database custom Function Using CallableStatement
JDBC batch update with Statement
JDBC batch update with PreparedStatement
JDBC batch update with CallableStatement
How to retrieve data from JDBC resultset in java
How to update a Row in a Database Table Using an updatable ResultSet
How to insert a Row in a Database Table Using an updatable ResultSet
JDBC ResultSet navigation methods example in java

If you have any feedback or suggestion please feel free to drop in below comment box.

Leave a Reply

Your email address will not be published. Required fields are marked *