How to delete data from Jtable and insert it again?

Asked

Viewed 84 times

1

Through a button that has Coo function update the table I want the data to be removed and then come back, my code until then this so:

public class atualizando implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent arg0) {
        if (arg0.getSource() == btnAtualizar) {

            DefaultTableModel tblRemove = (DefaultTableModel) tabela.getModel();
            DefaultTableModel tblRemove2 = (DefaultTableModel) tabela2.getModel();

                while (tabela.getModel().getRowCount() > 0) {  
                       ((DefaultTableModel) tabela.getModel()).removeRow(0);  
                   }
            while(tabela2.getModel().getRowCount() > 0) {
                ((DefaultTableModel) tabela2.getModel()).removeRow(0);  
            }


            try {
                Class.forName("com.mysql.cj.jdbc.Driver");
                EnderecoDB = "jdbc:mysql://22.222.222:3306/system";
                Conexao = DriverManager.getConnection(EnderecoDB, "estoque", "123456");
                Comando = Conexao.createStatement();

                ResultSet rsRegistro3;
                rsRegistro3 = Comando.executeQuery("SELECT * FROM Produtos ORDER BY Referencia");

                String Campo3[] = new String [4];
                int intNumRegistro3 = 0,intRegistro3 = 0;
                rsRegistro3.beforeFirst();
                rsRegistro3.next();
                intRegistro3 = rsRegistro3.getInt("Num_venda");
                Campo3[0] = Integer.toString(intRegistro3);
                Campo3[1] = rsRegistro3.getString("Produto");
                Campo3[2] = rsRegistro3.getString("Preco");
                Campo3[3] = rsRegistro3.getString("quantidade");
                ModeloAgenda2.insertRow(0, Campo);

                intNumRegistro3 = 1;
                while(rsRegistro.next()) {
                    intRegistro3 = rsRegistro3.getInt("Num_venda");
                    Campo3[0] = Integer.toString(intRegistro3);
                    Campo3[1] = rsRegistro3.getString("Produto");
                    Campo3[2] = rsRegistro3.getString("Preco");
                    Campo3[3] = rsRegistro3.getString("quantidade");
                    ModeloAgenda2.insertRow(intNumRegistro3, Campo3);
                }

            }catch(Exception e ) {
                System.out.println("deu um erro: "+e);
            }

        }
    }
  • Tested like this: removeRow.setNumRows(0) ?

1 answer

-1

I work with Javafx tableview, which is more boring. See if you have the remote:

tabela.removeAll();

Then you fill it with the new data.

A tip, don’t keep creating connection from DB all the time, over time you will have hundreds of connections from the same person. Create the connection only 1 time. Then when you want to use it, just call prepareStatement(linhasql); then run it();

Thanks.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.