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



1 comment:

  1. tulungan mo nman ako! give me a code of a program that acccepts even of 1's and odd of 0's or else it rejects

    ReplyDelete