import java.sql.*;		// Acesso a Banco de Dados relacionais
import javax.swing.*;		// JTable
import java.util.*;
import java.awt.*;
import java.awt.event.*;

public class ListarCliente implements ActionListener {
   private ScrollingPanel fields;
   private JTextArea output;
   private Connection connection;
    
   public ListarCliente(Connection c, ScrollingPanel f,
                      JTextArea o ) 
   {   
      connection = c;
      fields = f;
      output = o;
   }

    public void actionPerformed( ActionEvent e )
   {
      Statement statement;		// Comando SQL, pacote java.sql
      ResultSet resultSet;			// Resultado da consulta
      
      try {
         String query = "SELECT CLNCODG AS Codigo,"+
		" CLCDESC AS Descricao,CLYREND AS Renda FROM Tbcliente";		

         statement = connection.createStatement();	 // Obtem o objeto (não há instancia aqui)
         resultSet = statement.executeQuery( query );  // Executa o comando SQL
         displayResultSet( resultSet);
         statement.close();
      }
      catch ( SQLException sqlex ) {
         sqlex.printStackTrace();
      }
   }

   private void displayResultSet( ResultSet rs )
      throws SQLException
   {
      // position to first record
      boolean moreRecords = rs.next();  // Retorna se há registro (Não EOF (End Of File))
			// É posicionado antes do primeiro registro válido

      // If there are no records, display a message
      if ( ! moreRecords ) { // Se não há registros
		output.append ("\nResultSet nao contem registros" );
         output.append ("\nNenhum registro para exibir" );
         return;
      }

      output.append ("\nTabela Clientes da Empresa" );


      try {
         // get column heads
         ResultSetMetaData rsmd = rs.getMetaData(); // pacote java.sql
			// Informações do registro
		output.append ("\n");      
         for ( int i = 1; i <= rsmd.getColumnCount(); ++i ) // Quantidade de Colunas (campos)
		output.append (rsmd.getColumnName( i ) + "\t"); // Nome da coluna

         // get row data
         do {
output.append ("\n" + getNextRow( rs, rsmd ) );   // Formata a cada registro
         } while ( rs.next() );

      }
      catch ( SQLException sqlex ) {
         sqlex.printStackTrace();
      }
   }

   private String getNextRow( ResultSet rs, 
                              ResultSetMetaData rsmd )
       throws SQLException
   {
      String currentRow = "";
      
      for ( int i = 1; i <= rsmd.getColumnCount(); ++i )
         switch( rsmd.getColumnType( i ) ) {		// Tipo da coluna (campo)
            case Types.VARCHAR: // pacote java.sql
				  currentRow +=  rs.getString( i ) + "\t";	// Obtem o dado String
               break;
            case Types.INTEGER:
				  currentRow += new Long( rs.getLong( i ) )  + "\t";// Obtem o dado Long
               break;
            default: 
               currentRow += ( "Tipo : " + 
                  rsmd.getColumnTypeName( i ) + "\t" );
         }
      
      return currentRow;
   }
}