mimi
mimi

Reputation: 279

How to display next record from database

try {
    if ( rs.next( ) ) {
        int id_col = rs.getInt("ID");
        String id = Integer.toString(id_col);
        String first = rs.getString("First_Name");
        String last = rs.getString("Last_Name");
        String job = rs.getString("Job_Title");
        textID.setText(id);
        textFirstName.setText(first);
        textLastName.setText(last);
        textJobTitle.setText(job);
    }
    else {
        rs.previous( );
        JOptionPane.showMessageDialog(Workers.this, "End of File");
    }
}
catch (SQLException err) {
    JOptionPane.showMessageDialog(Workers.this, err.getMessage());
}

I can't get the next record when i use this code .. it only shows the first record.

Upvotes: 0

Views: 2982

Answers (2)

Harry Joy
Harry Joy

Reputation: 59660

try using

while(rs.next())
{
    int id_col = rs.getInt("ID");

    String id = Integer.toString(id_col);

    String first = rs.getString("First_Name");

    String last = rs.getString("Last_Name");

    String job = rs.getString("Job_Title");

    ......
}

Hope this helps.

Upvotes: 4

Michael
Michael

Reputation: 20049

Assuming you want to loop through and show them all one by one, you'll need to use a loop.

You're using an if statement on the next method, which will only get the next one and then stop. If you want to get each of them you need to do a loop.

Upvotes: 0

Related Questions