How can I clear the loaded data before loading the data into the table?


language

I am making a small inventory control system. This is the invoice table of my database.
Invoice form


The columns are inid(p), itid(p), cuid(p), itname, qty, sprice, total, isdate, redate.
This is the "Invoices" interface when searching for old invoices using the Invoice ID (inid). I have searched for one. Every data is properly set up as text fields and tables. But the problem is when I search another invoice, the other data will be set correctly but the table is not clear and the data of the second search will also be set to the given table under the data of the first search.
first search

This is the second search interface.
second searchGiven below is my search source code.

JDBC db = new JDBC();
public boolean b;   

  public void selectFromDate(JComboBox combodate, JDateChooser isdate, JDateChooser redate,       DateSpinner retime, DateSpinner istime,
          JTable jtable1, JTextField txtCuid, JTextField txtCuName, JTextField txtCuAdd,
          JTextField txtCuTp, JTextField txtgtotal, JTextField txtAdvance, JTextField txtDue,
          JTextField txtInvoiceID, JTextField txtDamage, JTextField txtInvoiceTotal) {
      SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd");
    SimpleDateFormat formatter2 = new SimpleDateFormat("HH:mm:ss");
    try {
        ResultSet rs1 = db.getData("SELECT * FROM invoice WHERE isdate='" + combodate.getSelectedItem().toString() + "'");
        if (rs1.next()) {
            b = true;
            while (rs1.next()) {                    
                txtInvoiceID.setText((rs1.getString("inid")));
            Vector v = new Vector();
            DefaultTableModel dd = (DefaultTableModel) jtable1.getModel();
               dd.setRowCount(0);
            v.add(rs1.getInt("itid"));
            v.add(rs1.getString("itname"));
            v.add(rs1.getInt("qty"));
            v.add(rs1.getDouble("sprice"));
            v.add(rs1.getDouble("total"));
            dd.addRow(v);

            String maindate1 = rs1.getString("isdate");
            String date1 = maindate1.substring(0, 10);
            String time1 = maindate1.substring(11, 19);

            Date d1 = formatter1.parse(date1);
            Date t1 = formatter2.parse(time1);

            isdate.setDate(d1);
            istime.setValue(t1);

            String maindate2 = rs1.getString("redate");
            String date2 = maindate2.substring(0, 10);
            String time2 = maindate2.substring(11, 19);

            Date d = formatter1.parse(date2);
            Date t = formatter2.parse(time2);

            redate.setDate(d);
            retime.setValue(t);


            try {
                ResultSet rs2 = db.getData("SELECT * FROM customer WHERE cuid='" + rs1.getInt("cuid") + "'");
                while (rs2.next()) {
                    txtCuid.setText(String.valueOf(rs1.getInt("cuid")));
                    txtCuName.setText(rs2.getString("cuname"));
                    txtCuAdd.setText(rs2.getString("cuadd"));
                    txtCuTp.setText(rs2.getString("cutp"));

                    try {
                        ResultSet rs3 = db.getData("SELECT * FROM indetails WHERE inid='" + rs1.getInt("inid") + "'");
                        if (rs3.first()) {
                            txtgtotal.setText(String.valueOf(rs3.getDouble("gtotal")));
                            txtAdvance.setText(String.valueOf(rs3.getDouble("advance")));
                            txtDue.setText(String.valueOf(rs3.getDouble("due")));
                            try {
                                ResultSet rs4 = db.getData("SELECT * FROM infinal WHERE inid=" + rs1.getInt("inid") + "");
                                if (rs4.first()) {
                                    txtDamage.setText(String.valueOf(rs4.getDouble("dcost")));
                                    txtInvoiceTotal.setText(String.valueOf(rs4.getDouble("intotal")));

                                } else {
                                    JOptionPane.showMessageDialog(null, "Invoice is not finalised");
                                }
                            } catch (Exception e) {
                                JOptionPane.showMessageDialog(null, this.getClass().getName() + " " + "rs4" + e);
                            }
                        }
                    } catch (Exception e) {
                        JOptionPane.showMessageDialog(null, this.getClass().getName() + " " + "rs3" + e);
                    }
                }
            } catch (Exception e) {
                JOptionPane.showMessageDialog(null, this.getClass().getName() + " " + "rs2" + e);
            }
            }

        } else {
            b = false;
            JOptionPane.showMessageDialog(null, this.getClass().getName() + " " + "rs1");
        }
    } catch (Exception e) {
        JOptionPane.showMessageDialog(null, this.getClass().getName() + "rs1 " + " " + e);
    }
}
}


When I comment dd.setRowCount(0); I just get the same result. please help me.
My JDBC class code.

package Model;

import static Model.JDBC.con;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.JOptionPane;

public class JDBC {

String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost/cateringnew";
static Connection con;
//10.226.50.130

private void setCon() {
    try {
        Class.forName(driver);
        con = DriverManager.getConnection(url, "root", "");
    } catch (Exception ex) {
        JOptionPane.showMessageDialog(null, ex);
    }
}

private Connection getCon() throws Exception {
    if (con == null) {
        setCon();
        System.out.println("setCon");
    }
    System.out.println("getCon");
    return con;
}


  public void putData(String sql) {
    try {
        PreparedStatement state = getCon().prepareStatement(sql);
        state.executeUpdate();
        System.out.println("putDate");

    } catch (Exception ex) {
        JOptionPane.showMessageDialog(null, ex);
    }
}

public ResultSet getData(String sql) throws Exception {

    Statement state = getCon().createStatement();
    ResultSet rset = state.executeQuery(sql);
    System.out.println("getData");
    return rset;
  }
 }
crazy programmer

You can clear UI forms in a number of ways, one of the easiest is to simply reset the model with an empty/default instance. This works great for JTable, JTree, JListand JComboBox.

Based on your sample code, you can do the following:

DefaultTableModel dd = new DefaultTableModel();
// Set the columns for the table model...
jtable1.setModel(dd);
while (rs1.next()) {                    
    txtInvoiceID.setText((rs1.getString("inid")));
    Vector v = new Vector();
    v.add(rs1.getInt("itid"));
    v.add(rs1.getString("itname"));
    v.add(rs1.getInt("qty"));
    v.add(rs1.getDouble("sprice"));
    v.add(rs1.getDouble("total"));
    dd.addRow(v);

Or you can do something like this...

DefaultTableModel dd = (DefaultTableModel) jtable1.getModel();
dd.setRowCount(0);
while (rs1.next()) {                    
    txtInvoiceID.setText((rs1.getString("inid")));
    Vector v = new Vector();
    v.add(rs1.getInt("itid"));
    v.add(rs1.getString("itname"));
    v.add(rs1.getInt("qty"));
    v.add(rs1.getDouble("sprice"));
    v.add(rs1.getDouble("total"));
    dd.addRow(v);

That would be simpler because you don't need to reconstruct the column information every time.

Related


How can I have a jQuery mobile spinner before the data is loaded?

Steven Matthews EDIT: I'm specifically looking for a solution using jQuery Mobile. jQuery Mobile has specific methods (as far as I know) that do exactly what I want. I don't want normal Javascript to have a spinning gif. This is not why I am posting this quest

How can I have a jQuery mobile spinner before the data is loaded?

Steven Matthews EDIT: I'm specifically looking for a solution using jQuery Mobile. jQuery Mobile has specific methods (as far as I know) that do exactly what I want. I don't want normal Javascript to have a spinning gif. This is not why I am posting this quest

How can I clear the table before inserting it?

boo Before reading further, please keep in mind that I have done my research and still can't find a solution. So I have a table that I want to populate with JQuery and it populates it correctly, but can't seem to clear it until it is repopulated. Here is my HT

How can I clear the table before inserting it?

boo Before reading further, please keep in mind that I have done my research and still can't find a solution. So I have a table that I want to populate with JQuery and it populates it correctly, but can't seem to clear it until it is repopulated. Here is my HT

Loading screen on page before data is loaded

Johny Wave I have a grid that loads animations on my page. What I need to do is load the data from the encrypted file and populate the textbox with it on that page. And, when it's encrypted, it will of course take some time to load. That's why I created the lo

Loading screen on page before data is loaded

Johny Wave I have a grid that loads animations on my page. What I need to do is load the data from the encrypted file and populate the textbox with it on that page. And, when it's encrypted, it will of course take some time to load. That's why I created the lo

Loading screen on page before data is loaded

Johny Wave I have a grid that loads animations on my page. What I need to do is load the data from the encrypted file and populate the textbox with it on that page. And, when it's encrypted, it will of course take some time to load. That's why I created the lo

Loading screen on page before data is loaded

Johny Wave I have a grid that loads animations on my page. What I need to do is load the data from the encrypted file and populate the textbox with it on that page. And, when it's encrypted, it will of course take some time to load. That's why I created the lo

Loading screen on page before data is loaded

Johny Wave I have a grid that loads animations on my page. What I need to do is load the data from the encrypted file and populate the textbox with it on that page. And, when it's encrypted, it will of course take some time to load. That's why I created the lo

How to clear table data

arrive I have many Devexpress controls on a form under layoutcontrol1. I use the code below to iterate over each control and clear the existing data when the user clicks the "clear" button. However, it doesn't work with LookupEdit (set EditValue to 0) and Chec

How to clear table data

arrive I have many Devexpress controls on a form under layoutcontrol1. I use the code below to iterate over each control and clear the existing data when the user clicks the "clear" button. However, it doesn't work with LookupEdit (set EditValue to 0) and Chec