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);
    }
   
}

Thursday 16 February 2012

How to delete record from Ms Access in Java








rajkumar9795@gmail.com
for delete a record in access follow instruction as mention in file connection blog 

Monday 13 February 2012

Connect Ms Access 2003 with Java (jdk1.3)


Hi
friends and student
 i was thinking from a long time how can i connect jdk1.3 to database.
Its very exciting and easy... Plz do following steps
1. Make a database in Ms Access
2. My Database name is bsw and table named stu(name,city)
3. Open Control panel>Administor>Data Sources(ODBC)
4.Select Ms Access (*.mdb) > Add
5. Write appropriate name of your ODBC Name
6. Select Database from select button
7. Paste this code,compile run and enjoy your database Connectivity


import java.sql.*;
import java.io.*;
class con2test
{
             public static void main(String[] args)
             {
                         // change this to whatever your DSN is
                         String dataSourceName = "mdbTEST";  //mdbTest is name which u hv mention in odbc data source name window
                         String dbURL = "jdbc:odbc:" + dataSourceName;
                         try
                        {
                                    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                                    Connection con = DriverManager.getConnection(dbURL, "","");    

                                     
                                    String nm;
                                    DataInputStream da=new DataInputStream(System.in);
                                    System.out.print("Enter name ");
                                    nm=da.readLine();
                                    System.out.print("Enter address ");
                                    address=da.readLine();
                                    Statement s = con.createStatement();
                                    s.executeUpdate("insert into user

values('"+nm+"','"+address+"')");
                                    con.close();
                                    System.out.println("Saved");
                                 
                         }
                         catch (Exception err)
                        {
                                    System.out.println( "Error: " + err );
                         }
             }
}