jButton de Atualizar numa Jtable consulta

8 respostas
diego_perez

Pessoal, como posso fazer um botao de Atualizar a Jtable depois que eu inserir no banco e já retornar a Jtable COnsulta

ja tentei de varias formas

getJFrame().disable();
getJFrame().enable();

getJFrame().setVisible(false);
getJFrame().serVisible(true);

tabela.serVisible(true);
tabela.serVisible(false);

tabela.repaint();

alguem tem mais alguma saida?

ta a class a baixo :

public PDV(){
		super();
		
			
		String url = "jdbc:mysql://localhost:3307/sistemacomercial";
		String usuario = "root";
		String senha = "123456";
		try{
		Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver" );
		con = DriverManager.getConnection(url, usuario, senha);
		}
		catch (Exception e){
		JOptionPane.showMessageDialog(null,"Conexão não estabelecida","Mensagem do Programa",
		JOptionPane.ERROR_MESSAGE);
		}
		
		buscaTabela();
		
	}
	
private Component buscaTabela(){
		Statement st;
		ResultSet res;
	
		try{


		
		
		Vector cabecalho = new Vector();
		Vector linhas = new Vector();
		st = con.createStatement();
		res = st.executeQuery("SELECT * FROM PDV ORDER BY Data");
		res.next();
		ResultSetMetaData rsmd = res.getMetaData();
		for (int i = 1; i <= rsmd.getColumnCount(); ++i)
		cabecalho.addElement(rsmd.getColumnName(i));
		do{
		linhas.addElement(proximaLinha(res,rsmd));
		}
		while (res.next());
		
		tabela = new JTable(linhas,cabecalho);
		tabela.setSize(new Dimension(693, 190));
		tabela.getColumnModel();
	    tabela.setFont(new Font("Lucida Sans", Font.CENTER_BASELINE, 14));
	    tabela.setRowHeight(20);
        tabela.updateUI(); 
	    tabela.getColumnModel().getColumn(1).setPreferredWidth(300);  
		JScrollPane scroller = new JScrollPane( tabela );
		scroller.setBorder(BorderFactory.createLineBorder(Color.black, 5));
		scroller.setBounds(new Rectangle(-1, 190, 676, 289));
		scroller.updateUI();

		
		
	getJContentPane().add(scroller, BorderLayout.CENTER);

		
		st.close();

		    
		}
		catch (SQLException sqlex){
		
		}
		return null;
		}
		private Vector proximaLinha(ResultSet rs, ResultSetMetaData rsmd){
		Vector LinhaAtual = new Vector();
		try{
		for (int i = 1; i <= rsmd.getColumnCount(); ++i)
		switch(rsmd.getColumnType(i)){
		case Types.VARCHAR: LinhaAtual.addElement(rs.getString(i));
		break;
		case Types.TIMESTAMP: LinhaAtual.addElement(rs.getDate(i));
		break;
		case Types.DOUBLE: LinhaAtual.addElement(new Double(rs.getDouble(i)));
		break;
		case Types.INTEGER: LinhaAtual.addElement(new Double(rs.getDouble(i)));
		break;
		}
		}
		catch(SQLException e){
		}
		return LinhaAtual;
		}

ai tenho um botao de inserir gostaria de quando inserisse ja atualizasse e mostrasse o q foi inserido
dicas??

obrigado

8 Respostas

andbecker

basta adicionar outra query de select depois de inserir o registro,

res = st.executeQuery(“SELECT * FROM PDV ORDER BY Data”);

pro botão atualizar a mesma coisa… chama a query de consulta acima…

diego_perez

tais falando do metodo buscaTabela(){

??? devo chamar ele de novo?

andbecker

isso mesmo!

diego_perez

Ja tentei cara
atraves de um botao

ai coloquei la

buscaTabela();

nao fez nada

tem como tu me explicar um jeito, ta complicado

andbecker

vc cria o botao atulizar e adiciona a ultima linha que eu pus no seu construtor... depois transforma o metodo busca tabela em classe [desculpe se compliquei, mas é o jeito que sei fazer funcionar]

fica algo mais ou menos assim:

SUGIRO QUE VC COPIE E COLE SEU CÓDIGO NUM NOVO ARQUIVO ANTES DE ALTERÁ-LO

public PDV() {
        super();

        String url = "jdbc:mysql://localhost:3307/sistemacomercial";
        String usuario = "root";
        String senha = "123456";
        try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            con = DriverManager.getConnection(url, usuario, senha);
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "Conexão não estabelecida", "Mensagem do Programa",
                    JOptionPane.ERROR_MESSAGE);
        }

        buscaTabela();
        btnAtualizar.addActionListener(new Atualizar());
    }

    public class Atualizar implements ActionListener {
        public void actionPerformed(ActionEvent clic) {

            Statement st;
            ResultSet res;

            try {

                Vector cabecalho = new Vector();
                Vector linhas = new Vector();
                st = con.createStatement();
                res = st.executeQuery("SELECT * FROM PDV ORDER BY Data");
                res.next();
                ResultSetMetaData rsmd = res.getMetaData();
                for (int i = 1; i <= rsmd.getColumnCount(); ++i) {
                    cabecalho.addElement(rsmd.getColumnName(i));
                }
                do {
                    linhas.addElement(proximaLinha(res, rsmd));
                } while (res.next());

                tabela = new JTable(linhas, cabecalho);
                tabela.setSize(new Dimension(693, 190));
                tabela.getColumnModel();
                tabela.setFont(new Font("Lucida Sans", Font.CENTER_BASELINE, 14));
                tabela.setRowHeight(20);
                tabela.updateUI();
                tabela.getColumnModel().getColumn(1).setPreferredWidth(300);
                JScrollPane scroller = new JScrollPane(tabela);
                scroller.setBorder(BorderFactory.createLineBorder(Color.black, 5));
                scroller.setBounds(new Rectangle(-1, 190, 676, 289));
                scroller.updateUI();

                getJContentPane().add(scroller, BorderLayout.CENTER);

                st.close();

            } catch (SQLException sqlex) {
            }
            return null;
        }

        private Vector proximaLinha(ResultSet rs, ResultSetMetaData rsmd) {
            Vector LinhaAtual = new Vector();
            try {
                for (int i = 1; i <= rsmd.getColumnCount(); ++i) {
                    switch (rsmd.getColumnType(i)) {
                        case Types.VARCHAR:
                            LinhaAtual.addElement(rs.getString(i));
                            break;
                        case Types.TIMESTAMP:
                            LinhaAtual.addElement(rs.getDate(i));
                            break;
                        case Types.DOUBLE:
                            LinhaAtual.addElement(new Double(rs.getDouble(i)));
                            break;
                        case Types.INTEGER:
                            LinhaAtual.addElement(new Double(rs.getDouble(i)));
                            break;
                    }
                }
            } catch (SQLException e) {
            }
            return LinhaAtual;
        }
    }
andbecker

eu falava da linha 16

diego_perez

Valeu amigo ajudou muito
esta RESOLVIDO

abraço

andbecker

por nada… é bom poder ajudar :smiley:

Criado 12 de abril de 2011
Ultima resposta 13 de abr. de 2011
Respostas 8
Participantes 2