import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.*;

public class inputadress implements ActionListener {
   private adresspanel fields;
   private JTextArea output;
   private Connection connection;
   public inputadress( Connection c, adresspanel f,JTextArea o )
   {  connection = c;
      fields = f;
      output = o;
   }
   public void actionPerformed( ActionEvent e )
   {
      try {
         Statement statement = connection.createStatement();
          String a[]=new String[14];
          a[0]=fields.name.getText();
          a[1]=fields.lastname.getText();
          a[2]=fields.adress.getText();
          a[3]=fields.city.getText();
          a[4]=fields.state.getText();
          a[5]=fields.zipcode.getText();
          a[6]=fields.country.getText();
          a[7]=fields.email.getText();
          a[8]=fields.home_phone.getText();
          a[9]=fields.work_phone.getText();
          a[10]=fields.mobile_phone.getText();
          a[11]=fields.fax.getText();
          a[12]=fields.web_adress.getText();
          a[13]=fields.home_adress.getText();
          for(int i=0;i<a.length;i++)
          {
          if(a[i].equals("")) a[i]=" ";
          }
         if ( !fields.lastname.getText().equals( "" ) &&
              !fields.name.getText().equals( "" ) ) {
            String query = "INSERT INTO adressbook (" +
                " name," +
                " lastname, " +
                " adress, " +
                " city, " +
                " state, " +
                " zipcode, " +
                " country, " +
                " email, " +
                " home_phone, " +
                " work_phone, " +
                " mobile_phone, " +
                " fax, " + 
                " web_adress, " + 
                " home_adress) "+
               " VALUES ('";
                query+=a[0]+ "', '";
                query+=a[1]+ "', '" ;
                query+=a[2]+ "', '" ;
                query+=a[3]+ "', '" ;
                query+=a[4]+ "', '" ;
                query+=a[5]+ "', '" ;
                query+=a[6]+ "', '" ;
                query+=a[7]+ "', '" ;
                query+=a[8]+ "', '" ;
                query+=a[9]+ "', '" ;
                query+=a[10]+ "', '" ;
                query+=a[11]+ "', '" ;
                query+=a[12]+ "', '" ;
                query+=a[13]+ "')";
            output.append( "\nSQL query is sent: " +
                           connection.nativeSQL( query )
                           + "\n" );
            int result = statement.executeUpdate( query );

            if ( result == 1 )
               output.append( "\nQuery operated successfully\n" );
            else {
               output.append( "\nQuery gives error\n" );
               fields.name.setText("");
               fields.lastname.setText("");
               fields.adress.setText("");
               fields.city.setText("");
               fields.state.setText("");
               fields.zipcode.setText("");
               fields.country.setText(" ");
               fields.email.setText("");
               fields.home_phone.setText("");
               fields.work_phone.setText("");
               fields.mobile_phone.setText("");
               fields.fax.setText("");
               fields.web_adress.setText("");
               fields.home_adress.setText("");
            }
         }
         else
            output.append( "\nat least name and last name should be entered  " +
                           "push the add button\n" );
         statement.close();
      }
      catch ( SQLException sqlex ) {
         sqlex.printStackTrace();
         output.append( sqlex.toString() );
      }
   }
}
