Monday, May 9, 2011

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

2 comments: