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)
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?
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
.
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
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, ...).