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
AbstractResultSet
class, one that (like AbstractQueue) implements all methods by throwing UnsupportedOperationException (Eclipse autogenerates these methods in a split second).AbstractResultSet
. The subclass can override only the methods you're interested in implementing.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.
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.
java.sql.ResultSet is an interface, so you could create your own class that implements that interface.
jOOQ has a MockResultSet for that and similar purpose, which can be used out of the box, or copied into your project.
DSLContext ctx = DSL.using(DEFAULT);
Field<Integer> col1 = DSL.field("COL1", SQLDataType.INTEGER);
Field<String> col2 = DSL.field("COL2", SQLDataType.VARCHAR(10));
Result<?> result = ctx.newResult(col1, col2);
result.add(ctx.newRecord(col1, col2).values(1, "a"));
result.add(ctx.newRecord(col1, col2).values(2, "b"));
try (ResultSet rs = new MockResultSet(result)) {
while (rs.next())
System.out.println(rs.getInt(1) + ": " + rs.getString(2));
}
Disclaimer: I work for the company behind jOOQ.
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