How to programmatically create a Java ResultSet from custom data with no database

  • Create your own AbstractResultSet class, one that (like AbstractQueue) implements all methods by throwing UnsupportedOperationException (Eclipse autogenerates these methods in a split second).
  • Now extend AbstractResultSet. The subclass can override only the methods you're interested in implementing.

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


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.

Tags:

Sql

Java