Olá pessoal tô com um pequeno problema no meu projeto, ele tem quatro botões de navegações, ai tudo bem, o erro que acontece é quando eu uso um dos outros botões (exemplo o botão de localizar) eu não consigo mais usar os botões de navegações, e o outro erro que estar acontecendo é na hora de usar o botão de alterar eu não estou conseguindo fazer o update… se alguém tiver alguma dica o projeto vai anexo, vlw pessoal.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
class Clientes extends JFrame implements ActionListener
{
ResultSet rs;
Statement MeuState;
JLabel lCodCliente, lNome, lCPF, lRG, lLogradouro,
lBairro, lCEP, lCidade, lUF, lTelefone, lCelular, lFax ;
static JTextField txCodCliente, txNome, txCPF, txRG, txLogradouro,
txBairro, txCEP, txCidade, txUF,
txTelefone, txCelular, txFax ;
JButton btnPrimeiro, btnAnterior, btnProximo, btnUltimo,
btnNovo, btnInserir, btnAlterar, btnExcluir, btnLocalizar;
public static void main(String args[])
{
JFrame frame = new Clientes();
frame.setVisible(true);
WindowListener Janela = new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
};
frame.addWindowListener(Janela);
}
public Clientes()
{
lCodCliente =new JLabel("Cód. Cliente:");
lCodCliente.setBounds(10,3,70,30);
add(lCodCliente);
txCodCliente = new JTextField();
txCodCliente.setBounds(10,25,70,20);
add(txCodCliente);
lNome =new JLabel("Nome:");
lNome.setBounds(90,3,40,30);
add(lNome);
txNome = new JTextField();
txNome.setBounds(90,25, 300,20);
add(txNome);
lCPF =new JLabel("Nº do CPF:");
lCPF.setBounds(10,40,70,30);
add(lCPF);
txCPF = new JTextField();
txCPF.setBounds(10,62,92,20);
add(txCPF);
lRG =new JLabel("Nº do RG:");
lRG.setBounds(112,40,60,30);
add(lRG);
txRG = new JTextField();
txRG.setBounds(112,62, 70,20);
add(txRG);
lLogradouro =new JLabel("Logradouro:");
lLogradouro.setBounds(10,77,70,30);
add(lLogradouro);
txLogradouro = new JTextField();
txLogradouro.setBounds(10,100, 380,20);
add(txLogradouro);
lBairro =new JLabel("Bairro:");
lBairro.setBounds(10,115, 70,30);
add(lBairro);
txBairro = new JTextField();
txBairro.setBounds(10,137, 115,20);
add(txBairro);
lCEP =new JLabel("CEP:");
lCEP.setBounds(135,115, 70,30);
add(lCEP);
txCEP = new JTextField();
txCEP.setBounds(135,137, 75,20);
add(txCEP);
lCidade =new JLabel("Cidade:");
lCidade.setBounds(220,115, 70,30);
add(lCidade);
txCidade = new JTextField();
txCidade.setBounds(220,137, 120,20);
add(txCidade);
lUF =new JLabel("UF:");
lUF.setBounds(352,115,70,30);
add(lUF);
txUF = new JTextField();
txUF.setBounds(352,137, 25,20);
add(txUF);
lTelefone =new JLabel("Telefone:");
lTelefone.setBounds(10,152,70,30);
add(lTelefone);
txTelefone = new JTextField();
txTelefone.setBounds(10,175, 91,20);
add(txTelefone);
lCelular =new JLabel("Celular:");
lCelular.setBounds(111,152,70,30);
add(lCelular);
txCelular = new JTextField();
txCelular.setBounds(111,175, 91,20);
add(txCelular);
lFax =new JLabel("Fax:");
lFax.setBounds(213,152,60,30);
add(lFax);
txFax = new JTextField();
txFax.setBounds(213,175, 91,20);
add(txFax);
//:::::::: Botões ::::::::
btnPrimeiro = new JButton("<");
btnPrimeiro.setBounds(90,205,50,25);
btnPrimeiro.setToolTipText("Primeiro registro");
btnPrimeiro.addActionListener(this);
add(btnPrimeiro);
btnAnterior = new JButton("<<");
btnAnterior.setBounds(143,205,50,25);
btnAnterior.setToolTipText("Registro anterior");
btnAnterior.addActionListener(this);
add(btnAnterior);
btnProximo = new JButton(">>");
btnProximo.setBounds(196,205,50,25);
btnProximo.setToolTipText("Próximo registro");
btnProximo.addActionListener(this);
add(btnProximo);
btnUltimo = new JButton(">");
btnUltimo.setBounds(249,205,50,25);
btnUltimo.setToolTipText("Último registro");
btnUltimo.addActionListener(this);
add(btnUltimo);
btnNovo = new JButton("Novo");
btnNovo.setBounds(04,238,75,25);
btnNovo.setToolTipText("Novo cliente");
btnNovo.addActionListener(this);
add(btnNovo);
btnInserir = new JButton("Inserir");
btnInserir.setBounds(79,238,75,25);
btnInserir.setToolTipText("Click aqui para inserir o cliente");
btnInserir.addActionListener(this);
add(btnInserir);
btnAlterar = new JButton("Alterar");
btnAlterar.setBounds(154,238,75,25);
btnAlterar.setToolTipText("Click aqui para salvar alterações");
btnAlterar.addActionListener(this);
add(btnAlterar);
btnExcluir = new JButton("Excluir");
btnExcluir.setBounds(229,238,75,25);
btnExcluir.setToolTipText("Excluir cliente");
btnExcluir.addActionListener(this);
add(btnExcluir);
btnLocalizar = new JButton("Localizar");
btnLocalizar.setBounds(304,238,86,25);
btnLocalizar.setToolTipText("Para localizar um cliente basta preencher o campo código do cliente e clica em localizar!");
btnLocalizar.addActionListener(this);
add(btnLocalizar);
//::::::: fim dos botões ::::::::::
getContentPane().setLayout(null);
setTitle("Cadastro de Clientes");
setSize(400,300);
setUndecorated(true);
setLocationRelativeTo(null);
setResizable(false);
getRootPane().setWindowDecorationStyle(JRootPane.FRAME);
getRootPane().setBorder(BorderFactory.createLineBorder(Color.black,3));
String url = "jdbc:odbc:dsCadCliente";
try
{
Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver" );
Connection MinhaConexao = DriverManager.getConnection(url);
MeuState = MinhaConexao.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_READ_ONLY);
rs = MeuState.executeQuery("SELECT * FROM Clientes");
rs.first();
atualizaCampos();
}
catch(ClassNotFoundException ex)
{
System.out.println("Driver JDBC-ODBC não encontrado!");
}
catch(SQLException ex)
{
System.out.println("Problemas na conexao com a fonte de dados");
}
}
public void actionPerformed(ActionEvent e)
{
try
{
if (e.getSource()==btnPrimeiro)
rs.first(); // Primeiro registro
if (e.getSource()==btnAnterior)
rs.previous(); // Registro anterior
if (e.getSource()==btnProximo)
rs.next(); // Próximo registro
if (e.getSource()==btnUltimo)
rs.last(); // Último registro
atualizaCampos();
}
catch(SQLException ex)
{
JOptionPane.showMessageDialog(null,"Erro ao tentar navegar entre os registros!");
}
if (e.getSource()==btnNovo)
{
limpaCampos();
txNome.requestFocus();
return;
}
if (e.getSource()==btnInserir)
{
Inserir();
}
if (e.getSource()==btnAlterar)
{
Alterar();
}
if (e.getSource()==btnExcluir)
{
Excluir();
}
if (e.getSource()==btnLocalizar)
{
Localizar();
}
}
public void atualizaCampos()
{
try
{
txCodCliente.setText(rs.getString("CodCliente"));
txNome.setText(rs.getString("Nome"));
txCPF.setText(rs.getString("CPF"));
txRG.setText(rs.getString("RG"));
txLogradouro.setText(rs.getString("Logradouro"));
txBairro.setText(rs.getString("Bairro"));
txCEP.setText(rs.getString("CEP"));
txCidade.setText(rs.getString("Cidade"));
txUF.setText(rs.getString("UF"));
txTelefone.setText(rs.getString("Telefone"));
txCelular.setText(rs.getString("Celular"));
txFax.setText(rs.getString("Fax"));
}
catch(SQLException ex)
{ }
}
public static void limpaCampos()
{
txCodCliente.setText("");
txNome.setText("");
txCPF.setText("");
txRG.setText("");
txLogradouro.setText("");
txBairro.setText("");
txCEP.setText("");
txCidade.setText("");
txUF.setText("");
txTelefone.setText("");
txCelular.setText("");
txFax.setText("");
}
//::::::::::::::::: Inserção do registro ::::::::::::::
public void Inserir()
{
try
{
String SQL = "INSERT INTO Clientes (Nome,CPF," +
"RG,Logradouro,Bairro,CEP,Cidade,UF,Telefone,Celular,Fax) Values ('"+
txNome.getText()+"','"+
txCPF.getText()+"','"+
txRG.getText()+"','"+
txLogradouro.getText()+"','"+
txBairro.getText()+"','"+
txCEP.getText()+"','"+
txCidade.getText()+"','"+
txUF.getText()+"','"+
txTelefone.getText()+"','"+
txCelular.getText()+"','"+
txFax.getText()+"')";
MeuState.executeUpdate(SQL);
JOptionPane.showMessageDialog(null,"Gravação realizada com sucesso!");
limpaCampos();
}
catch(SQLException ex)
{
if (ex.getMessage().equals("General error"))
JOptionPane.showMessageDialog(null,"Cliente já Cadastrado");
else
JOptionPane.showMessageDialog(null,"Dados Inválida");
}
} //::::::::::::::::: Fim da inserção ::::::::::::::
//::::::::::::::: Atualizar registro ::::::::::::::::::::
public void Alterar()
{
try
{
String SQL = "UPDATE Clientes SET "+
"CodCliente='"+txCodCliente.getText()+"',"+
"Nome='"+txNome.getText()+"',"+
"CPF='"+txCPF.getText()+"',"+
"RG='"+txRG.getText()+"',"+
"Logradouro='"+txLogradouro.getText()+"',"+
"Bairro='"+txBairro.getText()+"',"+
"CEP='"+txCEP.getText()+"' "+
"Cidade='"+txCidade.getText()+"' "+
"UF='"+txUF.getText()+"' "+
"Telefone='"+txTelefone.getText()+"' "+
"Celular='"+txCelular.getText()+"' "+
"Fax='"+txFax.getText()+"' "+
"WHERE CodCliente = '"+txCodCliente.getText()+"'";
int r = MeuState.executeUpdate(SQL);
if (r==1)
JOptionPane.showMessageDialog(null,"Atualização realizada com sucesso");
else
JOptionPane.showMessageDialog(null,"Esse Cliente ainda não está cadastrado\n Pressione Inserir");
}
catch(SQLException ex) { }
} //::::::::: Fim da atualização :::::::::::::::::
//::::::::: Excluir registro ::::::::::::::::::::::::::::
public void Excluir()
{
try
{
String SQL = "SELECT CodCliente, Nome FROM Clientes Where CodCliente = '"+txCodCliente.getText()+"'";
rs = MeuState.executeQuery(SQL);
String nome = "";
try
{
rs.next();
nome = "Deletar o Cliente: "+rs.getString("Nome");
}
catch(SQLException ex1)
{
JOptionPane.showMessageDialog(null,"Cliente não cadastrado!");
return;
}
int n = JOptionPane.showConfirmDialog(null,nome," ",JOptionPane.YES_NO_OPTION);
if (n==JOptionPane.YES_OPTION)
{
SQL = "DELETE FROM Clientes Where CodCliente = '"+txCodCliente.getText()+"'";
int r = MeuState.executeUpdate(SQL);
if (r==1)
JOptionPane.showMessageDialog(null,"Exclusão realizada com sucesso");
else
JOptionPane.showMessageDialog(null,"Não foi possível excluir o Cliente");
}
else
return;
}
catch(SQLException ex){ }
limpaCampos();
} //:::::: Fim da exclusão ::::::::::::::
//:::::::::::: Localizar Cliente ::::::::::::::::::::
public void Localizar()
{
try
{
String NomeCliente = JOptionPane.showInputDialog("Digite o nome do cliente");
rs = MeuState.executeQuery("SELECT * FROM Clientes WHERE Nome like '"+NomeCliente+"%'");
while(rs.next())
{
txCodCliente.setText(rs.getString("CodCliente"));
txNome.setText(rs.getString("Nome"));
txCPF.setText(rs.getString("CPF"));
txRG.setText(rs.getString("RG"));
txLogradouro.setText(rs.getString("Logradouro"));
txBairro.setText(rs.getString("Bairro"));
txCEP.setText(rs.getString("CEP"));
txCidade.setText(rs.getString("Cidade"));
txUF.setText(rs.getString("UF"));
txTelefone.setText(rs.getString("Telefone"));
txCelular.setText(rs.getString("Celular"));
txFax.setText(rs.getString("Fax"));
atualizaCampos();
}
}
catch(SQLException ex)
{
JOptionPane.showMessageDialog(null,"Cliente não Encontrado!");
return;
}
}
}