How to Use Callable Statement in Java to Call Stored Procedure?
Last Updated :
31 Jul, 2022
The CallableStatement of JDBC API is used to call a stored procedure. A Callable statement can have output parameters, input parameters, or both. The prepareCall() method of connection interface will be used to create CallableStatement object.
Following are the steps to use Callable Statement in Java to call Stored Procedure:
1) Load MySQL driver and Create a database connection.
import java.sql.*;
public class JavaApplication1 {
public static void main(String[] args) throws Exception
{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost/root","geek","geek");
}
}
2) Create a SQL String
We need to store the SQL query in a String.
String sql_string="insert into student values(?,?,?)";
3) Create CallableStatement Object
The prepareCall() method of connection interface will be used to create CallableStatement object. The sql_string will be passed as an argument to the prepareCall() method.
CallableStatement cs = con.prepareCall(sql_string);
4) Set The Input Parameters
Depending upon the data type of query parameters we can set the input parameter by calling setInt() or setString() methods.
cs.setString(1,"geek1");
cs.setString(2,"python");
cs.setString(3,"beginner");
5) Call Stored Procedure
Execute stored procedure by calling execute() method of CallableStatement class.
Example of using Callable Statement in Java to call Stored Procedure
Java
// Java program to use Callable Statement
// in Java to call Stored Procedure
package javaapplication1;
import java.sql.*;
public class JavaApplication1 {
public static void main(String[] args) throws Exception
{
Class.forName("com.mysql.jdbc.Driver");
// Getting the connection
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/root", "acm", "acm");
String sql_string = "insert into students values(?,?,?)";
// Preparing a CallableStateement
CallableStatement cs = con.prepareCall(sql_string);
cs.setString(1, "geek1");
cs.setString(2, "python");
cs.setString(3, "beginner");
cs.execute();
System.out.print("uploaded successfully\n");
}
}
Output:
students table after running code
Similar Reads
How to Use Stored Procedure in SQLite? SQLite is a popular, lightweight, self-contained, serverless, and open-source relational database management system (RDBMS) that is widely used in various applications. SQLite does not directly support stored procedures like other database management systems (DBMS) such as MySQL or PostgreSQL. To ac
4 min read
How to Create and Call a Stored Procedure in SQL? With this article, we will learn how to Create and Call a Stored Procedure in SQL. For this article, we are going to use MSSQL as our database server. What is a Stored Procedure?A stored procedure is a pre-written SQL query that can be called multiple times and will run as the same. Like we can crea
2 min read
How to Use PreparedStatement in Java? A PreparedStatement is a pre-compiled SQL statement. It is a subinterface of Statement. Prepared Statement objects have some useful additional features than Statement objects. Instead of hard coding queries, PreparedStatement object provides a feature to execute a parameterized query. Advantages of
3 min read
How to List all Stored Procedures in MariaDB? When working with MariaDB, it's important to be able to manage and maintain stored procedures effectively. Listing all stored procedures in a database can provide valuable insights into its structure and functionality. In this article, we'll explore how to list all stored procedures in MariaDB by un
4 min read
How to Call Stored Functions and Stored Procedures using JDBC? JDBC or Java Database Connectivity is a Java API to connect and execute the query with the database. It is a specification from Sun microsystems that provides a standard abstraction(API or Protocol) for java applications to communicate with various databases. It provides the language with java datab
5 min read
How to Write a Simple SELECT Stored Procedure in PL/SQL? In PL/SQL, stored procedures are powerful tools for encapsulating logic and executing complex queries. This article will guide you through the process of creating a simple SELECT stored procedure in PL/SQL. In this article, we will learn how to store procedures with the help of various methods and e
6 min read