Tuesday, 28 February 2012

Database

import java.net.*;
import java.io.*;
import java.sql.*;
import java.util.*;
class clida
{
    public static void main(String a[])
    {
        String res="";
        try
        {
        Socket cs=new Socket("localhost",8888);
        System.out.println("Connected with Server...");
        DataInputStream dis = new DataInputStream(cs.getInputStream());
        DataInputStream udis = new DataInputStream(System.in);
        PrintStream ps =new PrintStream(cs.getOutputStream());
        while(true){
        System.out.println("Database Server");
        System.out.println("1:Add Data ");
        System.out.println("2:View Data ");
        System.out.println("3:Exit");
        System.out.print("\n\nEnter the Choice :");
        int choice=Integer.parseInt(udis.readLine());
        switch(choice)
        {
        case 1:
                ps.println("1");
                System.out.println("Enter the ID:");
                String id=udis.readLine();
                System.out.print("Enter Name : ");
                String nam=udis.readLine();
                System.out.print("Enter City : ");
                String city =udis.readLine();
                System.out.print("Enter Age : ");
                String age =udis.readLine();
                String inp=id+"@"+nam+"@"+city+"@"+age;
                ps.println(inp);
                break;
               
        case 2:
                ps.println("2");
                res=dis.readLine();
                StringTokenizer Stt =new StringTokenizer(res,"#");
                while(Stt.hasMoreTokens())
                {
                    String temp=Stt.nextToken();
                    System.out.println(temp);
                }
                break;
        case 3:
                ps.println("3");
                System.exit(0);       
        default:
                 System.out.print("Enter choice between 1 and 3");
                 break;
         }
        }
      }catch(Exception e){e.getMessage();}
    }
}



import java.net.*;
import java.io.*;
import java.sql.*;
import java.util.*;
class serda
{
    public static void main(String a[])
    {
        Connection con;
        Statement stat;
        try
        {
            System.out.println("Waiting for Client...");
            ServerSocket ss= new ServerSocket(8888);
            Socket s=ss.accept();
            System.out.println("Client Connected");
            DataInputStream dis = new DataInputStream(s.getInputStream());
            PrintStream ps =new PrintStream(s.getOutputStream());
            while(true){
            int ch=Integer.parseInt(dis.readLine());
            switch(ch)
            {
            case 1:
                    int c=0;
                    String inp[] = new String[4];
                    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                    con=DriverManager.getConnection("jdbc:odbc:subha","","");
                    stat=con.createStatement();
                    ResultSet rs = stat.executeQuery("select * from subha");
                    //while(rs.next())
                    //pk=Integer.parseInt(rs.getString(1));
                    //pk+=1;
                    rs.close();
                    con.close();
                    stat.close();
                    String data=dis.readLine();
                    StringTokenizer stt = new StringTokenizer(data,"@");       
                    while(stt.hasMoreTokens())
                    {
                        inp[c]=stt.nextToken();
                        c++;
                    }
                    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                    con=DriverManager.getConnection("jdbc:odbc:subha","","");
                    stat=con.createStatement();
                    int pk=Integer.parseInt(inp[0]);
                     int age=Integer.parseInt(inp[3]);
                    int cc=stat.executeUpdate("INSERT INTO subha (id,name,address,age) VALUES ("+pk+",'"+inp[1]+"','"+inp[2]+"',"+age+")");
                    con.close();
                    stat.close();
                    break;       
            case 2:   
                    String resq="";   
                    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                    con=DriverManager.getConnection("jdbc:odbc:subha","","");
                    stat=con.createStatement();
                    rs = stat.executeQuery("select * from subha");
                    while(rs.next())
                    {
                        String temp=rs.getString(1)+"    "+rs.getString(2)+"    "+rs.getString(3)+"    "+rs.getString(4);
                        resq+=temp+"#";
                    }
                    ps.println(resq);
                    con.close();
                    stat.close();
                    break;
            case 3:
                   System.exit(0);       
            }
        }
      }catch(Exception e){ e.printStackTrace();}
    }
}

No comments:

Post a Comment