Discussion Locked

Collection,Vector or What

0 helpful
7 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

Hi,

Please help.
I have resultset returned from the db.
This could be thousands and each resultset has 4 fields.

What is the best way to put this in a collection or vector or ... in a way that I can retrieve it in the JSP by one resultset and loop through the resultset for each field and go to the next resultset and loop through each field again till the rest of the resultset? Please provide sample code.

Thank you,
Nhan Nguyen

7 replies

by boyarsky

The result set is already a collection object. The method next() returns false when there are no more rows. getString(), getInt(), getDate() are used to get the data from the fields.

For example:
ResultSet rs;
while (rs.next()) {
String s1 = rs.getString(1);
String s2 = rs.getString(2);
String s3 = rs.getString(3);
String s4 = rs.getString(4);
}

by nhanaiu99

I had used the method above and add the strings to the ArrayList and be able to pass ArrayList to JSP through request object. My question here is the best way to assemble the resultset and be able to retrieve as an array of record which allows stepping through each record that has more than one fields.
Thank you for your effort,

by rcline

Create a data class that contains a string for each of your fields. And have get and set methods for each field. An instance of this class will represent one row of data. Next, create a vector which contains many instances of this class.

The code below should give you a rough idea. I'm just typing this on the fly, so don't expect it to compile.

Data Class:
public class SalesData
{
private String salesPersonName;
private String salesPersonCode;

public SalesData() { }

public String getSalesPersonName() {
return this.salesPersonName;
}
public void setSalesPersonName( String salesPersonName ) {
this.salesPersonName = salesPersonName;
}

public String getSalesPersonCode() {
return this.salesPersonCode;
}
public void setSalesPersonCode( String salesPersonCode ) {
this.salesPersonCode = salesPersonCode;
}
}

Control Code:
...
Vector v = new Vector();
for ( int i = 0; i < resultset.length; i++ ) {
SalesData sales = new SalesData();
sales.setSalesPersonName( resultset[i][0] );
sales.setSalesPersonCode( resultset[i][1] );
v.add( sales );
}

JSP code:
Vector v = get... <-- whatever your method is called
for ( int i = 0; i < v.size();i++ ) {
SalesData sales = v.get( i );
}
out.println( sales.getSalesPersonName() );
out.println( sales.getSalesPersonCode() );

by diva_cen2000

regards,
In ResultSet u have method called getObject(),which returns the object of a ResultSet row put it in a Vector.

ResultSet rs=...

Vector v=new Vector();

while(rs.next())
{
v.addElement(rs.getObject());
}

In jsp page u get the Vector Object which is going to hold all the rows returned by the Resultset.

This discussion is closed to new replies.