Sunday, 25 March 2018

Java and Mysql Connection


Write a JAVA Program to insert data into Student DATA BASE and retrieve info based on particular queries(For example update, delete, search etc…).

1.Analysis


construction of java program to which has to connect to mysql database by following the sequence of jdbc steps.

Import java.sql.* packages.

Load and register the JDBC driver

Open a connection to the database by creating a connection object.

Create a statement object to create a query.

Execute the statement object and return a query resultset.

Process the resultset.

Close the resultset and statement objects.

Close the connection.
               


2.Design


3.Implementation

package J2eeLabPrograms;

import java.sql.*;
import java.io.*;

class prg1
{
    public static void menu()  {
        System.out.println("Enter you Choice");
        System.out.println("1.Insert Student Details "+
                         "\n 2.Update Details" +
                         "\n 3.Delete Record"  +
                         "\n 4.Display Records"+
                         "\n 5.Exit ");
       System.out.println("--------------------");
    }

    public static void main(String args[])   {
    Connection con=null;
    Statement st=null;
    PreparedStatement pst=null;
    ResultSet rs=null;
    String str=null;
    String url="jdbc:mysql://localhost:3306/kumardb","root","";
   
     try{
             Class.forName("com.mysql.jdbc.Driver");
             con=DriverManager.getConnection(url,"","");
              DataInputStream in=new DataInputStream(System.in);
                        
             while(true) {
                  try{
                         menu();
                      int ch=Integer.parseInt(in.readLine());
                   switch(ch) {

    case 1:
                               System.out.println("Enter the name");
                          String name=in.readLine();
                               System.out.println("Enter the USN");
                          String usn=in.readLine();
                 System.out.println("Enter the Branch");
                          String branch=in.readLine();
                 System.out.println("Enter the Average marks");
                          int AvgMarks=Integer.parseInt(in.readLine());
               pst=con.prepareStatement("insert into student values(?,?,?,?)");
                         pst.setString(1,name);
                         pst.setString(2,usn);
                         pst.setString(3,branch);
                         pst.setInt(4,AvgMarks);
                         pst.executeUpdate();
                         break;
                         
                 case 2:
                         System.out.println("Enter the USN");
                         String u=in.readLine();
                         System.out.println("Enter the Name");
                         String n=in.readLine();
               String query = new String ("UPDATE student SET name = ? WHERE usn=?");
                           pst = con.prepareStatement(query);
                           pst.setString(1,n);
                         pst.setString(2,u);
                            int row=pst.executeUpdate();
                            if(row>0){ 
                              System.out.println("record updated successfully");
                             }
                         else{
                                System.out.println(" un successful");
                          }
                            break;
                
             case 3:
                          System.out.println("Enter the USN");
                          String u1=in.readLine();
              String q1 = new String ("delete from student  WHERE usn=?");
                          pst = con.prepareStatement(q1);
                        pst.setString(1,u1);
             int r=pst.executeUpdate();
                 if(r>0){ 
                 System.out.println("record updated successfully");
                  }
                 else{
                    System.out.println(" un successful");
}

                             break;

             case 4:
             System.out.println("Name \t USN \t Branch \t Avg Marks ");
             st=con.createStatement();
             rs=st.executeQuery("select * from student");
                         
             while(rs.next())
             {

                             System.out.println(rs.getString(1)+"\t"+
                                    rs.getString(2)+"\t "+
                                    rs.getString(3)+"\t\t"+
                                    rs.getInt(4)+"\t\t" );
             }
             System.out.println("--------------------------------------------");
             break;
                
             case 5:System.exit(0);
                 break;
             } // end of switch case
     } // end of inner try
             catch(SQLException e){
                         System.out.println(e.toString()+" Enter again");
             }
   } // end of while loop
} // end of outer try
            catch(ClassNotFoundException e){
                           System.out.println("Driver not found");
               }
   catch(SQLException e){
                           System.out.println(e);
               }
   catch(Exception e){
                           System.out.println(e);
               }
} // end of main
}


4.OUTPUT:

Enter you Choice
1.Insert Student Details
 2.Update Details
 3.Delete Record
 4.Display Records
 5.Exit
--------------------
1
Enter the name
syed
Enter the USN
1re13mca01
Enter the Branch
mca
Enter the Average marks
70
Enter you Choice
1.Insert Student Details
 2.Update Details
 3.Delete Record
 4.Display Records
 5.Exit
--------------------
4
Name    USN    Branch             Avg Marks
syed     1re13mca01     mca                 70                   
--------------------------------------------
Enter you Choice
1.Insert Student Details
 2.Update Details
 3.Delete Record
 4.Display Records
 5.Exit
--------------------
1
Enter the name
ravi D
Enter the USN
1re13mca02
Enter the Branch
mca
Enter the Average marks
75
Enter you Choice
1.Insert Student Details
 2.Update Details
 3.Delete Record
 4.Display Records
 5.Exit
--------------------
4
Name    USN    Branch             Avg Marks
syed     1re13mca01     mca                 70                   
ravi D  1re13mca02     mca                 75                   
--------------------------------------------
Enter you Choice
1.Insert Student Details
 2.Update Details
 3.Delete Record
 4.Display Records
 5.Exit
--------------------
2
Enter the USN
1re13mca01
Enter the Name
syed
record updated successfully
Enter you Choice
1.Insert Student Details
 2.Update Details
 3.Delete Record
 4.Display Records
 5.Exit
--------------------
2
Enter the USN
1re13mca01
Enter the Name
syed khutubuddin
record updated successfully
Enter you Choice
1.Insert Student Details
 2.Update Details
 3.Delete Record
 4.Display Records
 5.Exit
--------------------
4
Name    USN    Branch             Avg Marks
syed khutubuddin       1re13mca01     mca                 70                   
ravi D  1re13mca02     mca                 75                   
--------------------------------------------
Enter you Choice
1.Insert Student Details
 2.Update Details
 3.Delete Record
 4.Display Records
 5.Exit
--------------------
3
Enter the USN
1re13mca01
record updated successfully
Enter you Choice
1.Insert Student Details
 2.Update Details
 3.Delete Record
 4.Display Records
 5.Exit
--------------------
4
Name    USN    Branch             Avg Marks
ravi D  1re13mca02     mca                 75                   
--------------------------------------------
Enter you Choice
1.Insert Student Details
 2.Update Details
 3.Delete Record
 4.Display Records
 5.Exit
--------------------





No comments:

Post a Comment

PUBLISHER & SUBSCRIBER PATTERN

Using the UML Drawing Tool by implementing the code in Java demonstrate the Observer  Design Pattern. The Publisher-Subscriber desig...