How to return an ArrayList from a database query?
I am querying a 开发者_如何学Godatabase for a web service using the Jersey JAX-RS. I can return a single row as XML or JSON, but I can't figure out how to return multiple records.
Do I use an ArrayList
? If so how? Does anyone have an example of how to do this?
- Create a
RowHolder
class that defines all the fields from the table - Annotate the
RowHolder
with@XmlType
- Use a
List<RowHolder>
with@XmlElement
on it.
How to get the date in the List<RowHolder>
-
List<RowHolder> list = new LinkedList<RowHolder>();
while (rs.next()) {
RowHolder holder = new RowHolder();
holder.setName(rs.get("name"));
holder.setSurname(rs.get("surname"));
holder.setEmail(rs.get("email"));
// etc. for all the relevant fields
list.add(holder);
}
精彩评论