Java Statement

 

Java 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[])

    {

                Connection conc;

               

        try

        {

                        String url = "jdbc:mysql://localhost:3306/College";

                        String usrname = "root";

                        String pwd = "JP@2026";

            conc = DriverManager.getConnection(

                url, usrname, pwd);

                               

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

 

                        Statement stat = conc.createStatement();

 

                        stat.executeUpdate("create table dept(dc INT, dn varchar(20), ds INT);");

                       

                        stat.executeUpdate("insert into dept values (205, 'IT',57), (104,'eee',45), (105,'ece',65), (111,'mech',60), (107,'civil',59);");

                       

                        ResultSet rs = stat.executeQuery("Select * from dept;");

                       

                        while(rs.next())

                        {

                                int code = rs.getInt("dc");

                                String name = rs.getString("dn");

                                int count = rs.getInt("ds");

                       

                                System.out.println("Code = "+code);

                                System.out.println("Name = "+name);

                                System.out.println("Count = "+count);

                        }

                        conc.close();

                       

                }

                catch(SQLException e)

                {

                        System.out.println(e);

                }

        catch(Exception e)

        {

            System.out.println(e);

        }

                finally

                {

                        System.out.println("EOP");

        }

    }

}

No comments:

Post a Comment

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

Anu