ArrayList output from database

I have an output that I get from my Oracle database where it takes parameters from form entries and queries the database.It list the records where it works with an ArrayList to get the output.Everything works great but was wondering if there is a more efficient way to do this?

ArrayList firstnameArray =new ArrayList();

ArrayList lastnameArray =new ArrayList();

ArrayList cityArray =new ArrayList();

String myLastname = request.getParameter("firstname");

String myFirstname = request.getParameter("lastname");

String myCity= request.getParameter("city");

//DB connection here

try{

...

while(results.next())

{

String theFirstname = results.getString("myFirstname");

String theLastname = results.getString("myLastname");

String theCity = results.getString("myCity");

firstnameArray.add(theFirstname);

lastnameArray.add(theLastname);

cityArray.add(theCity);

}

....

}

....

//output records using a for loop

for(int i = 0;i < myParameterHere;i++)

{

out.write("Record value " + firstnameArray.get(i) +" " + lastnameArray.get(i) +" " + cityArray.get(i) );

}

...

[1907 byte] By [Evergreana] at [2007-11-27 11:28:50]
# 1

More efficient? Probably, but only at the cost of other things.

For instance you could print your values straight from the result set without storing them in an ArrayList. You save on memory, but then you are mixing the presentation and data access layers.

Beware of premature optimization. Is it really wasting that much? Is it fast enough? Is there plenty of memory? Then why spend the effort to make it more efficient, but less flexible/understandable? There is always a tradeoff in this manner.

One thing I would suggest is that rather than three "parallel" lists of firstname, lastname, city, have one list of Objects, and those values as attributes of the object. It may be slightly more efficient as you only do one lookup into the list, one insertion etc etc, but that would be negligible. I suggest it more for a better design than a speed/efficiency thing.

Cheers,

evnafets

evnafetsa at 2007-7-29 16:24:24 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

Use a View Object.

Example:

public class Person{

private String firstName;

private String lastName;

private String city;

//Accessors and Mutators

}

List personsList= new ArrayList();

//DB connection here

Person person = null;

try {

...

while(results.next())

{

person = new Person();

person.setFirstName(results.getString("myFirstname"));

person.setLastName(results.getString("myLastname"));

person.setCity(results.getString("myCity"));

personsList.add(persons);

}

....

}

manuel.leiriaa at 2007-7-29 16:24:24 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

Thanks, I appreciate all the tips and info!

Evergreana at 2007-7-29 16:24:24 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...