// Fig. 18.30: FindRecord.java
// Class FindRecord defintion
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.firma_acik_unvani.getText().equals( "" ) &&
                   fields.telefon_1.getText().equals( "" )) {
            Statement statement =connection.createStatement();
            String query = "SELECT * FROM REHBER " +
                           "WHERE firma_acik_unvani LIKE '" +
                           fields.firma_acik_unvani.getText() + "*'";
            output.append( "\nsorgu gönderildi: " +
                           connection.nativeSQL( query )
                           + "\n" );
            ResultSet rs = statement.executeQuery( query );
            display( rs );
            output.append( "\n şirket ismi sorgusu başarılı\n" );
            statement.close();
         }
         else if ( !fields.telefon_1.getText().equals( "" ) &&
         fields.firma_acik_unvani.getText().equals( "" )) {
            Statement statement =connection.createStatement();
            String query = "SELECT * FROM REHBER " +
                           "WHERE telefon_1 = '" +
                           fields.telefon_1.getText() + "*'";
            output.append( "\nsorgu gönderildi: " +
                           connection.nativeSQL( query )
                           + "\n" );
            ResultSet rs = statement.executeQuery( query );
            display( rs );
            output.append( "\n telefon sorgusu başarılı\n" );
            statement.close();
         }
         else if (!((fields.telefon_1.getText().equals( "" )
         && fields.telefon_1.getText().equals( "" )))) {
            Statement statement =connection.createStatement();
            String query = "SELECT * FROM REHBER " +
                           "WHERE telefon_1 LIKE '" +
                           fields.telefon_1.getText() + "*'"+
                           " AND firma_acik_unvani LIKE '" +
                           fields.firma_acik_unvani.getText() + "*'";
            output.append( "\n name+lastname Query is sending: " +
                           connection.nativeSQL( query )
                           + "\n" );
            ResultSet rs = statement.executeQuery( query );
            display( rs );
            output.append( "\nsorgu başarılı\n" );
            statement.close();
         }
         else
            fields.telefon_1.setText(
               "Buraya firma telefon numarasını 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.channel_no.setText(rs.getString( 1 ));
fields.müsteri_tipi.setText(rs.getString( 2 ));
fields.firma_acik_unvani.setText(rs.getString( 3 ));
fields.fatura_adresi1.setText(rs.getString( 4 ));
fields.fatura_adresi2.setText(rs.getString( 5 ));
fields.fatura_adresi3.setText(rs.getString( 6 ));
fields.telefon_1.setText(rs.getString( 7 ));
fields.telefon_2.setText(rs.getString( 8 ));
fields.telefon_3.setText(rs.getString( 9 ));
fields.yetkili_kisi.setText(rs.getString( 10 ));
fields.email.setText(rs.getString( 11 ));
fields.web.setText(rs.getString( 12 ));
fields.urun_sevk_adresi1.setText(rs.getString( 13 ));
fields.urun_sevk_adresi2.setText(rs.getString( 14 ));
fields.banka_hesap1.setText(rs.getString( 15 ));
fields.banka_hesap2.setText(rs.getString( 16 ));	
fields.ek_not.setText(rs.getString( 17 ));

         }

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

