ok, tá ai a classe inteira. é um peq. formulário com alguns campos. onde o user digita o código que quer fazer pesquisa, e ao acertar o botão 'consulta' ele recebe os dados do ID correspondente, podendo faze alteração. mas eu não consigo joga o conteudo do banco pros campos de texto, como deu pra nota :D
import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.text.ParseException;
import javax.swing.AbstractButton;
import javax.swing.ComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.text.JTextComponent;
import javax.swing.text.MaskFormatter;
import classes_conexao.gConexaoConfig;
/**
* @author Mauricio
*/
public class Alterar extends JFrame {
JLabel jLb_ID_Cliente, jLb_Nome, jLb_RG, jLb_CPF, jLb_estado, jLb_municipio, jLb_endereco, jLb_numero,
jLb_fone, jLb_cell, jLb_mail, jLb_OBS;
JTextField jTF_ID, jTF_nome, jTF_endereco, jTF_numero, jTF_mail;
JTextArea jTA_OBS;
JComboBox jCB_estado, jCB_municipio;
JButton jBT_gravar, jBT_cancelar, jBT_consultar;
JFormattedTextField jFT_RG, jFT_CPF, jFT_fone, jFT_cell;
MaskFormatter jMask_RG, jMask_cpf, jMask_fone;
int []combo;
public Alterar(){
super("Alterar - Pessoa Física");
Container cont = getContentPane();
cont.setLayout(null);
//Instanciamento
try {
jLb_ID_Cliente= new JLabel("Código");
jLb_Nome = new JLabel("Nome");
jLb_RG = new JLabel("RG");
jLb_CPF = new JLabel("CPF");
jLb_estado = new JLabel("Estado");
jLb_municipio = new JLabel("Município");
jLb_endereco = new JLabel("Endereço");
jLb_numero = new JLabel("N°");
jLb_fone = new JLabel("Telefone");
jLb_cell = new JLabel("Celular");
jLb_mail = new JLabel("E-Mail");
jLb_OBS = new JLabel("Observações");
jTF_ID= new JTextField(null); jTF_ID.setEnabled(true);
jTF_nome = new JTextField(null);
jTF_endereco = new JTextField(null);
jTF_numero = new JTextField(null);
jTF_mail = new JTextField(null);
jMask_fone = new MaskFormatter ("(##)####-####"); jMask_fone.setPlaceholderCharacter('_');
jMask_RG = new MaskFormatter ("##########"); jMask_RG.setPlaceholderCharacter('_');
jMask_cpf = new MaskFormatter ("###.###.###-##"); jMask_cpf.setPlaceholderCharacter('_');
jFT_fone = new JFormattedTextField(jMask_fone);
jFT_cell = new JFormattedTextField(jMask_fone);
jFT_RG = new JFormattedTextField(jMask_RG);
jFT_CPF = new JFormattedTextField(jMask_cpf);
jTA_OBS = new JTextArea();
jCB_estado = new JComboBox();
jCB_municipio = new JComboBox();
jBT_gravar = new JButton("Gravar");
jBT_cancelar = new JButton("Cancelar");
jBT_consultar = new JButton("Consultar");
} catch (Exception e) {
e.printStackTrace();
}
//Adicinar ao container
try {
cont.add(jLb_ID_Cliente); jLb_ID_Cliente.setBounds(5, 10, 40, 20);
cont.add(jLb_Nome); jLb_Nome.setBounds (50, 10, 60, 20);
cont.add(jLb_RG); jLb_RG.setBounds (205, 10, 65, 20);
cont.add(jLb_CPF); jLb_CPF.setBounds (275, 10, 70, 20);
cont.add(jLb_estado); jLb_estado.setBounds (5, 60, 60, 20);
cont.add(jLb_municipio); jLb_municipio.setBounds (70, 60, 75, 20);
cont.add(jLb_endereco); jLb_endereco.setBounds (150, 60, 150, 20);
cont.add(jLb_numero); jLb_numero.setBounds (340, 60, 35, 20);
cont.add(jLb_fone); jLb_fone.setBounds (5, 110, 90, 20);
cont.add(jLb_cell); jLb_cell.setBounds (100, 110, 90, 20);
cont.add(jLb_mail); jLb_mail.setBounds (195, 110, 200, 20);
cont.add(jLb_OBS); jLb_OBS.setBounds (5, 185, 380, 20);
cont.add(jTF_ID); jTF_ID.setBounds(5, 35, 30, 20);
cont.add(jTF_nome); jTF_nome.setBounds (40, 35, 150, 20);
cont.add(jFT_RG); jFT_RG.setBounds (195, 35, 75, 20);
cont.add(jFT_CPF); jFT_CPF.setBounds (275, 35, 100, 20);
cont.add(jTF_endereco); jTF_endereco.setBounds (150, 85, 185, 20);
cont.add(jTF_numero); jTF_numero.setBounds (340, 85, 30, 20);
cont.add(jFT_fone); jFT_fone.setBounds (5, 135, 90, 20);
cont.add(jFT_cell); jFT_cell.setBounds (100, 135, 90, 20);
cont.add(jTF_mail); jTF_mail.setBounds (195, 135, 180, 20);
cont.add(jTA_OBS); jTA_OBS.setBounds (10, 210, 360, 100);
cont.add(jCB_estado); jCB_estado.setBounds (5, 85, 65, 20);
cont.add(jCB_municipio); jCB_municipio.setBounds (70, 85, 75, 20);
cont.add(jBT_gravar); jBT_gravar.setBounds (140, 325, 100, 35);
cont.add(jBT_cancelar); jBT_cancelar.setBounds (250, 325, 100, 35);
cont.add(jBT_consultar); jBT_consultar.setBounds (20, 325, 100, 35);
} catch (Exception e) {e.printStackTrace();System.out.println(e);}
jBT_consultar.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent arg0) {
altera();
}
});
//---------------------------------------------------------------------------------------------------------------
jBT_cancelar.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent evento){
if((jTF_nome.getText()).length() == 0){
if((jFT_RG.getText()).length() == 0)
if((jFT_CPF.getText()).length() == 0)
if((jTF_endereco.getText()).length() == 0)
if((jTF_numero.getText()).length() == 0)
if((jFT_fone.getText()).length() == 0)
if((jFT_cell.getText()).length() == 0)
if((jTF_mail.getText()).length() == 0)
if((jTA_OBS.getText()).length() == 0){}
// if((jCB_estado.getText()).length() == 0){}
// if((jCB_municipio.getText()).length() == 0){}
dispose();
}else{
int status = JOptionPane.showConfirmDialog(null,"Deseja realmente fechar o " +
"programa?","Mensagem de saída",JOptionPane.YES_NO_OPTION);
if (status == JOptionPane.YES_OPTION){
//segundaJanela.setVisible(false);
dispose();
}
}
}
});
//**************\
//cont.setBackground(Color.white);
setSize(400,400);
setResizable(false);
setLocationRelativeTo(null);
setVisible(true);
} //chave construtor
private void altera(){
try
{ String id;
Connection con;
con = gConexaoConfig.getConexao();
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("SELECT ID, Nome from pessoas as id, nome where ID = "+jTF_ID+ "");
rs.next();
id=Integer.toString(rs.getInt(0));
jTF_nome= id.getText();
st.close();
con.close();
}catch(Exception e){System.out.print(e);}
}
public static void main(String args[]) throws ParseException{
Alterar app = new Alterar();
app.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
}
public void setModalExclusionType(boolean b) {
// TODO Auto-generated method stub
}
}