Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Mapping data

Status
Not open for further replies.

kensington45

Technical User
Jun 26, 2008
9
I have something that works with data mapping but was wondering if there was a more efficient way to do this?

Java part:
Code:
....
//database part here...ResultSet rs
...

List myMap = new ArrayList();
 
while (rs.next())
{
    String firstname = rs.getString("firstname");
    String lastname = rs.getString("lastname");
    String address = rs.getString("address");
    myMap.add(firstname + "," + lastname + "," + address); 
}


output:
Code:
  //output comma delimeter data     
  out.print(myMap.get(0));

Please advise.
 
Your code is correct. My only recommandation is to use a StringBuffer or StringBuilder to fill the myMap. StringBuffers/Builders are mutable, and if you append a value, you don't need to create extra objects.

List myMap = new ArrayList();
StringBuilder buf;
while (rs.next()) {
buf = new StringBuilder(200);
buf.append(rs.getString("firstname")),
buf.append(",");
buf.append(rs.getString("lastname"));
buf.append(",");
buf.append(rs.getString("address"));
myMap.add(buf.toString());
}
 
Some ideas:

1.- Are you using the map for anything else? If not, you can just print the result

2.- Side thinking: use the database with select field1 + field2


Cheers,
Dian
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top