Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resultset To List

Tags:

java

jsp

jdbc

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 
like image 857
Gnaniyar Zubair Avatar asked Dec 27 '09 19:12

Gnaniyar Zubair


People also ask

How do I convert a ResultSet to a list?

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 .


1 Answers

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); } 
like image 108
OMG Ponies Avatar answered Sep 21 '22 20:09

OMG Ponies