Jtable values ​​duplicating the last value

I am developing a Java application for my college education. I ran into a problem while loading values ​​into a Jtable. I used the following code to load values. He makes duplicate entries. Can you help me get rid of this problem?

 private void LoadSupplierTable() {
    clearTable((javax.swing.table.DefaultTableModel) tblSupp.getModel());
    javax.swing.table.DefaultTableModel dtSupp = (javax.swing.table.DefaultTableModel) tblSupp.getModel();
    Vector ver = new Vector();
    try {
        ResultSet resO = null;
        String sqo = "select id,name from supplier";
        Connection coo = DataBaseConnection.getDbConnection();
        PreparedStatement pso = coo.prepareStatement(sqo);
        resO = pso.executeQuery();
        while (resO.next()) {

            ver.add(resO.getInt("id"));
            ver.add(resO.getString("name"));
            dtSupp.addRow(ver);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
+4
source share
1 answer

You have to declare a vector inside the while loop ... just copy paste the following code

 private void LoadSupplierTable() {
    clearTable((javax.swing.table.DefaultTableModel) tblSupp.getModel());
    javax.swing.table.DefaultTableModel dtSupp = (javax.swing.table.DefaultTableModel) tblSupp.getModel();

    try {
        ResultSet resO = null;
        String sqo = "select id,name from supplier";
        Connection coo = DataBaseConnection.getDbConnection();
        PreparedStatement pso = coo.prepareStatement(sqo);
        resO = pso.executeQuery();
        while (resO.next()) {
            Vector ver = new Vector();
            ver.add(resO.getInt("id"));
            ver.add(resO.getString("name"));
            dtSupp.addRow(ver);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
+1
source

All Articles