• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Displaying Multiple Columns And Records from database.

 
Johnny Woosh
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello there,i have a table called 'user_meaasurement' and i need to display all the columns and its records onto a JSP page.any idea how to display all the columns and its records out? my rs.getstring can only retrieve one column and its record. Im not really sure how to do it.any help is appreciated.thanks.
The following are my codes

 
craig a chapman
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
rs.next() moves the cursor to the next row of your database, at which point you retrieve all the column data you need to make up your record. Conceptually you have a user who has some measurements - waist size, shoe size etc. You could have a java class that represents this data and in each loop create a new instance, set the details and add to an array list or similar, the present your data from the list containing all your users info
 
vidhyasagar reddy
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ResultSet rs = stmt.executeQuery("SELECT a, b, c FROM TABLE2");
ResultSetMetaData rsmd = rs.getMetaData();
int numberOfColumns = rsmd.getColumnCount();
while(rs.next())
{
for(i=1;i<=numberOfColumns;i++)
{
System.out.println(rs.getString(i)+ " ");
}
}
 
craig a chapman
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's best to try and avoid processing ResultSets and accessing databases directly from your jsp pages if you can.

You can do something like:

String sql = SELECT * FROM user_measurement
ResultSet result = statement.executeQuery(sql);
User user = null
List list = new ArrayList();

while(result.next)
{
user = new User();
user.setWaist(result.getString("waist");
...
...
l.add(user);
}
return list;

Then you can use taglibs to display your data.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic