02 May 2012

Contacts Projects : Add 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.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

public class AddContact extends JFrame implements ActionListener,ItemListener{
     
      JButton b,d;
      JTextField name,number;
      JLabel i,k;
      JComboBox group;
     
      AddContact()
      {
      setTitle("Add Contact");
      setLayout(null);
     
      b=new JButton("Add");
      b.setBounds(0,240, 100,20);
      add(b);
      b.addActionListener(this);
     
      d=new JButton("exit");
      d.setBounds(190,240, 100,20);
      add(d);
      d.addActionListener(this);
     

     
      name=new JTextField(5);
      name.setBounds(100, 50, 100, 20);
      add(name);
     
      number=new JTextField(5);
      number.setBounds(100, 100, 100, 20);
      add(number);
     
     
      String[] str = {"family","friends"};
      group = new JComboBox(str);
      group.setBounds(100, 150, 100, 20);
      add(group);
     
     
}

           
           
            @Override
            public void actionPerformed(ActionEvent e) {
                   if(e.getSource()==b)
                   {

                  try{
                        if((JOptionPane.showConfirmDialog(this, "Do you Want to Proceed?"))==0)
                        {
                        Class.forName("oracle.jdbc.driver.OracleDriver");
                        Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE", "system","system");
                        System.out.println("connected");                                             
                        PreparedStatement pr=con.prepareStatement("insert into contacts values(?,?,?)");
                       
                        pr.setString(1,name.getText());                            
                        pr.setString(2,number.getText());
                        pr.setString(3,group.getSelectedItem().toString());
                       
                        pr.executeUpdate();
                        Statement st=con.createStatement();
                        ResultSet rs=st.executeQuery("select * from contacts");
                       
                        while(rs.next())
                        {
                        System.out.println(rs.getString(1));
                        System.out.println(rs.getString(2));
                        System.out.println(rs.getString(3));
                        }
                       
                        System.out.println("Added");
                        JOptionPane.showMessageDialog(this, "contact added");
                        dispose();
                        windows me = new windows();
                    me.setVisible(true);
                    me.setLocation(500, 400);
                        }
                       
                        else
                        {

                        JOptionPane.showMessageDialog(this, "operation failed");
                        dispose();
                        windows me = new windows();
                    me.setVisible(true);
                    me.setLocation(500, 400);
                  }
                       
                       


                  }
                  catch(Exception e1)
                  {
                        System.out.println(e1);
                        JOptionPane.showMessageDialog(this,e1);
                        dispose();
                        windows me = new windows();
                    me.setVisible(true);
                    me.setLocation(500, 400);
                  }
                   }
                   
                   if(e.getSource()==d)
                   {
                            dispose();
                              windows me = new windows();
                          me.setVisible(true);
                          me.setLocation(500, 400);
                   }
           
           
            }
           
            public static void main(String ar[])
            {
                  AddContact as=new AddContact();
                  as.setSize(300, 300);
                  as.setVisible(true);
                  as.setLocation(500, 400);
            }

}
                 
           
           




     



0 comments:

Post a Comment

Related Posts with Thumbnails

Popular Posts

 
;