Prepared Statement

Prepared Statement


Database     : College

Table name : Department

Fields : DeptCode INT & DeptName VARCHAR(20)

        

 

 

// 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");

 

                        PreparedStatement ps = conc.prepareStatement("Select * from Department where DeptCode=?");

 

                        ps.setInt(1,205);                                 

 

                        ResultSet rs = ps.executeQuery();

 

                        if(rs.next())

                        {

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

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

                        }

                        else

                                System.out.println("No Deptartment found.");

               

                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 = 205

Department Name = IT

End of Program.

 

> 

 


No comments:

Post a Comment

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

Anu