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

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

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

   public void actionPerformed( ActionEvent e )
   {
      try {
         if ( !fields.name.getText().equals( "" ) &&
                   fields.lastname.getText().equals( "" )) {
            Statement statement =connection.createStatement();
            String query = "SELECT * FROM adressbook " +
                           "WHERE name = '" +
                           fields.name.getText() + "'";
            output.append( "\nname querry is send: " +
                           connection.nativeSQL( query )
                           + "\n" );
            ResultSet rs = statement.executeQuery( query );
            display( rs );
            output.append( "\n name querry s successfull\n" );
            statement.close();
         }
         else if ( !fields.lastname.getText().equals( "" ) &&
         fields.name.getText().equals( "" )) {
            Statement statement =connection.createStatement();
            String query = "SELECT * FROM adressbook " +
                           "WHERE lastname = '" +
                           fields.lastname.getText() + "'";
            output.append( "\nQuery is send: " +
                           connection.nativeSQL( query )
                           + "\n" );
            ResultSet rs = statement.executeQuery( query );
            display( rs );
            output.append( "\n lastname querry s successfullý\n" );
            statement.close();
         }
         else if (!((fields.lastname.getText().equals( "" )
         && fields.lastname.getText().equals( "" )))) {
            Statement statement =connection.createStatement();
            String query = "SELECT * FROM adressbook " +
                           "WHERE lastname = '" +
                           fields.lastname.getText() + "'"+
                           " AND name = '" +
                           fields.name.getText() + "'";
            output.append( "\n name+lastname Query is sending: " +
                           connection.nativeSQL( query )
                           + "\n" );
            ResultSet rs = statement.executeQuery( query );
            display( rs );
            output.append( "\n name + lastname Query is successful\n" );
            statement.close();
         }
         else
            fields.lastname.setText(
               "Burada soyismi (ve/veya üste ismi) girin ve bul düðmesine basýn" );
      }
      catch ( SQLException sqlex ) {
         sqlex.printStackTrace();
         output.append( sqlex.toString() );
      }
   }

   public void display( ResultSet rs )
   {
      try {
         rs.next();
         //int recordNumber = rs.getInt( 1 );
         //if ( recordNumber != 0 ) 
         {
               fields.name.setText(rs.getString( 1 ));
               fields.lastname.setText(rs.getString( 2 ));
               fields.adress.setText(rs.getString( 3 ));
               fields.city.setText(rs.getString( 4 ));
               fields.state.setText(rs.getString( 5 ));
               fields.zipcode.setText(rs.getString( 6 ));
               fields.country.setText(rs.getString( 7 ));
               fields.email.setText(rs.getString( 8 ));
               fields.home_phone.setText(rs.getString( 9 ));
               fields.work_phone.setText(rs.getString( 10 ));
               fields.mobile_phone.setText(rs.getString( 11 ));
               fields.fax.setText(rs.getString( 12 ));
               fields.web_adress.setText(rs.getString( 13 ));
               fields.home_adress.setText(rs.getString( 14 ));
         }

     }
      catch ( SQLException sqlex ) {
         sqlex.printStackTrace();
         output.append( sqlex.toString() );
      }
   }
}
