// Fig. 18.30: FindRecord.java
// Class FindRecord defintion
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.*;

public class adresbul implements ActionListener {
   private adrespanel fields;
   private JTextArea output;
   private Connection connection;

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

   public void actionPerformed( ActionEvent e )
   {
      try {
         if ( !fields.isim.getText().equals( "" ) &&
                   fields.soyisim.getText().equals( "" )) {
            Statement statement =connection.createStatement();
            String query = "SELECT * FROM adres " +
                           "WHERE isim = '" +
                           fields.isim.getText() + "'";
            output.append( "\nisim Query'si gönderiliyor: " +
                           connection.nativeSQL( query )
                           + "\n" );
            ResultSet rs = statement.executeQuery( query );
            display( rs );
            output.append( "\n isim Query'si başarıyla sonuçlandı\n" );
            statement.close();
         }
         else if ( !fields.soyisim.getText().equals( "" ) &&
         fields.isim.getText().equals( "" )) {
            Statement statement =connection.createStatement();
            String query = "SELECT * FROM adres " +
                           "WHERE soyisim = '" +
                           fields.soyisim.getText() + "'";
            output.append( "\nQuery gönderiliyor: " +
                           connection.nativeSQL( query )
                           + "\n" );
            ResultSet rs = statement.executeQuery( query );
            display( rs );
            output.append( "\n soyisim query'si başarıyla sonuçlandı\n" );
            statement.close();
         }
         else if (!((fields.soyisim.getText().equals( "" )
         && fields.soyisim.getText().equals( "" )))) {
            Statement statement =connection.createStatement();
            String query = "SELECT * FROM adres " +
                           "WHERE soyisim = '" +
                           fields.soyisim.getText() + "'"+
                           " AND isim = '" +
                           fields.isim.getText() + "'";
            output.append( "\n isim+soyisim Query'si gönderiliyor: " +
                           connection.nativeSQL( query )
                           + "\n" );
            ResultSet rs = statement.executeQuery( query );
            display( rs );
            output.append( "\n isim + soyisim Query'si başarıyla sonuçlandı\n" );
            statement.close();
         }
         else
            fields.soyisim.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.id.setText( String.valueOf( recordNumber));
               fields.isim.setText(rs.getString( 2 ));
               fields.soyisim.setText(rs.getString( 3 ));
               fields.adres.setText(rs.getString( 4 ));
               fields.ilce.setText(rs.getString( 5 ));
               fields.il.setText(rs.getString( 6 ));
               fields.postakodu.setText(rs.getString( 7 ));
               fields.ulke.setText(rs.getString( 8 ));
               fields.emailadresi.setText(rs.getString( 9 ));
               fields.telefonev.setText(rs.getString( 10 ));
               fields.telefonis.setText(rs.getString( 11 ));
               fields.telefoncep.setText(rs.getString( 12 ));
               fields.faks.setText(rs.getString( 13 ));
               fields.webadresi.setText(rs.getString( 14 ));
               fields.evadresi.setText(rs.getString( 15 ));
         }
         else
            output.append( "\nKayıt bulunamadı\n" );
      }
      catch ( SQLException sqlex ) {
         sqlex.printStackTrace();
         output.append( sqlex.toString() );
      }
   }
}

