Friday, August 21, 2015

callable statement

import java.sql.*;
public class Mysql_callableDemo
{
    public static void main(String[] args)
    {
        try
        {
            Class.forName("com.mysql.jdbc.Driver");
            System.out.println("Driver is loaded");
            Connection c = DriverManager.getConnection("jdbc:mysql://localhost:3306/java2all","root","kamal");
            System.out.println("Connection created");
            String q = "call addproc(?,?,?)";
            CallableStatement cs = c.prepareCall(q);
            cs.setInt(1, 10);
            cs.setInt(2, 20);
            cs.registerOutParameter(3, Types.INTEGER);
            cs.execute();
            int add = cs.getInt(3);
            System.out.println("addition = "+add); 
        }
        catch(Exception e)
        {
            System.out.println("Exception : " +e);
        }      
    }
}

Stored Procedure:
 
DELIMITER $$
CREATE PROCEDURE `datainfo`( IN sid INT,OUT sname VARCHAR(20), OUT scity VARCHAR(20),OUT sage INT)
BEGIN 
select name,city,age INTO sname,scity,sage from data where id=sid; 
END$$
 
We will pass the id when we call the stored procedure and it will return name,city and age as per the id we passed.

 

No comments:

Post a Comment