0

.next() is not working in this code. I tried a lot but in vain. For example, if I have 10 rows in branch_issuance_g_item_vise (DB table name). This code prints 10 times the first row only.

void stockIssuance(String date) throws SQLException{
    report_table.setVisible(true);
    
    Statement s = db.conn().createStatement();
    ResultSet rs = s.executeQuery("SELECT * FROM branch_issuance_g_item_vise");
    
    DefaultTableModel dt = (DefaultTableModel) report_table.getModel();
    dt.setRowCount(0);
    
    Vector v = new Vector();
    if(!rs.next()){
        m.printError("No Data Found");
        rs.beforeFirst();
    }
    else{
        while(rs.next()){
            v.add(rs.getString("item_name"));
            v.add(rs.getString("qty"));

            dt.addRow(v);
        }
    }
}

Even, this is not working.

void stockIssuance(String date) throws SQLException{
    report_table.setVisible(true);
    
    Statement s = db.conn().createStatement();
    ResultSet rs = s.executeQuery("SELECT * FROM branch_issuance_g_item_vise");
    
    DefaultTableModel dt = (DefaultTableModel) report_table.getModel();
    dt.setRowCount(0);
    
    Vector v = new Vector();
    while(rs.next()){
        v.add(rs.getString("item_name"));
        v.add(rs.getString("qty"));

        dt.addRow(v);
    }
}
  • try to remove the `if(!rs.next())`. Just keep the while loop. – Eran Oct 18 '20 at 09:48
  • I tried, but the same result. – Muhammad Akif Saleem Oct 18 '20 at 09:50
  • The logic of that `if` will cause it to skip a line. And you keep using the same vector. So you are actually updating all its previous instances. You have to use a new Vector in each iteration. – RealSkeptic Oct 18 '20 at 09:52
  • The second code block is what you should use, but you need to move the `Vector v = new Vector();` statement *inside* the `while` loop, so each call to `addRow()` adds a different vector object. Same as described in the [duplicate link](https://stackoverflow.com/questions/19843506/why-does-my-arraylist-contain-n-copies-of-the-last-item-added-to-the-list) up top. – Andreas Oct 18 '20 at 09:56
  • @RealSkeptic thanks a lot brother. Can you please explain, why It needs to create a new Vector Object in every iteration? – Muhammad Akif Saleem Oct 18 '20 at 10:03

3 Answers3

0

You should ceate a new vector v at the start of the while loop. As is, the rows will be appended to the vector but you only have two columns.

Furthermore, every time you call rs.next the resultset is advanced one step. Easiest fix would be to remove the check if the resultset is empty.

0

Add rs.beforeFirst before while, and move the Vector ArrayList creation inside loop

else {
    rs.beforeFirst();
    while(rs.next()) {
       ArrayList v = new ArrayList();

Moves the cursor to the front of this ResultSet object, just before the first row. This method has no effect if the result set contains no rows.

You should use ArrayList instead of Vector

Ori Marko
  • 56,308
  • 23
  • 131
  • 233
0

First of all, you should call res.beforeFirst() before the else condition, because everytime the if statements checks the condition, you are doing rs.next() and when you are doing else condition you are skipping the first element by doing rs.next() again.

To solve your problem, I suggest you to do something like this, without using a vector:

ResultSet res=stat.executeQuery(query);
        while (res.next()){
            model.setRowCount(model.getRowCount() + 1);
            int currentRow = model.getRowCount() - 1;
            model.setValueAt(res.getString("param1"), currentRow, 0);
            model.setValueAt(res.getString("param2"), currentRow, 1);
            model.setValueAt(res.getString("param3"), currentRow, 2);
            model.setValueAt(res.getString("param4"), currentRow, 3);
            
        }

In this example I'm using a row counter and I have 4 fields, like "Name|Surname|Date|ID|" that's why you see 0,1,2,3. If you just have one column in your Table, just use

model.setValueAt(res.getString("something"), currentRow, 0);

If you keep want to use your vector, you need to init it everytime in the while loop:

Vector v;
while(rs.next()){
   v=new Vector();
   .....
}
Stefano Leone
  • 635
  • 5
  • 14