Resultset to Arraylist

Hi,

I have a resultset from a query in a bean which I want to populate a 2D arraylist with.

The arraylist then gets passed back to the displaypage, and I need to get the values from the arraylist in a table.

I seem to get the first row of the arraylist displayed in the table on every row.

Here is the code:

Bean:

ArrayList Rows = new ArrayList();
ArrayList Row = new ArrayList();

while (rs.next())
{
for (int i = 1; i <= colcount ; i++)
{
Row.add(rs.getString(i));
}
rows.add(Row);

Display Page:

ArrayList Rows = new ArrayList();
Rows = db.getArrayList();
int rowcount = Rows.size();

for (int i = 0;i < rowcount ; i++)
{
id = ((ArrayList)Rows.get(i)).get(0).toString();
make = ((ArrayList)Rows.get(i)).get(1).toString();
model  = ((ArrayList)Rows.get(i)).get(2).toString();

out.println("<tr>");
out.println("<td>");
out.println("<font id='blacktextthin'>" + id + "</font>");
out.println("</td>");
out.println("<td>");
out.println("<font id='blacktextthin'>" + make + "</font>");
out.println("</td>");
out.println("<td>");
out.println("<font id='blacktextthin'>" + model + "</font>");
out.println("</td>");


Any ideas or ways to improve on the code?
barnarpAsked:
Who is Participating?
 
bloodredsunConnect With a Mentor Commented:
I would say your best bet is to create a javabean that can be your Data Transfer Object. Rather than complex arraylists of arraylists.

The reason your code is repeating the first 3 is that you never create a new arraylist for each row, but just keep adding to the old one.

Do this:

      while (rs.next()){
            ArrayList row = new ArrayList();
            for (int i = 1; i <= columnCount ; i++){
                  row.add(rs.getString(i));
            }
            rows.add(row);
      }

And change your variables so that they are lowercase as that is normal convention.
0
 
bloodredsunCommented:
Why are you replacing the ResultSet with an object that has less functionality? My advice would be to use the resultset properly.
0
 
barnarpAuthor Commented:
So what you are saying:

Should I have a jsp page that references a "database" bean. In the dbbean, I would run the query, get a resultset and return the resultset to the jsp page?

0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
barnarpAuthor Commented:
Is it not better practice to store the database results in another object, ie arraylist, then to work with the open resultset?
0
 
bloodredsunCommented:
No it's not better practise. A resultset is an object designed specifically for the result of an sql query, why bother re-inventing the wheel by coming up with your own version of it.
0
 
barnarpAuthor Commented:
Seems every person has their own thoughts on the matter.

By using the resultset, you cannot immediately close the connection as the resultset will be destroyed. Another "best practise" is to close the connection,statement and resultset immediately after db operations are comlete.

Would you then place ther connection in a session maybe, and only when the session is closed, close the connection?
0
 
bloodredsunCommented:
I would recommend the usage of a Data Transfer Object (DTO) like a javabean if you are looking to pass this information between pages, in the same way that Struts does it with an ActionForm, but this is different from mapping a resultset to an ArrayList.

>>Would you then place ther connection in a session maybe, and only when the session is closed, close the connection?
No, I would use a DataSource instead. This creates a pool of connections which you can then re-use. This prevents the lag time associated with creating new connections, aswell as being the best way to use connections in an efficient and scalable manner.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.