import java.awt.*;
import java.io.*;
import java.sql.*;
import java.util.*;
import messagebox;

public class Responsavel extends Frame
{
   Panel p1, p2, p3;
   Label l1, l2, l3;
   TextField t1, t2;
   Choice ch1;
   Button b1, b2, b3, b4;
   messagebox mgb;

   static String url = "jdbc:odbc:Escolas.mdb";
   static String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
   static String login = "dba";
   static String passwd = "javabank";
   static Connection Conexao;

   public Responsavel()
   {
      try
      {
         Class.forName(driver);

         Conexao = DriverManager.getConnection(url,login,passwd);
         checkForWarnings(Conexao.getWarnings());
      }
      catch(SQLException e)
      {
         System.out.println(e);
      }
      catch(java.lang.Exception e)
      {
         mgb = new messagebox("Erro na abertura do banco de dados.");
         mgb.show();
      }

      setLayout(new BorderLayout());

      p1 = new Panel();
      add("North",p1);

      l1 = new Label("Codigo :");
      p1.add(l1);

      t1 = new TextField(10);
      p1.add(t1);


      p2 = new Panel();
      add("Center",p2);

      l2 = new Label("Nome :");
      p2.add(l2);

      t2 = new TextField(20);
      p2.add(t2);

      l3 = new Label("Sexo :");
      p2.add(l3);

      ch1 = new Choice();
      ch1.addItem("Masculino");
      ch1.addItem("Feminino");
      p2.add(ch1);


      p3 = new Panel();
      add("South",p3);

      b1 = new Button("Gravar");
      p3.add(b1);

      b2 = new Button("Consultar");
      p3.add(b2);

      b3 = new Button("Deletar");
      p3.add(b3);

      b4 = new Button("Fechar");
      p3.add(b4);

      setTitle("Responsavel");
      reshape(50,50,410,460);

      show();
   }

   private static void checkForWarnings(SQLWarning warn)
   {
      while (warn != null)
      {
         System.out.println(warn);
         warn = warn.getNextWarning();
      }
   }

   public boolean action(Event evt, Object arg)
   {
      if("Gravar".equals(evt.arg))
      {
         try
         {
             int Cod = Integer.valueOf(t1.getText()).intValue();

             Statement Procedimento = Conexao.createStatement();

             boolean Resultado = Procedimento.execute("INSERT INTO Responsavel VALUES("+Cod+",'"+t2.getText()+"','"+ch1.getSelectedItem()+"')");

             Procedimento.close();

             if(Resultado == true)
             {
                t1.setText("");
                t2.setText("");
                t1.requestFocus();
             }
         }
         catch(SQLException e)
         {
            e.printStackTrace();
         }
         catch(java.lang.Exception e)
         {
            mgb = new messagebox("Erro ao gravar responsavel.");
            mgb.show();
         }
         return true;
      }

      if("Deletar".equals(evt.arg))
      {
         try
         {
            int Cod = Integer.valueOf(t1.getText()).intValue();

            Statement Procedimento = Conexao.createStatement();

            boolean Resultado = Procedimento.execute("DELETE FROM Responsavel WHERE Codigo = "+Cod);

            Procedimento.close();

            if(Resultado == true)
            {
                t1.setText("");
                t2.setText("");
                t1.requestFocus();
            }
         }
         catch(SQLException e)
         {
            e.printStackTrace();
         }
         catch(java.lang.Exception e)
         {
             mgb = new messagebox("Erro ao excluir responsavel.");
             mgb.show();
         }
         return true;
      }

      if("Consultar".equals(evt.arg))
      {
         try
         {
             t2.setText("");

             float Cod = Float.valueOf(t1.getText()).floatValue();

             Statement Procedimento = Conexao.createStatement();

             ResultSet Resultado = Procedimento.executeQuery("SELECT Nome, Sexo FROM Responsavel WHERE  Codigo = "+Cod);

             while (Resultado.next())
             {
                 String nome = Resultado.getString("Nome");
                 String sexo = Resultado.getString("Sexo");

                 t2.setText(nome);

                 Busca_Sexo(sexo);
             }

             if(t2.getText().equals(""))
             {
                mgb = new messagebox("Registro nao encontrado.");
                mgb.show();
             }

             Resultado.close();
             Procedimento.close();
         }
         catch(SQLException e)
         {
            e.printStackTrace();
         }
         catch(java.lang.Exception e)
         {
             mgb = new messagebox("Erro na leitura do arquivo de responsavel.");
             mgb.show();
         }
         return true;
      }

      if("Fechar".equals(evt.arg))
      {
          hide();
          return true;
      }
      return true;
   }

   public boolean Busca_Sexo(String sexo)
   {
       for(int i = 0; i < ch1.countItems();i++)
       {
           if(ch1.getItem(i).equals(sexo))
           {
               ch1.select(sexo);
           }
       }
       return true;
   }
}

