JDBC CallableStatements

 

 JDBC CallableStatements 


Database     : College

Table name : Department

Fields : DeptCode INT & DeptName VARCHAR(20)

         



                Stored Procedure 

            DELIMITER //

 

            CREATE PROCEDURE getDepartment(IN val INT)

            BEGIN

                SELECT *

                FROM Department

                WHERE DeptCode = val;

            END //

 

            DELIMITER ;




// JDBC


import java.sql.*;


class JDBC_Demo

{

    public static void main(String as[])

    {  

        try

        {

            Connection conc = DriverManager.getConnection("jdbc:mysql://localhost:3306/College","root", "JP@2026");


System.out.println("Database Connected");


CallableStatement cs = conc.prepareCall("{call getDepartment(159)}");

ResultSet rs = cs.executeQuery();


if(rs.next())

{

System.out.println("Department Code = "+rs.getInt("DeptCode"));

System.out.println("Department Name = "+rs.getString(2));

}

else

System.out.println("No record found for department code.");

conc.close();

}

catch(SQLException e)

{

System.out.println(e);

}

        catch(Exception e)

        {

            System.out.println(e);

        }

finally

{

System.out.println("End of Program.");

        }

    }

}


Sample Output


>javac JDBC_Demo.java

>java JDBC_Demo

Database Connected

Department Code = 159

Department Name = CDS

End of Program.


>


No comments:

Post a Comment

Don't be a silent reader...
Leave your comments...

Anu