• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 19000
  • Last Modified:

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?
0
barnarp
Asked:
barnarp
  • 4
  • 3
1 Solution
 
bloodredsunCommented:
Why are you replacing the ResultSet with an object that has less functionality? My advice would be to use the resultset properly.
0
 
bloodredsunCommented:
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
 
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now