// Class FindRecord defintion
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.*;

public class ProcurandoRegistro implements ActionListener {
   private ScrollingPanel fields;
   private JTextArea output;
   private Connection connection;

   public ProcurandoRegistro( Connection c, ScrollingPanel f,
                      JTextArea o )
   {
      connection = c;
      fields = f;
      output = o;
   }

   public void actionPerformed( ActionEvent e )
   {
      try {
         if ( !fields.clcdesc.getText().equals( "" ) ) {
            Statement statement =connection.createStatement();
            String query = "SELECT * FROM TBcliente " +
                           "WHERE clcdesc like '" +
                           fields.clcdesc.getText() + "'";
            output.append( "\nEnviando query : " + 
                           connection.nativeSQL( query ) 
                           + "\n" );
            ResultSet rs = statement.executeQuery( query );
            display( rs );
            output.append( "\nQuery bem sucedida\n" );
            statement.close();
         }
         else 
            fields.clcdesc.setText( 
               "Entre com a descricao e precione Procurar" );
      }
      catch ( SQLException sqlex ) {
         sqlex.printStackTrace();
         output.append( sqlex.toString() );
      }
   }

   // Display results of query. If rs is null
   public void display( ResultSet rs )
   {
      try {         
         rs.next();

         int recordNumber = rs.getInt( 1 );

         if ( recordNumber != 0 ) {
            fields.clncodg.setText( String.valueOf( recordNumber));
            fields.clcdesc.setText( rs.getString( 2 ) );
            fields.clyrend.setText( rs.getString( 3 ) );
         }
         else
            output.append( "\nNenhum registro encontrado\n" );         
      }
      catch ( SQLException sqlex ) {
         sqlex.printStackTrace();
         output.append( sqlex.toString() );
      }
   }
}

/**************************************************************************
 * (C) Copyright 1999 by Deitel & Associates, Inc. and Prentice Hall.     *
 * All Rights Reserved.                                                   *
 *                                                                        *
 * DISCLAIMER: The authors and publisher of this book have used their     *
 * best efforts in preparing the book. These efforts include the          *
 * development, research, and testing of the theories and programs        *
 * to determine their effectiveness. The authors and publisher make       *
 * no warranty of any kind, expressed or implied, with regard to these    *
 * programs or to the documentation contained in these books. The authors *
 * and publisher shall not be liable in any event for incidental or       *
 * consequential damages in connection with, or arising out of, the       *
 * furnishing, performance, or use of these programs.                     *
 *************************************************************************/
