tags:

views:

597

answers:

6

I have some existing code that accepts a java.sql.ResultSet that contains info retrieved from an Oracle database. I would now like to reuse this code, but I'd like to pass it a ResultSet object that I create myself from some in-memory data that is not affiliated with any database. Is there an existing Java framework class that I can use for this? ResultSet has tons of methods, so implementing my own class for this seemed like overkill, even though I could ignore most of the methods for my specific case.

I was thinking of something along the lines of the old Microsoft ADO recordset object, where I could create the fields and then populate the row data for each field. This seemed like an easily googlable question, but I've been unable to find any good pointers.

A: 

You could take a look at the CachedRowSet interface:

http://java.sun.com/j2se/1.5.0/docs/api/javax/sql/rowset/CachedRowSet.html

which allows you work in disconnected mode.

davek
A: 

I don't normally answer java questions, as I'm not a java developer, but this seems like an architectural flaw if you need to create a sql object from code to pass into a method in order to recycle a method. I would think you would want to make your receiving method accept some other more specific form of input (such as a custom defined object array) to make it reusable, and then parse your ResultData into that format.

md5sum
Oops... i meant `ResultSet', and it's an interface rather than a concrete class, so I thought that'd be okay. The ResultSet can be pretty variable in its content, so making a List<SomeType> seemed to be not the best choice.
Chris Farmer
+1  A: 

My first option would be to refactor the code so that it takes List<Map<String,Object>> or something appropriate (that is, List<Map<String,Object>> if the data being moved around has no real structure or fully-typed objects if it does have structure).

If that's not feasible or time efficient, the "fun" hack is to query an in-memory H2 database to get a ResultSet. If you don't find a reasonable stub for ResultSet you can easily instantiate, it might be quicker than rolling your own (pull the jar in, write 20 lines of code for creating the db and populating a table).

alex
Agreed, that's what I was trying to get at in java terms. +1
md5sum
+1  A: 

java.sql.ResultSet is an interface, so you could create your own class that implements that interface.

rayd09
He explicitly said that implementing ResultSet is a ton of work (it really is).
alex
Keith Randall
It would be if your are trying to achieve all of the functionality of the database specific ResultSet classes, but if you are only attempting to mimic a ResultSet for some specific cases then it shouldn't be difficult at all. It is like he said. He could ignore most of the methods.
rayd09
I'd recommend implementing only the methods you need (YAGNI)
Greg Smith
+1  A: 

This is a slightly left-field solution, but you could make use of a mocking framework (e.g. JMock). These frameworks are generally intended for creating mock implementations of interfaces for unit testing, but I see no reason why you could use one to create a "partial implementation" of java.sql.ResultSet.

For example, say you only wanted to implement the getString() method, and nothing else:

Mockery mockery = new Mockery();
final ResultSet resultSet = mockery.mock(ResultSet.class);

mockery.checking(new Expectations() {{
    allowing(resultSet).getString(1); will(returnValue("my first string"));
    allowing(resultSet).getString(2); will(returnValue("my second string"));
}});

// resultSet is now a java.sql.ResultSet object, which you can pass to your legacy code
resultSet.getString(1);

Rather unorthodox, and quite cumbersome, but it should work

skaffman
+3  A: 
  • Create an AbstractResultSet, one that (like AbstractQueue) implements all methods by throwing UnsupportedOperationException (Eclipse does that in a split second).
  • Now extend AbstractResultSet. The subclass can override only the methods you're interested in implementing.
Joel
Thanks. I ended up going this route. The AbstractResultSet class is a nice layer to use for the noise of all the `throw` statements, and the final implementation class can contain only meaningful code.
Chris Farmer