JDBC ResultSet is giving only one row although there are many rows in table?

后端 未结 3 1539
遇见更好的自我
遇见更好的自我 2020-12-21 11:08

I am having many rows in table and I ran the same query on my database which is MySql but java ResultSet is only giving the first row of the table. Here is my code.

相关标签:
3条回答
  • 2020-12-21 11:25
    if(resultSet.next()) {
                System.out.println(resultSet.getString("albumname"));
                allAlbumsName.add(resultSet.getString("albumname"));
            }
    

    If you would like to get all rows, it should be:

    while(resultSet.next()) {
            System.out.println(resultSet.getString("albumname"));
            allAlbumsName.add(resultSet.getString("albumname"));
        }
    

    The while statement continually executes a block of statements while a particular condition is true

    Note: As @BalusC commented, your code would introduce SQL Injection attack, it is better to use ptmt.set... Instead of constructing SQL String manually.

    0 讨论(0)
  • 2020-12-21 11:30

    Change if (resultSet.next()) { to while (resultSet.next()) {

    0 讨论(0)
  • 2020-12-21 11:41

    try while(resultSet.next()) {

    instead of if (resultSet.next()) {

    0 讨论(0)
提交回复
热议问题