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;
I have executed that using preparedstatement and got the Resultset. But how to convert it as a List and want to display the result like this:
userID userName ------------------ 1001 user-X 1006 user-Y 1007 user-Z
You need to use ResultSet#getString to fetch the name . Now, each time you fetch one record, get the name field and add it to your list. Now, since you haven't given enough information about your DTO , so that part you need to find out, how to add this ArrayList to your DTO .
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); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With