Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Q2-phonebook
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- import java.sql.*;
- class PhoneBook extends JFrame implements ActionListener
- {
- JLabel l1,l2,l3,l4;
- JTextField tf1,tf2,tf3,tf4;
- JButton b1,b2,b3,b4;
- Connection con;
- PreparedStatement ps;
- Statement stmt;
- ResultSet rs;
- PhoneBook()
- {
- try
- {
- Class.forName("org.postgresql.Driver");
- con=DriverManager.getConnection("jdbc:postgresql://localhost/tydb","root","");
- if(con==null)
- {
- JOptionPane.showMessageDialog(null,"Unable to connect database : ");
- System.exit(0);
- }
- stmt=con.createStatement();
- rs=stmt.executeQuery("select * from phone");
- }
- catch(Exception e)
- {
- JOptionPane.showMessageDialog(null,e);
- }
- setLayout(null);
- l1=new JLabel("Name:");
- l1.setSize(100,30);
- l1.setLocation(10,30);
- add(l1);
- tf1=new JTextField();
- tf1.setSize(200,30);
- tf1.setLocation(150,30);
- add(tf1);
- b1=new JButton("ADD");
- b1.addActionListener(this);
- b1.setSize(150,30);
- b1.setLocation(400,30);
- add(b1);
- l2=new JLabel("Address:");
- l2.setSize(100,30);
- l2.setLocation(10,80);
- add(l2);
- tf2=new JTextField();
- tf2.setSize(200,30);
- tf2.setLocation(150,80);
- add(tf2);
- b2=new JButton("DELETE");
- b2.addActionListener(this);
- b2.setSize(150,30);
- b2.setLocation(400,80);
- add(b2);
- l3=new JLabel("Phone");
- l3.setSize(100,30);
- l3.setLocation(10,130);
- add(l3);
- tf3=new JTextField();
- tf3.setSize(200,30);
- tf3.setLocation(150,130);
- add(tf3);
- b3=new JButton("NEXT");
- b3.addActionListener(this);
- b3.setSize(150,30);
- b3.setLocation(400,130);
- add(b3);
- l4=new JLabel("Email:");
- l4.setSize(100,30);
- l4.setLocation(10,180);
- add(l4);
- tf4=new JTextField();
- tf4.setSize(200,30);
- tf4.setLocation(150,180);
- add(tf4);
- b4=new JButton("PREVIOUS");
- b4.addActionListener(this);
- b4.setSize(150,30);
- b4.setLocation(400,180);
- add(b4);
- setTitle("Q.20 Phone Book Demo");
- setSize(600,500);
- setVisible(true);
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- }
- public void actionPerformed(ActionEvent ae)
- {
- String s=(String)ae.getActionCommand();
- if(s.equals("ADD"))
- {
- String name=tf1.getText();
- String address=tf2.getText();
- String phone=tf3.getText();
- String email=tf4.getText();
- if(name.length()==0 || address.length()==0||phone.length()==0||email.length()==0)
- {
- JOptionPane.showMessageDialog(null,"Can not add data because few Fields are empty");
- return;
- }
- if(phone.charAt(0)=='-')
- {
- JOptionPane.showMessageDialog(null,"Can not add data because phone number field is negative");
- return;
- }
- try
- {
- ps=con.prepareStatement("insert into phone values(?,?,?,?)");
- ps.setString(1,name);
- ps.setString(2,address);
- ps.setString(3,phone);
- ps.setString(4,email);
- ps.executeUpdate();
- JOptionPane.showMessageDialog(null,"Record is added Succesfully....");
- tf1.setText("");
- tf2.setText("");
- tf3.setText("");
- tf4.setText("");
- stmt=con.createStatement();
- rs=stmt.executeQuery("select * from phone");
- }
- catch(Exception e)
- {
- JOptionPane.showMessageDialog(null,"Problem While Inserting Record...."+e);
- }
- }//ADD
- if(s.equals("DELETE"))
- {
- String name=tf1.getText();
- String address=tf2.getText();
- String phone=t
Add Comment
Please, Sign In to add comment