Conversion ResultSet to ArrayList

For the conversion is necessary to have a data class (in this case User). We use while loop in this case. Every iteration we create a new object and insert data to them. After that we insert the object into an ArrayList. Direct conversion of ResultSet in ArrayList does not exist.

ArrayList = new ArrayList();
ResultSet rs = stmt.executeQuery("SELECT * FROM tblUsers");
while (rs.next()) {
  int id = rs.getInt("userid");
  String name = rs.getString("username");
  User user = new User(id, name);
  arrayList.add(user);
}


This entry was posted in Java. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>