Sunday, 21 October 2012

updating a table's data in JAVA



import java.sql.*;
import java.util.*;
class update
{
    public static void main(String[] argv)
    {
        try
        {
            Scanner sc=new Scanner(System.in);
            int a, result, tot,phno;
            String dob, roll;
         
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

            String database="jdbc:odbc:eceDSN";
            Connection con=DriverManager.getConnection(database,"","");
            System.out.println("DATABASE OF ECE is Connected");
            System.out.println("Enter The Choice");
            sc.nextLine();
            System.out.println("1 for TOTAL update"); 
            System.out.println("2 for DOB update");
            System.out.println("3 for Ph No update");
            a=sc.nextInt();
            sc.nextLine();
            if(a==1)
            {
                System.out.println("Enter The Roll No");
            roll=sc.nextLine();
            System.out.println("Enter The Total No");
            tot=sc.nextInt();
            sc.nextLine();
            String query1="UPDATE ECE SET tot = ? WHERE roll LIKE ?";
            PreparedStatement ps=con.prepareStatement(query1);
            ps.setString(2, roll);
            ps.setInt(1,tot);
            result=ps.executeUpdate(); 
            if(result==1)
            {
                System.out.println("The Total is Updated Successfully");
            }
            }
          
          
          
          
          
             else
            if(a==2)
            {
                System.out.println("Enter The Roll No: ");
            roll=sc.nextLine();
            System.out.println("Enter The DOB: ");
            dob=sc.nextLine();
            String query2="UPDATE ECE SET dob = ? WHERE roll LIKE ?";
            PreparedStatement ps=con.prepareStatement(query2);
            ps.setString(2, roll);
            ps.setString(1, dob);
            result=ps.executeUpdate(); 
            if(result==1)
            {
                System.out.println("The DOB is Updated Successfully");
            }
 
            }
            else
         
         
            if(a==3)
                {
                System.out.println("Enter The Roll No");
            roll=sc.nextLine();
            System.out.println("Enter The Ph No");
            phno=sc.nextInt();
            sc.nextLine();
            String query3="UPDATE ECE SET phno = ? WHERE roll LIKE ?";
            PreparedStatement ps=con.prepareStatement(query3);
            ps.setString(2, roll);
            ps.setInt(1,phno);
            result=ps.executeUpdate(); 
            if(result==1)
            {
                System.out.println("The Ph No is Updated Successfully");
            }
            }
          
     
         
            else
            {
                System.out.println("You have entered wrong choice !");
            }
     
        con.close();
         
    }
    catch(Exception err)
        {
            System.out.println(err.getMessage());
        }
        //loading all the drivers to the memory
    }
}

No comments:

Post a Comment

Tech N Science © 2013