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

public class changeadress implements ActionListener {
   private adresspanel fields;
   private JTextArea output;
   private Connection connection;

   public changeadress( Connection c, adresspanel f, JTextArea o )
   {
      connection = c;
      fields = f;
      output = o;
   }

   public void actionPerformed( ActionEvent e )
   {
try {
         Statement statement = connection.createStatement();
          if ( ! fields.id.getText().equals( "" ) )
         {
          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 ( !a[1].equals( "" ) &&
              !a[0].equals( "" ) )
         {
            String query = "UPDATE adres SET ";
            query+="name ='";query+=fields.name.getText();
            query+="',lastname ='";query+=fields.lastname.getText();
            query+="',adress ='";query+=fields.adress.getText();
            query+="',city ='";query+=fields.city.getText();
            query+="',state ='";query+=fields.state.getText();
            query+="',zipcode ='";query+=fields.zipcode.getText();
            query+="',country ='";query+=fields.country.getText();
            query+="',email ='";query+=fields.email.getText();
            query+="',home_phone ='";query+=fields.home_phone.getText();
            query+="',work_phone ='";query+=fields.work_phone.getText();
            query+="',mobile_phone ='";query+=fields.mobile_phone.getText();
            query+="',fax='";query+=fields.fax.getText();
            query+="',web_adress ='";query+=fields.web_adress.getText();
            query+="',home_adress ='";query+=fields.home_adress.getText();
            query+="' WHERE ID=";query+=fields.id.getText();
            output.append( "\nquery is send : " +
            connection.nativeSQL( query ) + "\n" );
            int result = statement.executeUpdate( query );
            if ( result == 1 )
               output.append( "\nrecord is changed successfully \n" );
            else {
               output.append( "\nrecord change process is unsucsessfull\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("");
                 }
            statement.close();
         }   //
         } //end of if
         else
            output.append( "\nonly existing records can be changed\n" );
      }
      catch ( SQLException sqlex ) {
         sqlex.printStackTrace();
         output.append( sqlex.toString() );
      }
   }
}
