Segue aí, eu peguei de um livro…
package br.com.gerson.cadastros;
import java.awt.;
import java.awt.event.ActionEvent;
import javax.swing.;
import javax.swing.JTextField;
import javax.swing.border.;
import java.util.;
import java.sql.;
import java.text.;
import br.com.gerson.util.ConfiguracaoFirebird;
/**
public class Pesq1 extends JDialog {
private JPanel jContentPane = null;
private JPanel jPanel = null;
private JRadioButton radIndex1 = null;
private JRadioButton radIndex2 = null;
private JRadioButton radIndex3 = null;
private JTextField campoPsq = null;
private JButton btnPsq = null;
private JButton btnRecuperar = null;
private JButton btnSair = null;
private JTable tblRubrica = null;
private String DRV;
private String url;
private String userName;
private String password;
private Connection conn;
private String query = "";
private String nuCodigo = "";
public Pesq1() {
super();
initialize();
}
private void initialize() {
this.setSize(300,200);
this.setContentPane(getJContentPane());
this.setTitle("Pesquisar Rubricas");
// Lê arquivo de configuração
ConfiguracaoFirebird cf = new ConfiguracaoFirebird();
DRV = cf.getDRV();
url = cf.getUrl();
userName = cf.getUserName();
password = cf.getPassword();
// Carrega o driver Firebird
try {
Class.forName(DRV);
conn = DriverManager.getConnection(url, userName, password);
}
catch (ClassNotFoundException cnfex) {
System.err.println("Não foi possível carregar o driver." );
cnfex.printStackTrace();
System.exit(1); // Encerra o programa
}
catch (SQLException sqlex) {
System.err.println("Não foi possível estabelecer conexão." );
sqlex.printStackTrace();
}
// Dimensiona a tela
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
setSize(new Dimension(760,360));
setLocation((screenSize.width-760)/2, (screenSize.height-360)/2);
setVisible(false);
// Recupera dados da tabela
query = " select * from BANCOS order by CODIGO ";
pesquisaTabela();
}
private JPanel getJContentPane() {
if(jContentPane == null) {
jContentPane = new JPanel();
jContentPane.removeAll();
jContentPane.setLayout(new BorderLayout());
jContentPane.setBackground(new Color(238,238,238));
jContentPane.add(getTblRubrica(), BorderLayout.CENTER);
jContentPane.add(getJPanel(), BorderLayout.SOUTH);
}
return jContentPane;
}
private JPanel getJPanel() {
if (jPanel == null) {
jPanel = new JPanel();
jPanel.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.RAISED));
jPanel.setBackground(java.awt.Color.orange);
jPanel.add(getRadIndex1(), null);
jPanel.add(getRadIndex2(), null);
jPanel.add(getRadIndex3(), null);
jPanel.add(getCampoPsq(), null);
jPanel.add(getBtnPsq(), null);
jPanel.add(getBtnRecuperar(), null);
jPanel.add(getBtnSair(), null);
//campoPsq.requestFocus();
}
return jPanel;
}
private JRadioButton getRadIndex1() {
if (radIndex1 == null){
radIndex1 = new JRadioButton();
radIndex1.setFont(new Font("Dialog", Font.BOLD, 10));
radIndex1.setText("Código");
radIndex1.setSelected(true);
radIndex1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
if (radIndex1.isSelected()){
radIndex2.setSelected(false);
radIndex3.setSelected(false);
campoPsq.requestFocus();
}
}
});
}
return radIndex1;
}
private JRadioButton getRadIndex2() {
if (radIndex2 == null){
radIndex2 = new JRadioButton();
radIndex2.setFont(new Font("Dialog", Font.BOLD, 10));
radIndex2.setText("Banco");
radIndex2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
if (radIndex2.isSelected()){
radIndex1.setSelected(false);
radIndex3.setSelected(false);
campoPsq.requestFocus();
}
}
});
}
return radIndex2;
}
private JRadioButton getRadIndex3() {
if (radIndex3 == null){
radIndex3 = new JRadioButton();
radIndex3.setFont(new Font("Dialog", Font.BOLD, 10));
radIndex3.setText("Agência");
radIndex3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
if (radIndex3.isSelected()){
radIndex1.setSelected(false);
radIndex2.setSelected(false);
campoPsq.requestFocus();
}
}
});
}
return radIndex3;
}
private JTextField getCampoPsq() {
if (campoPsq == null) {
campoPsq = new JTextField(10);
}
return campoPsq;
}
private JButton getBtnPsq() {
if (btnPsq == null) {
btnPsq = new JButton();
btnPsq.setFont(new Font("Dialog", Font.BOLD, 11));
btnPsq.setText("Psq");
btnPsq.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
nuCodigo = "";
if (radIndex1.isSelected())
query = " select * from BANCOS "
+ " where CODIGO = " + campoPsq.getText()
+ " order by CODIGO ";
else if (radIndex2.isSelected())
query = " select * from BANCOS "
+ " where BANCO = " + campoPsq.getText()
+ " order by CODIGO ";
else if (radIndex3.isSelected())
query = " select * from BANCOS "
+ " where AGENCIA = " + campoPsq.getText()
+ " order by CODIGO ";
pesquisaTabela();
}
});
}
return btnPsq;
}
private JButton getBtnRecuperar() {
if (btnRecuperar == null) {
btnRecuperar = new JButton();
btnRecuperar.setFont(new Font("Dialog", Font.BOLD, 11));
btnRecuperar.setText("Recuperar");
btnRecuperar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
pegaValor();
fechaConexao();
setVisible(false);
dispose();
}
});
}
return btnRecuperar;
}
private JButton getBtnSair() {
if (btnSair == null) {
btnSair = new JButton();
btnSair.setFont(new Font("Dialog", Font.BOLD, 11));
btnSair.setText("Sair");
btnSair.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
nuCodigo = "";
fechaConexao();
setVisible(false);
dispose();
}
});
}
return btnSair;
}
private JTable getTblRubrica() {
if (tblRubrica == null) {
tblRubrica = new JTable();
tblRubrica.setBorder(BorderFactory.createEmptyBorder(0,0,0,0));
tblRubrica.setBackground(Color.white);
tblRubrica.setForeground(new Color(51,51,51));
tblRubrica.setGridColor(new Color(122,138,153));
}
return tblRubrica;
}
private void pesquisaTabela() {
Statement comm;
ResultSet resultSet;
try {
comm = conn.createStatement();
resultSet = comm.executeQuery(query);
exibeDados(resultSet);
comm.close();
}
catch (SQLException sqlex) {
sqlex.printStackTrace();
}
}
private void exibeDados(ResultSet rs) throws SQLException {
// Posiciona para o primeiro registro
boolean registros = rs.next();
// Verifica se a tabela contém registros
if (!registros) {
JOptionPane.showMessageDialog(this, "Tabela vazia!" );
setTitle("Sem registros para exibir" );
return;
}
setTitle("Rubricas");
Vector colunas = new Vector();
Vector linhas = new Vector();
try {
// Monta cabeçalhos das colunas
ResultSetMetaData rsmd = rs.getMetaData();
for (int i = 1; i <= rsmd.getColumnCount(); ++i) {
colunas.addElement(trataNomeColuna(rsmd.getColumnName(i)));
}
do {
linhas.addElement(recuperaDados(rs, rsmd));
} while (rs.next());
// Exibe os dados
tblRubrica = new JTable(linhas, colunas);
JScrollPane scroller = new JScrollPane(tblRubrica);
getContentPane().add(scroller, BorderLayout.CENTER);
validate();
}
catch (SQLException sqlex) {
sqlex.printStackTrace();
}
}
private Vector recuperaDados(ResultSet rs, ResultSetMetaData rsmd)
throws SQLException {
Vector dadosTabela = new Vector();
for (int i = 1; i <= rsmd.getColumnCount(); ++i) {
dadosTabela.addElement(rs.getString(i));
}
return dadosTabela;
}
private void fechaConexao() {
try {
conn.close();
}
catch (SQLException sqlex) {
System.err.println( "Não foi possível encerrar a conexão." );
sqlex.printStackTrace();
}
}
public void pegaValor() {
int row;
int col = 0;
try {
row = tblRubrica.getSelectedRow();
if (row >= 0) {
Object data = tblRubrica.getValueAt(row, col);
nuCodigo = data.toString();
}
}
catch(Exception e) {
nuCodigo = "";
}
}
private String trataNomeColuna(String str) {
String result = "";
String[] nomeColuna = new String[6];
nomeColuna[0] = "CODIGO";
nomeColuna[1] = "BANCO";
nomeColuna[2] = "AGENCIA";
nomeColuna[3] = "CONTA";
nomeColuna[4] = "LIMITE";
nomeColuna[5] = "CHEQUE";
String[] nomeTratado = new String[6];
nomeTratado[0] = "Código";
nomeTratado[1] = "Banco";
nomeTratado[2] = "Agência";
nomeTratado[3] = "Conta";
nomeTratado[4] = "Limite";
nomeTratado[5] = "Chq.Pré";
for (int i = 0; i < nomeColuna.length; ++i) {
if (str.trim().equals(nomeColuna[i].trim())) {
result = nomeTratado[i];
break;
}
}
return result;
}
public String getRubrica() {
return nuCodigo;
}
}