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

后端 未结 7 1898
渐次进展
渐次进展 2020-12-17 08:53

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

相关标签:
7条回答
  • 2020-12-17 09:05
    • 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.
    0 讨论(0)
  • 2020-12-17 09:06

    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.

    0 讨论(0)
  • 2020-12-17 09:14

    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.

    0 讨论(0)
  • 2020-12-17 09:15

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

    0 讨论(0)
  • 2020-12-17 09:22

    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.

    0 讨论(0)
  • 2020-12-17 09:25

    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

    0 讨论(0)
提交回复
热议问题