Monday, May 9, 2011

In a bank, different customers having saving account. Some customers may have taken a loan from the bank. So bank always maintains information about bank depositors and borrowers. Design a Base class Customer (name, phone-number).Derive a class Depositor (accno, balance) from Customer. Again derive a class Borrower (loan-no, loan-amt) from Depositor. Write necessary member functions to read and display the details of ‘n’ customers.

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

class Slip2
{
    public static void main(String args[])
    {
        Connection con;
        Statement state;
        ResultSet rs;
        int ch;
        try
        {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            System.out.println("Driver Loaded");
            con=DriverManager.getConnection("jdbc:odbc:mydsn");
            System.out.println("Statement object created");

            do
            {
                System.out.println("\n");
                System.out.println("Menu:");
                System.out.println("1.Insert Record into the Table");
                System.out.println("2.Update The Existing Record.");
                System.out.println("3.Display all the Records from the Table");
                System.out.println("4.Exit");
                System.out.println("Enter your choice: ");

                BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
                ch=Integer.parseInt(br.readLine());

                switch(ch)
                {
                    case 1:
                    System.out.println("Enter Employee Number: ");
                    int no=Integer.parseInt(br.readLine());
                    System.out.println("Enter Employee Name: ");
                    String name=br.readLine();
                    System.out.println("Enter Employee Salary: ");
                    int sal=Integer.parseInt(br.readLine());
                    String sql="insert into emp values(?,?,?)";
                    PreparedStatement p=con.prepareStatement(sql);
                    p.setInt(1,no);
                    p.setString(2,name);
                    p.setInt(3,sal);
                    p.executeUpdate();
                    System.out.println("Record Added");
                    //p.close();
                    //con.close();
                    break;

                    case 2:
                    System.out.println("Enter Employee Number for the record you wish to Update: ");
                    no=Integer.parseInt(br.readLine());
                    System.out.println("Enter new Name: ");
                    name=br.readLine();
                    System.out.println("Enter new Salary: ");
                    sal=Integer.parseInt(br.readLine());
                    sql="update emp set Name=?, Salary=? where Code=?";
                    p=con.prepareStatement(sql);
                    p.setString(1,name);
                    p.setInt(2,sal);
                    p.setInt(3,no);
                    p.executeUpdate();
                    System.out.println("Record Updated");
                    //p.close();
                    //con.close();
                    break;

                    case 3:
                    state=con.createStatement();
                    sql="select * from emp";
                    rs=state.executeQuery(sql);
                    while(rs.next())
                    {
                        System.out.println("\n");
                        System.out.print("\t" +rs.getInt(1));
                        System.out.print("\t" +rs.getString(2));
                        System.out.print("\t" +rs.getInt(3));
                    }
                    break;

                    case 4:
                    System.exit(0);

                    default:
                    System.out.println("Invalid Choice");
                    break;
                }
            }while(ch!=4);
        }catch(Exception e)
        {
            System.out.println(e);
        }
    }
}



Create a class telephone containing name, telephone number & city and write necessary member functions for the following: -Search the telephone number with given name. -Search the name with given telephone number. -Search all customers in a given city. (Use function overloading)

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.sql.*;
import javax.sql.*;
import java.util.*;

public class Slip1 extends Frame implements ActionListener
{
    TextField t1,t2,t3;
    Label l1,l2,l3;
    Button b1;
    Connection con;
    public Database()
    {
        setLayout(new GridLayout(4,2));
        t1=new TextField(20);
        t2=new TextField(20);
        t3=new TextField(20);
        l1=new Label("Enter Employee Number: ");
        l2=new Label("Enter Name: ");
        l3=new Label("Enter Salary: ");
        b1=new Button("ADD");

        add(l1);
        add(t1);
        add(l2);
        add(t2);
        add(l3);
        add(t3);
        add(b1);

        b1.addActionListener(this);

        addWindowListener(new WindowAdapter()
        {
            public void windowClosing(WindowEvent we)
            {
                setVisible(false);
                System.exit(0);
                dispose();
            }
        });

    }

    public void actionPerformed(ActionEvent ae)
    {
        try
        {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            con=DriverManager.getConnection("jdbc:odbc:mydsn");

            if(ae.getSource()==b1)
            {
                int no=Integer.parseInt(t1.getText());
                String name=t2.getText();
                int sal=Integer.parseInt(t3.getText());

                String sql="insert into emp values(?,?,?)";
                PreparedStatement p=con.prepareStatement(sql);
                p.setInt(1,no);
                p.setString(2,name);
                p.setInt(3,sal);
                p.executeUpdate();
                System.out.println("Record Added");
                p.close();
                con.close();
            }
        }catch(Exception e)
        {
            System.out.println(e);
        }
    }

    public static void main(String args[])
    {
        Database d1=new Database();
        d1.setSize(300,300);
        d1.setVisible(true);
    }
}