views:

540

answers:

5

Hi I want to retrieve a set of records from a database, do a rs.next() and then assign the result of this to a variable to pass to a method that will use this record, in the same way that I would without assigning it to a variable and passing it to a method is there any way to do this? I'm using JAVA (1.5)

A: 

You want to assign the result of rs.next() to a variable?

I'm afraid you cannot do that. You can have a reference from that rs and process it in other method ( the same way you would without assign it to a var )

When you pass the rs as an argument to a method that is exactly what you're doing already.

while( rs.next() ) { 
    list.add( createFrom( rs ) );
}

...

public Object createFrom( ResultSet rs ) {  // Here rs is a copy of the reference in the while above. 
}

Did I get right your question?

OscarRyz
Technically, you can, it returns a bool :P
Vinko Vrsalovic
I realise now that my question was not correct, as rs.next() returns a boolean value, not the current row. If the resultSet is passed to a method, is it passed with the current row being the same as the row in the calling method?
kilhra
Yes, it is the same! So you cannot pass the result set without the possibility the other is calling next()
Arne Burmeister
A: 

I think I understood you now. :)

No, it’s not possible to do it that way. The ResultSet changes its internal state on each call to ResultSet.next() so storing the reference to it won’t get you anywhere. You have to extract the data you want into some custom object and store that.

CustomData cd = new CustomData();
cd.setString1(rs.getString(1));
cd.setInt1(rs.getInt(2));
...

You can then store the cd variable and get on with processing your ResultSet.

Bombe
A: 

Thank you for all the answers

I do not want to pass the whole resultSet to a method, only the current row, but as I understand, it is not possible to do this

kilhra
nope, you have to fetch the values contained in the rs first with rs.getString or rs.getDate etc. depending on your data.
OscarRyz
Pity, that was exactly what I wanted to avoid, because I wanted to process the record in a separate method
kilhra
Well the only way is to automate this process, but I don't think it worth it. You can grab the resultset metadata and using reflection fill the attributes of an object. Sounds a little bit too much. But is an alternative. See Arne answer for this.
OscarRyz
+1  A: 

No, that is not supported out of the box, but maybe the following idea may help you:

ResultSetMetaData meta = rs.getMetaData();
while (rs.next()) {
  Map<String,Object> row = new HashMap<String,Object>();
  for (int i = 1; i <= meta.getColumnCount(); ++i) {
    row.put(meta.getColumnName(i), rs.getObject(i));
  }
  processRow(row);
}

The problem is, you need to cast the values from the row-map in processRow() and it will not work for all type/driver combinations (BLOBs, ...).

Arne Burmeister
A: 

I often find List> data structure very useful while working with result sets.