02 May 2012

Contacts Projects : Delete Contact

package conpack;


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JTextField;
import javax.swing.JOptionPane;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

public class DeleteContact extends JFrame implements ActionListener,ListSelectionListener{
     
      JButton b,d;
      JTextField name;
      JLabel i,k;
      JList list1;
    DefaultListModel model ;
     
      DeleteContact()
      {
      setTitle("Delete Contact");
      setLayout(null);
     
      model = new DefaultListModel();
      list1 = new JList(model);
      list1.setBounds(100,70,100,100);
      add(list1);
      // Initialize the list with items
      list1.addListSelectionListener(this);
     
      b=new JButton("Delete");
      b.setBounds(20,200, 100,20);
      add(b);
      b.addActionListener(this);
     
      d=new JButton("Delete All");
      d.setBounds(180,200, 100,20);
      add(d);
      d.addActionListener(this);
     

     
      name=new JTextField(5);
      name.setBounds(100, 20, 100, 20);
      add(name);
     
     
      try{
             
            Class.forName("oracle.jdbc.driver.OracleDriver");
            Connection con=
DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE", "system","system");
           
           
            Statement st=con.createStatement();
            String str = name.getText();
           
            ResultSet rs=st.executeQuery("select * from contacts");
          int i = 0;
            while(rs.next()){

            model.add(i,rs.getString("nam"));
            i++;
          
            }
           
     

        }
      catch(Exception e1)
      {
            System.out.println(e1);
      }
     
     
     
}

      public void valueChanged(ListSelectionEvent e) {
            //number.setText(list.getSelectedValue().toString());
             try{
                    
                        Class.forName("oracle.jdbc.driver.OracleDriver");
                        Connection con=
DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE", "system","system");
                       
                       
                        Statement st=con.createStatement();
                        String str = list1.getSelectedValue().toString();
ResultSet rs=st.executeQuery("select num from contacts where nam = '"+str+"'");
                       
                     
                        while(rs.next()){
                 
                              name.setText(rs.getString(1));
            
                        }
                       
                 

                    }
                  catch(Exception e1)
                  {
                        System.out.println(e1);
                       
                  }
           
      }
           
            @Override
            public void actionPerformed(ActionEvent e) {
                  if(e.getSource()== b) // delete one by one
                   {

                  try{
                       
if((JOptionPane.showConfirmDialog(this, "Do you Want to delete?"))==0)
                        {
                       
                        Class.forName("oracle.jdbc.driver.OracleDriver");
                        Connection con=
DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE", "system","system");
                       
PreparedStatement pr=con.prepareStatement("delete from contacts where nam=(?)");
     
                        pr.setString(1,list1.getSelectedValue().toString());
                                   
                       
                        pr.executeUpdate();
                       
                        JOptionPane.showMessageDialog(this, "contact deleted");
                         dispose();
                         windows me = new windows();
                     me.setVisible(true);
                     me.setLocation(500, 400);
                       
                        }
                        else
                              {
JOptionPane.showMessageDialog(this, "operation failed");dispose();
                             
                               dispose();
                               windows me = new windows();
                           me.setVisible(true);
                           me.setLocation(500, 400);
                              }

                  }
                  catch(Exception e1)
                  {
                        System.out.println(e1);dispose();
                         dispose();
                         windows me = new windows();
                     me.setVisible(true);
                     me.setLocation(500, 400);
                  }
           
                   }
                 
                  if(e.getSource()== d) // delete all
                   {

                  try{
                 
if((JOptionPane.showConfirmDialog(this, "Do you Want to delete All?"))==0)
                        {
                       
                        Class.forName("oracle.jdbc.driver.OracleDriver");
                        Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE", "system","system");
                       
PreparedStatement pr = con.prepareStatement("TRUNCATE TABLE contacts");
     
                 
                       
                         pr.executeUpdate();
                       
JOptionPane.showMessageDialog(this, "All contacts deleted");
                         dispose();
                         windows me = new windows();
                     me.setVisible(true);
                     me.setLocation(500, 400);
                       
                        }
                        else
                              {
JOptionPane.showMessageDialog(this, "operation failed");dispose();
                             
                               dispose();
                               windows me = new windows();
                           me.setVisible(true);
                           me.setLocation(500, 400);
                              }

                  }
                  catch(Exception e1)
                  {
                       
                         System.out.println(e1);dispose();
                         dispose();
                         windows me = new windows();
                     me.setVisible(true);
                     me.setLocation(500, 400);
                    
                  }
           
                   }
           
            }
            public static void main(String ar[])
            {
                  DeleteContact as=new DeleteContact();
                  as.setSize(300, 300);
                  as.setVisible(true);
                  as.setLocation(500, 400);
            }


}







                 
           
           

     




0 comments:

Post a Comment

Related Posts with Thumbnails

Popular Posts

 
;