[Resolvido] Ajuda java com mysql - atualização e exclusão em tempo real

1 resposta
javamysql
R

Boa noite.
Estou estudando Java com BD MySQL com cenexão JDBC.
É uma aplicação simples com acesso ao banco de dados.
O fato é que a conexão está ok e tudo funciona normalmente, porém, o banco de dados (no form) só atualiza após uma inserção, atualização ou exclusão quando eu fecho o form e abro novamente. Podem me dar uma luz?
Segue o código:

Classe de conexão:
package controle;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

import javax.swing.JOptionPane;

import modelo.Pessoa;

/**
*

  • @author RalphChaos
    */
    public class Conecta {

    //RESPONSÁVEL POR PREPARAR E REALIZAR
    //PESQUISAS E INSERÇÕES NO BANCO DE DADOS
    public Statement stm;

    //RESPONSÁVEL POR ARMAZENAR O RESULTADO DE
    //UMA PESQUISA PASSADA PARA O STATEMENT
    public ResultSet rs;

    //REALIZA A CONEXÃO COM O BANCO DE DADOS
    public Connection conn = null;

    String connectionUrl = jdbc:mysql://localhost:3306/bdtardeaulaum;
    
    String usuario = root;
    
    String senha = “”;
    

    public Conecta(){

    }

    //MÉTODO DE CONEXÃO COM O BANCO DE DADOS
    public Connection conectandoBanco(){

    try {
         conn = DriverManager.getConnection(connectionUrl, usuario, senha);
     } catch (Exception ex) {
         JOptionPane.showMessageDialog(null, "Erro de conexão! \n"
         + ex.getMessage()
         + ex.getStackTrace());
     }
     return conn;
    

    }

    //MÉTODO PARA CADASTRAR PESSOAS
    
    public int cadastraPessoas(Pessoa cadPessoa){
    
    int linha = 0;
    
    try {
    
    stm = conn.createStatement();
    
    linha = stm.executeUpdate("insert into tbaulaum "
    
    + "(nome, cidade, salario, idade) values "
    
    + (’” + cadPessoa.getNome() + “’,’” + cadPessoa.getCidade() + “’,+ cadPessoa.getSalario() + , + cadPessoa.getIdade() + ));
    
    } catch (SQLException ex) {
    
    JOptionPane.showMessageDialog(null, ex.getMessage());
    
    }
    
    return linha;
    
    }
    
    public void desconecta(){
    
    try {
    
    conn.close();
    
    } catch (SQLException ex) {
    
    JOptionPane.showMessageDialog(null, “Erro ao fechar a conexão! \n erro: + ex.getMessage());
    
    }
    
    }
    

    public void executaSql(String sql) {

    try {
         /*Statement stm
                 = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
                         ResultSet.CONCUR_READ_ONLY);*/
                 Statement stm
                 = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
                         ResultSet.CONCUR_UPDATABLE);
         rs = stm.executeQuery(sql);
     } catch (SQLException ex) {
         JOptionPane.showMessageDialog(null, "Erro ao executar SQL!\n Erro:" + ex.getMessage());
     }
    

    }

    public void excluir(int codigo) {
    
    try {
    
    stm = conn.createStatement();
    
    stm.execute(“DELETE FROM tbaulaum WHERE codigo =”
    
    + codigo);
    
    JOptionPane.showMessageDialog(null, “Registro excluído!”);
    
    } catch (SQLException ex) {
    
    JOptionPane.showMessageDialog(null, “Erro ao excluir!\n Erro:
    
    + ex.getMessage());
    
    ex.getStackTrace();
    
    }
    
    }
    
    }
    

Classe Pessoa:
package modelo;

/**
*

  • @author RalphChaos
    */
    public class Pessoa {

    //ATRIBUTOS DA CLASSE
    
    private String nome;
    
    private String cidade;
    
    private float salario;
    
    private int idade;
    

    /**

    • @return the nome
      */
      public String getNome() {
      return nome;
      }

    /**

    • @param nome the nome to set
      */
      public void setNome(String nome) {
      this.nome = nome;
      }

    /**

    • @return the cidade
      */
      public String getCidade() {
      return cidade;
      }

    /**

    • @param cidade the cidade to set
      */
      public void setCidade(String cidade) {
      this.cidade = cidade;
      }

    /**

    • @return the salario
      */
      public float getSalario() {
      return salario;
      }

    /**

    • @param salario the salario to set
      */
      public void setSalario(float salario) {
      this.salario = salario;
      }

    /**

    • @return the idade
      */
      public int getIdade() {
      return idade;
      }

    /**

    • @param idade the idade to set
      */
      public void setIdade(int idade) {
      this.idade = idade;
      }

}

Classe Form de cadastro:
package visao;

import controle.Conecta;

import java.sql.SQLException;

import java.util.logging.Level;

import java.util.logging.Logger;

import javax.swing.JOptionPane;

import modelo.Pessoa;

/**
*

  • @author RalphChaos
    */
    public class FrmTelaPrincipal extends javax.swing.JFrame {

    Conecta objConecta = new Conecta();
    
    Pessoa cadPessoa = new Pessoa();
    
    int codigo;
    
    String sql = select * from tbaulaum;
    

    /**

    • Creates new form FrmTelaPrincipal
      
      */
      
      public FrmTelaPrincipal() {
      
      objConecta.conectandoBanco();
      
      initComponents();
      
      setLocationRelativeTo(this);
      
      objConecta.executaSql(sql);
      
      //Tratamento para iniciar com os dados carregados
      
      try {
      
      objConecta.rs.first();
      
      carregarDados();
      
      btnAnt.setEnabled(false);
      
      btnPri.setEnabled(false);
      
      btnCancel.setEnabled(false);
      
      btnLimpar.setEnabled(false);
      
      } catch (SQLException ex) {
      
      Logger.getLogger(FrmTelaPrincipal.class.getName())
      
      .log(Level.SEVERE, null, ex);
      
      }
      
      //Fim do tratamento
      
      }
      
    public void limpar() {
    
    txtNome.setText(null);
    
    txtCidade.setText(null);
    
    txtSalario.setText(null);
    
    txtIdade.setText(null);
    
    txtNome.grabFocus();
    
    }
    
    public void recebeDados() {
    
    try {
    
    cadPessoa.setNome(txtNome.getText());
    
    cadPessoa.setCidade(txtCidade.getText());
    
    cadPessoa.setSalario(Float.parseFloat(txtSalario.getText()));
    
    cadPessoa.setIdade(Integer.parseInt(txtIdade.getText()));
    
    cadastrarBanco();
    
    } catch (NumberFormatException e) {
    
    JOptionPane.showMessageDialog(this, Digite os valores corretamente);
    
    }
    
    }
    
    public void cadastrarBanco() {
    
    if (objConecta.cadastraPessoas(cadPessoa) > 0) {
    
    JOptionPane.showMessageDialog(null, Dados cadastrados com sucesso!);
    
    //limpar();
    
    btnNovo.setEnabled(true);
    
    btnPri.setEnabled(true);
    
    btnAnt.setEnabled(true);
    
    btnProx.setEnabled(true);
    
    btnUlt.setEnabled(true);
    
    btnExclui.setEnabled(true);
    
    btnLimpar.setEnabled(false);
    
    btnCadastrar.setEnabled(false);
    
    btnCancel.setEnabled(false);
    
    txtNome.setEditable(false);
    
    txtCidade.setEditable(false);
    
    txtSalario.setEditable(false);
    
    txtIdade.setEditable(false);
    
    try {
    
    objConecta.rs.first();
             carregarDados();
         } catch (SQLException ex) {
             Logger.getLogger(FrmTelaPrincipal.class.getName())
                     .log(Level.SEVERE, null, ex);
         }
     } else {
         JOptionPane.showMessageDialog(this, "Erro ao cadastrar!");
     }
    

    }

    public final void carregarDados() {
    
    try {
    
    objConecta.rs.refreshRow();
    
    codigo = objConecta.rs.getInt(codigo);
    
    txtNome.setText(objConecta.rs.getString(nome));
    
    txtCidade.setText(objConecta.rs.getString(cidade));
    
    txtSalario.setText(objConecta.rs.getString(salario));
    
    txtIdade.setText(objConecta.rs.getString(idade));
    
    btnCadastrar.setEnabled(false);
    
    } catch (SQLException ex) {
    
    JOptionPane.showMessageDialog(null, A tabela está vazia. Não existem cadastros.);
    
    }
    
    }
    

    /**

    • This method is called from within the constructor to initialize the form.

    • WARNING: Do NOT modify this code. The content of this method is always

    • regenerated by the Form Editor.
      
      */
      
      @SuppressWarnings(unchecked)
      
      // 
      
      private void initComponents() {
      
      jTextField1 = new javax.swing.JTextField();
      
      jLabel1 = new javax.swing.JLabel();
      
      jLabel2 = new javax.swing.JLabel();
      
      jLabel3 = new javax.swing.JLabel();
      
      jLabel4 = new javax.swing.JLabel();
      
      txtNome = new javax.swing.JTextField();
      
      txtCidade = new javax.swing.JTextField();
      
      txtSalario = new javax.swing.JTextField();
      
      txtIdade = new javax.swing.JTextField();
      
      btnCadastrar = new javax.swing.JButton();
      
      btnLimpar = new javax.swing.JButton();
      
      btnNovo = new javax.swing.JButton();
      
      btnCancel = new javax.swing.JButton();
      
      btnExclui = new javax.swing.JButton();
      
      btnPri = new javax.swing.JButton();
      
      btnAnt = new javax.swing.JButton();
      
      btnProx = new javax.swing.JButton();
      
      btnUlt = new javax.swing.JButton();
      
      btnSair = new javax.swing.JButton();
      

      jTextField1.setText(“jTextField1”);

      setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
      setType(java.awt.Window.Type.UTILITY);

      jLabel1.setText(“Nome”);

      jLabel2.setText(“Cidade”);

      jLabel3.setText(“Salário”);

      jLabel4.setText(“Idade”);

      txtNome.setEditable(false);

      txtCidade.setEditable(false);

      txtSalario.setEditable(false);

      txtIdade.setEditable(false);

      btnCadastrar.setMnemonic(a);
      
      btnCadastrar.setText(Cadastrar);
      
      btnCadastrar.addActionListener(new java.awt.event.ActionListener() {
      
      public void actionPerformed(java.awt.event.ActionEvent evt) {
      
      btnCadastrarActionPerformed(evt);
      
      }
      
      });
      
      btnLimpar.setMnemonic(L);
      
      btnLimpar.setText(Limpar);
      
      btnLimpar.addActionListener(new java.awt.event.ActionListener() {
      
      public void actionPerformed(java.awt.event.ActionEvent evt) {
      
      btnLimparActionPerformed(evt);
      
      }
      
      });
      
      btnNovo.setMnemonic(N);
      
      btnNovo.setText(Novo);
      
      btnNovo.addActionListener(new java.awt.event.ActionListener() {
      
      public void actionPerformed(java.awt.event.ActionEvent evt) {
      
      btnNovoActionPerformed(evt);
      
      }
      
      });
      
      btnCancel.setMnemonic(C);
      
      btnCancel.setText(Cancelar);
      
      btnCancel.addActionListener(new java.awt.event.ActionListener() {
      
      public void actionPerformed(java.awt.event.ActionEvent evt) {
      
      btnCancelActionPerformed(evt);
      
      }
      
      });
      
      btnExclui.setMnemonic(E);
      
      btnExclui.setText(Excluir);
      
      btnExclui.setToolTipText("");
      
      btnExclui.addActionListener(new java.awt.event.ActionListener() {
      
      public void actionPerformed(java.awt.event.ActionEvent evt) {
      
      btnExcluiActionPerformed(evt);
      
      }
      
      });
      
      btnPri.setMnemonic(P);
      
      btnPri.setText(Primeiro);
      
      btnPri.addActionListener(new java.awt.event.ActionListener() {
      
      public void actionPerformed(java.awt.event.ActionEvent evt) {
      
      btnPriActionPerformed(evt);
      
      }
      
      });
      
      btnAnt.setMnemonic(t);
      
      btnAnt.setText(Anterior);
      
      btnAnt.addActionListener(new java.awt.event.ActionListener() {
      
      public void actionPerformed(java.awt.event.ActionEvent evt) {
      
      btnAntActionPerformed(evt);
      
      }
      
      });
      
      btnProx.setMnemonic(r);
      
      btnProx.setText(Próximo);
      
      btnProx.addActionListener(new java.awt.event.ActionListener() {
      
      public void actionPerformed(java.awt.event.ActionEvent evt) {
      
      btnProxActionPerformed(evt);
      
      }
      
      });
      
      btnUlt.setMnemonic(i);
      
      btnUlt.setText(Último);
      
      btnUlt.addActionListener(new java.awt.event.ActionListener() {
      
      public void actionPerformed(java.awt.event.ActionEvent evt) {
      
      btnUltActionPerformed(evt);
      
      }
      
      });
      
      btnSair.setMnemonic(s);
      
      btnSair.setText(Sair);
      
      btnSair.addActionListener(new java.awt.event.ActionListener() {
      
      public void actionPerformed(java.awt.event.ActionEvent evt) {
      
      btnSairActionPerformed(evt);
      
      }
      
      });
      
      javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
      
      getContentPane().setLayout(layout);
      
      layout.setHorizontalGroup(
      
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      
      .addGroup(layout.createSequentialGroup()
      
      .addGap(21, 21, 21)
      
      .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
      
      .addComponent(jLabel4)
      
      .addComponent(jLabel3)
      
      .addComponent(jLabel2)
      
      .addComponent(jLabel1))
      
      .addGap(18, 18, 18)
      
      .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
      
      .addComponent(txtNome)
      
      .addComponent(txtCidade, javax.swing.GroupLayout.DEFAULT_SIZE, 223, Short.MAX_VALUE)
      
      .addComponent(txtIdade, javax.swing.GroupLayout.PREFERRED_SIZE, 52, javax.swing.GroupLayout.PREFERRED_SIZE)
      
      .addComponent(txtSalario, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE))
      
      .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
      
      .addGroup(layout.createSequentialGroup()
      
      .addContainerGap()
      
      .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      
      .addGroup(layout.createSequentialGroup()
      
      .addComponent(btnPri, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
      
      .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
      
      .addComponent(btnAnt, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
      
      .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
      
      .addComponent(btnProx, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
      
      .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
      
      .addComponent(btnUlt, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
      
      .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
      
      .addComponent(btnSair, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
      
      .addGap(0, 0, Short.MAX_VALUE))
      
      .addGroup(layout.createSequentialGroup()
      
      .addComponent(btnNovo, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
      
      .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
      
      .addComponent(btnCancel, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
      
      .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
      
      .addComponent(btnLimpar, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
      
      .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
      
      .addComponent(btnCadastrar, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
      
      .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
      
      .addComponent(btnExclui, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
      
      .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
      
      );
      
      layout.setVerticalGroup(
      
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      
      .addGroup(layout.createSequentialGroup()
      
      .addGap(19, 19, 19)
      
      .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
      
      .addGroup(layout.createSequentialGroup()
      
      .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
      
      .addGroup(layout.createSequentialGroup()
      
      .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
      
      .addComponent(jLabel1)
      
      .addComponent(txtNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
      
      .addGap(18, 18, 18)
      
      .addComponent(jLabel2))
      
      .addComponent(txtCidade, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
      
      .addGap(18, 18, 18)
      
      .addComponent(jLabel3))
      
      .addComponent(txtSalario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
      
      .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
      
      .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
      
      .addComponent(txtIdade, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
      
      .addComponent(jLabel4))
      
      .addGap(18, 18, 18)
      
      .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
      
      .addComponent(btnNovo)
      
      .addComponent(btnCancel)
      
      .addComponent(btnExclui)
      
      .addComponent(btnLimpar)
      
      .addComponent(btnCadastrar))
      
      .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
      
      .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
      
      .addComponent(btnPri)
      
      .addComponent(btnAnt)
      
      .addComponent(btnProx)
      
      .addComponent(btnUlt)
      
      .addComponent(btnSair))
      
      .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
      
      );
      

      pack();
      }//

    private void btnLimparActionPerformed(java.awt.event.ActionEvent evt) {
    
    // TODO add your handling code here:
    
    limpar();
    
    }
    
    private void btnCadastrarActionPerformed(java.awt.event.ActionEvent evt) {
    
    // TODO add your handling code here:
    
    recebeDados();
    
    }
    
    private void btnNovoActionPerformed(java.awt.event.ActionEvent evt) {
    
    // TODO add your handling code here:
    
    limpar();
    
    btnNovo.setEnabled(false);
    
    btnPri.setEnabled(false);
    
    btnAnt.setEnabled(false);
    
    btnProx.setEnabled(false);
    
    btnUlt.setEnabled(false);
    
    btnExclui.setEnabled(false);
    
    btnLimpar.setEnabled(true);
    
    btnCadastrar.setEnabled(true);
    
    btnCancel.setEnabled(true);
    
    txtNome.setEditable(true);
    
    txtCidade.setEditable(true);
    
    txtSalario.setEditable(true);
    
    txtIdade.setEditable(true);
    
    }
    
    private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {
    
    // TODO add your handling code here:
    
    try {
    
    objConecta.rs.first();
    
    carregarDados();
    
    } catch (SQLException ex) {
    
    Logger.getLogger(FrmTelaPrincipal.class.getName())
    
    .log(Level.SEVERE, null, ex);
    
    }
    
    btnNovo.setEnabled(true);
    
    btnPri.setEnabled(true);
    
    btnAnt.setEnabled(true);
    
    btnProx.setEnabled(true);
    
    btnUlt.setEnabled(true);
    
    btnExclui.setEnabled(true);
    
    btnLimpar.setEnabled(false);
    
    btnCadastrar.setEnabled(false);
    
    btnCancel.setEnabled(false);
    
    txtNome.setEditable(false);
    
    txtCidade.setEditable(false);
    
    txtSalario.setEditable(false);
    
    txtIdade.setEditable(false);
    

    }

    private void btnSairActionPerformed(java.awt.event.ActionEvent evt) {
    
    // TODO add your handling code here:
    
    int escolha = (JOptionPane.showConfirmDialog(null, Deseja realmente sair?”,
    
    Sair, JOptionPane.YES_NO_OPTION,
    
    JOptionPane.QUESTION_MESSAGE));
    
    if (escolha == 0) {
    
    System.exit(0);
    
    }
    
    }
    
    private void btnExcluiActionPerformed(java.awt.event.ActionEvent evt) {
    
    // TODO add your handling code here:
    
    String nome;
    
    nome = txtNome.getText();
    
    int escolha = (JOptionPane.showConfirmDialog(null, "Você excluirá "
    
    + nome + “\nDeseja realmente apagar este registo?”,
    
    “Excuir Registro”, JOptionPane.YES_NO_OPTION,
    
    JOptionPane.WARNING_MESSAGE));
    
    if (escolha == 0) {
    
    objConecta.excluir(codigo);
    
    try {
    
    objConecta.rs.first();
    
    carregarDados();
    
    } catch (SQLException ex) {
    
    /<em>Logger.getLogger(FrmCadastraAluno.class.getName())
    
    .log(Level.SEVERE, null, ex);</em>/
    
    JOptionPane.showMessageDialog(null, ex);
    
    }
    
    }
    
    }
    
    private void btnPriActionPerformed(java.awt.event.ActionEvent evt) {
    
    // TODO add your handling code here:
    
    try {
    
    objConecta.rs.first();
    
    carregarDados();
    
    btnAnt.setEnabled(false);
    
    btnPri.setEnabled(false);
    
    btnProx.setEnabled(true);
    
    btnUlt.setEnabled(true);
    
    } catch (Exception ex) {
         Logger.getLogger(FrmTelaPrincipal.class.getName())
                 .log(Level.SEVERE, null, ex);
     }
    

    }

    private void btnAntActionPerformed(java.awt.event.ActionEvent evt) {
    
    // TODO add your handling code here:
    
    try {
    
    if (!objConecta.rs.isFirst()) {
    
    objConecta.rs.previous();
    
    carregarDados();
    
    btnProx.setEnabled(true);
    
    btnUlt.setEnabled(true);
    
    } else {
    
    JOptionPane.showMessageDialog(null, Primeiro Registro!);
    
    btnAnt.setEnabled(false);
    
    btnPri.setEnabled(false);
    
    }
    
    } catch (SQLException ex) {
    
    Logger.getLogger(FrmTelaPrincipal.class.getName())
    
    .log(Level.SEVERE, null, ex);
    
    }
    
    }
    
    private void btnProxActionPerformed(java.awt.event.ActionEvent evt) {
    
    // TODO add your handling code here:
    
    try {
    
    if (!objConecta.rs.isLast()) {
    
    objConecta.rs.next();
    
    carregarDados();
    
    btnAnt.setEnabled(true);
    
    btnPri.setEnabled(true);
    
    } else {
    
    JOptionPane.showMessageDialog(null, “Último Registro!);
    
    btnProx.setEnabled(false);
             btnUlt.setEnabled(false);
         }
     } catch (SQLException ex) {
         Logger.getLogger(FrmTelaPrincipal.class.getName())
                 .log(Level.SEVERE, null, ex);
     }
    

    }

    private void btnUltActionPerformed(java.awt.event.ActionEvent evt) {
    
    // TODO add your handling code here:
    
    try {
    
    objConecta.rs.last();
    
    carregarDados();
    
    btnAnt.setEnabled(true);
    
    btnPri.setEnabled(true);
    
    btnProx.setEnabled(false);
    
    btnUlt.setEnabled(false);
    
    } catch (SQLException ex) {
    
    Logger.getLogger(FrmTelaPrincipal.class.getName())
    
    .log(Level.SEVERE, null, ex);
    
    }
    
    }
    

    /**

    • @param args the command line arguments
      /
      public static void main(String args[]) {
      /
      Set the Nimbus look and feel /
      //
      /
      If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.

      • For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
        */
        try {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
        if (“Nimbus”.equals(info.getName())) {
        javax.swing.UIManager.setLookAndFeel(info.getClassName());
        break;
        }
        }
        } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(FrmTelaPrincipal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(FrmTelaPrincipal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(FrmTelaPrincipal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(FrmTelaPrincipal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //
      /* Create and display the form */
      
      java.awt.EventQueue.invokeLater(new Runnable() {
      
      public void run() {
      
      new FrmTelaPrincipal().setVisible(true);
      
      }
      
      });
      
      }
      
    // Variables declaration - do not modify
    
    private javax.swing.JButton btnAnt;
    
    private javax.swing.JButton btnCadastrar;
    
    private javax.swing.JButton btnCancel;
    
    private javax.swing.JButton btnExclui;
    
    private javax.swing.JButton btnLimpar;
    
    private javax.swing.JButton btnNovo;
    
    private javax.swing.JButton btnPri;
    
    private javax.swing.JButton btnProx;
    
    private javax.swing.JButton btnSair;
    
    private javax.swing.JButton btnUlt;
    
    private javax.swing.JLabel jLabel1;
    
    private javax.swing.JLabel jLabel2;
    
    private javax.swing.JLabel jLabel3;
    
    private javax.swing.JLabel jLabel4;
    
    private javax.swing.JTextField jTextField1;
    
    private javax.swing.JTextField txtCidade;
    
    private javax.swing.JTextField txtIdade;
    
    private javax.swing.JTextField txtNome;
    
    private javax.swing.JTextField txtSalario;
    
    // End of variables declaration
    
    }
    

1 Resposta

R

Já resolvi! Faltava fazer uma nova chamada para o objConecta.executaSql(sql);

Criado 14 de agosto de 2017
Ultima resposta 14 de ago. de 2017
Respostas 1
Participantes 1