Resultset To List

后端 未结 3 1353
别那么骄傲
别那么骄傲 2020-12-05 10:33

I want to convert my Resultset to List in my JSP page. and want to display all the values. This is my query:

SELECT userId, userName 
  FROM user;


        
相关标签:
3条回答
  • 2020-12-05 10:46

    You need to iterate over the ResultSet object in a loop, row by row, to pull out each column value:

    List ll = new LinkedList();
    ResultSet rs = stmt.executeQuery("SELECT userid, username FROM USER");
    
    // Fetch each row from the result set
    while (rs.next()) {
      int i = rs.getInt("userid");
      String str = rs.getString("username");
    
      //Assuming you have a user object
      User user = new User(i, str);
    
      ll.add(user);
    }
    
    0 讨论(0)
  • 2020-12-05 10:48

    A ResultSet should never get as far as a JSP. It should be mapping into a data structure or object and closed inside the method scope in which it was created. It's a database cursor, a scarce resource. Your app will run out of them soon if you persist with such a design.

    0 讨论(0)
  • 2020-12-05 11:02

    You could always use Commons DbUtils and the MapListHandler. From the doc:

    ResultSetHandler implementation that converts a ResultSet into a List of Maps

    so it'll take a lot of boilerplate code out of your hands.

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