Pages

Tuesday, 21 February 2012

Complete menu based java database program for read,write,delete,update


hi folks
this is complete menu based program  java database read write.You'll have to set odbc driver which i have mention in java database connection blog
paste following code in your editor compile and run.....

import java.sql.*;
import java.io.*;
class dataoperation
{
        String nm,ct,db,cls;
        int rl;
        Connection con=null;      
        DataInputStream da=new DataInputStream(System.in);
        Statement stmt=null;
        dataoperation()
        {
                cls="sun.jdbc.odbc.JdbcOdbcDriver";
                db="jdbc:odbc:mydata";
                try
                {
                        Class.forName(cls);
                        con=DriverManager.getConnection(db,"","");
                        stmt=con.createStatement();
                }
                catch(Exception ex)
                {
                        System.out.println("Error : "+ex.getMessage());
                }
               
        }
        void write()
        {
                try
                {
                     
                        System.out.print("Enter Roll ");
                        rl=Integer.parseInt(da.readLine());
                        System.out.print("Enter name  ");
                        nm=da.readLine();
                        System.out.print("Enter city ");
                        ct=da.readLine();
                      
                        stmt.executeUpdate("insert into stu values("+rl+",'"+nm+"','"+ct+"')");
                        System.out.println("\nInserted");
                      
                }
                catch(Exception ex)
                {
                    System.out.println("Error : "+ex.getMessage());
                }
               
        }
        void delete()
        {
            System.out.println("Enter Roll ");
           
            try
            {
                rl=Integer.parseInt(da.readLine());
                stmt.executeUpdate("delete from stu where roll="+rl);
            }
            catch(Exception ex)
            {
                System.out.println("Error : "+ex.getMessage());
            }
        }
        void update()
        {
            try
            {
                System.out.print("Enter roll ");
                rl=Integer.parseInt(da.readLine());
                System.out.print("Enter Name ");
                nm=da.readLine();
                System.out.print("Enter City ");
                ct=da.readLine();
                stmt.executeUpdate("update stu set name='"+nm+"', city='"+ct+"' where roll="+rl);
               // con.close();
                System.out.print("\nRecord Updated");
            }
            catch(Exception ex)
            {
                System.out.print("\nError"+ex.getMessage());
            }
        }
        void print()
        {
            System.out.println("\n\n********* OUTPUT *************");
            try
            {
                ResultSet rlt=stmt.executeQuery("select * from stu");
                while(rlt.next())
                {
                    System.out.print("\t"+rlt.getString(1)+" "+rlt.getString(2)+" "+rlt.getString(3)+"\n");
                }
            }
            catch(Exception ex)
            {
                System.out.print("\nError : "+ex.getMessage());
            }
            System.out.println("\n****************************\n\n");
        }
}


class dbcomplete
{
   
   
    public static void main(String[] args) throws Exception
    {
       int opt;
       DataInputStream da=new DataInputStream(System.in);
       dataoperation ob=new dataoperation();
       do
       {
           System.out.println("****************************************");
           System.out.print("\n1. Add\n2. Delete\n3. Update\n4. Print\n0. Exit\n****************************************");
           System.out.print("\nEnter Your choice ");
           opt=Integer.parseInt(da.readLine());
           switch(opt)
           {
               case 1:
                    ob.write();
                    break;
               case 2:
                   ob.delete();
                   break;
               case 3:
                   ob.update();
                   break;
               case 4:
                   ob.print();
                   break;
               case 0:
                   break;
               default:
                   System.out.print("\nInvalid choice");
           }
          
       }while(opt!=0);
    }
   
}

No comments:

Post a Comment