songbird159
songbird159

Reputation: 39

JDBC Mysql Select don't return all rows results

I have a db with 4 rows in a column called 'category'

My code says:

try {
        String query = "SELECT category FROM descriptionLink";
        ResultSet rs = con.statement.executeQuery(query);

        while (rs.next()){
            ta_results.setText(rs.getString("category")+"\n");
        } rs.close();
    } catch (SQLException erro){
    JOptionPane.showMessageDialog(null, "Error: "+ erro);
}

When it runs, it just return the LAST row. Why? I want it to return all the 4 rows, and print those results in a text area (ta_results)

Upvotes: 0

Views: 255

Answers (1)

Eran
Eran

Reputation: 393781

It looks like each row you read overwrites the previous one:

ta_results.setText(rs.getString("category")+"\n");

Try to accumulate the output :

    StringBuilder sb = new Stringuilder();
    while (rs.next()){
        sb.append(rs.getString("category")+"\n");
    } 
    rs.close();
    ta_results.setText(sb.toString());

Upvotes: 2

Related Questions