What is CallableStatement?
A java.sql.CallableStatement is used to call stored procedures in a database. A stored procedure is like a function or method in a class, except it lives inside the database.
CallableStatement
– Can be created by calling the prepareCall() method from the Connection object
– Contains the functionality of calling a stored procedure
– Is used for queries with INPUT as well as OUTPUT parameters of Stored Procedure.
Select all Records from procedure based on Customer Table : Using CallableStatement and executeQuery()
import java.sql.*;
class SelectCustomerByProcedure
{
public static void main(String[] args) throws SQLException, ClassNotFoundException
{
String custid; String custname; int qty; float price;float total;
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/javaskoolDB","root","admin");
//Calling Stored Procedure of SQL and selecting all records
CallableStatement cs = con.prepareCall("{call CustSelect}");
ResultSet rs = cs.executeQuery();
System.out.println("CustID\t CustName\t qty\t price\t total");
while(rs.next())
{
custid=rs.getString(1); //using Column number
custname=rs.getString(2);
qty=rs.getInt(3);
price=rs.getFloat(4);
total=rs.getFloat(5);
System.out.println(custid+"\t "+custname+"\t\t"+qty+"\t "+price+"\t "+total);
}
}
}
Here is the code for Procedure and calling procedure statement.
Recent Comments