Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

building from ResultSet

 
liliya woland
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone,
I'm new to jsp and I have a basic question about RecordSet.
I want to build a map out of my ResultSet rs.
What I have now is :

String paramValue="";
paramValue = rs.getString("T2");
map.put("T2", paramValue);
paramValue = rs.getString("T3");
map.put("T3", paramValue);
paramValue = rs.getString("C1");
map.put("C1", paramValue);

The problem with the above is that there are many fields and I would prefer not to access them one at a time, as above.
So, can anyone tell me if there is a way I could maybe create 2 arrays out of ResultSet rs (one with fields names, one with fields values) - the actual syntax of it? And then simply have a loop for traversing the two arrays and populating my map with their values.
Thanks in advance.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65335
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moved to the JDBC forum.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65335
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is indeed a very good idea to capture the data in the result set into another structure for passing to a JSP. You want to be able to close the result set as soon as possible, and not exposing such back-end details to the UI layer is a good architectural decision.

Rather than dealing with each field one at a time, you can obtain an instance of ResultSetMetaData by calling the result set's getMetaData() method.

This will allow you to discover the fields in the result set without hard-coding them and you can easily use the information to automaticaaly generate a Map with the field/value pairs.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic