JDBC Callable Statement

JDBC Callable Statement:

  • It is an interface of java.sql.* ,  used to Execute Stored Procedure and functions.
Java Program Which calling stored procedure in database
package com.spy.javaforlearn;
 
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
 
import com.mysql.jdbc.Driver;
 
public class CallableStatement1
{
 
	public static void main(String[] args)
	{
 
		Connection con=null;
		CallableStatement stmt=null;
		ResultSet rs=null;
 
 
		try 
		{
 
 
			/*
			 * load the driver
			 */
			Driver driverref=new Driver();
			DriverManager.registerDriver(driverref);
 
			/*
			 * get db connection via driver
			 */
 
	   String url="jdbc:mysql://localhost:3306/test?user=root&password=root";
	   con=DriverManager.getConnection(url);
 
	             /*
		      * issue sql query via driver
		     */
 
	String query="call student_insert_update(4,'Ankur','Panesariya')";
 
 
			stmt=con.prepareCall(query);
 
 
 
		  boolean isdbresult = stmt.execute();
 
		  if(isdbresult)	
		  {
			 System.out.println("result is type of dbresult"); 
 
			  rs= stmt.getResultSet();
 
			 while(rs.next())
			 {
 
				String userid=rs.getString("User_id"); 
				String fnm=rs.getString("f_name");
				String lnm=rs.getString("l_name");
 
 
				System.out.println(userid);
				System.out.println(fnm); 
				System.out.println(lnm);
 
 
			 }
 
		  }
		  else
		  {
			  System.out.println("result is type of integer count:");
 
			  int count=stmt.getUpdateCount();
 
			  System.out.println("no of row affected: "+count);
 
		  }
 
 
 
 
		} 
 
		catch (SQLException e) 
		{
			e.printStackTrace();
		}
 
 
 
		/*
		 * close all jdbc object
		 */
 
		finally
		{
 
			try {
				if(con!=null)
 
				{
					con.close();
				}
 
				if(stmt!=null)
				{
					stmt.close();
				}
 
				if(rs!=null)
				{
					rs.close();
				}
 
			}
 
			catch (SQLException e) 
			{
 
				e.printStackTrace();
			}	
		}
	 }
}
 
output:
 
result is type of integer count:
no of row affected: 1

Now you can see data in Mysql database

JDBC CALLABLE STATEMENT

Leave a Reply