Consulta

3 respostas
J

Ola pessoal,

To tentando fazer uma classe de pesquisa q mostra os dados filtrados num jtable. O problema é que quando clico no botao pesquisar nao ta acontencendo nada... algume pode me ajudar e me dizer onde to errando?

import java.awt.Color;
import java.awt.Font;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.Socket;
import javax.swing.*;
import java.awt.BorderLayout;
import javax.swing.JButton;

public class DisplayQueryResults extends JDialog implements ActionListener{
	
	JLabel lblNome;
	JTextField txtPesquisar;
	JButton btnPesquisar;
	Font Fonte;
	JPanel Painel;
	static ResultSet query;	
	
	public DisplayQueryResults() throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException{
		
		//Adiciona o painel		
		Painel = new JPanel();
		setTitle("Pesquisar");
		setBounds(0,0,370,240);
		Painel.setLayout(null); 
		Painel.setBackground(Color.LIGHT_GRAY);
		Painel.setBounds(0,0,370,240);
		getContentPane().add(Painel);
		
		//Label Nome
		lblNome = new JLabel ("Nome");
		lblNome.setBounds(5,0,150,20);
		Fonte = new Font("Verdana",Font.BOLD,12);
		lblNome.setFont(Fonte);
		Painel.add(lblNome);
		
		//Campo de texto Pesquisar
		txtPesquisar = new JTextField();
		txtPesquisar.setBounds(5,20,230,20);
		txtPesquisar.setBackground(Color.WHITE);
		Painel.add(txtPesquisar);
		
		//Botao Pesquisar
		btnPesquisar = new JButton ("Pesquisar");
		btnPesquisar.setForeground(Color.BLACK);
		btnPesquisar.setBounds(245,20,110,20);
		//btnPesquisar.addActionListener(this);
		Painel.add(btnPesquisar);		
	}
		
	public void pesquisar(){
		
		String sql = "SELECT CIDCODIGO AS CODIGO, CIDNOME AS NOME, CIDUF AS UF FROM CIDADE WHERE CIDNOME LIKE '" +  txtPesquisar.getText()+ "'";
		try
		{
			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance(); 
			Connection con = DriverManager.getConnection("jdbc:odbc:PROOS","SYSDBA","masterkey");
			Statement stmt = con.createStatement();
			
			PreparedStatement ps = con.prepareStatement("SELECT CIDCODIGO AS CODIGO, CIDNOME AS NOME, CIDUF AS UF FROM CIDADE WHERE CIDNOME LIKE '" +  txtPesquisar.getText()+ "'");
			query = ps.executeQuery();
			ResultSetMetaData meta = query.getMetaData();
			int cc = meta.getColumnCount();
			
			//Monta o Grid
			Vector header = new Vector();
			for (int i = 0; i < cc; i++) {
				header.add(meta.getColumnName(i + 1));
			}
			
			Vector data = new Vector();
			while (query.next()) {
				Vector row = new Vector();
				for (int i = 0; i < cc; i++) {
					row.add(query.getString(i + 1));
				}
				data.add(row);
			}
			
			JScrollPane scrollPane = new JScrollPane(new JTable(data, header)); 
			scrollPane.setBounds( new Rectangle (5,50,350,150));		
			Painel.add(scrollPane);
		}
		catch(Exception err)
		{
			JOptionPane.showMessageDialog(null, "Erro ao conectar banco de dados : " + err.getMessage());
		}
	}

	public void actionPerformed(ActionEvent evt){

		Object Evento = evt.getSource();
		
		if (Evento == btnPesquisar){
			pesquisar();
		}
	}

	public static void main(String[] args) throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException{
		DisplayQueryResults displayQueryResults = new DisplayQueryResults();
		displayQueryResults.setVisible(true);		
	}
}

Grato,

Julio.

3 Respostas

P

Por que esta linha esta comentada?

//btnPesquisar.addActionListener(this);

teste sem as barras

Pedrosa

E outra dica crie uma classe especifica para conexão e outra para suas regras de negócio. MVC.

J

Pessoal, obrigado pelas respostas…

Maior vacilo meu… esqueci de tirar o // da linha…

Aproveitando… como faco p/ pegar o valor codigo da linha e mandar p/ outra tela?

Valeu pessoal,

Julio.

Criado 29 de maio de 2006
Ultima resposta 29 de mai. de 2006
Respostas 3
Participantes 3