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.
我有一些现有的代码接受包含从Oracle数据库检索的信息的java.sql.ResultSet。我现在想重用这段代码,但我想传递一个ResultSet对象,我自己从一些与任何数据库无关的内存数据创建。是否存在可用于此的现有Java框架类? ResultSet有很多方法,所以实现我自己的类似乎有点矫枉过正,尽管我可以忽略大多数方法来解决我的具体情况。
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.
我正在考虑旧的Microsoft ADO记录集对象的内容,我可以在其中创建字段,然后填充每个字段的行数据。这看起来像是一个容易混淆的问题,但我一直无法找到任何好的指针。
6 个解决方案
#1
15
- Create your own
AbstractResultSet
class, one that (like AbstractQueue) implements all methods by throwing UnsupportedOperationException (Eclipse autogenerates these methods in a split second). - 创建自己的AbstractResultSet类,它(如AbstractQueue)通过抛出UnsupportedOperationException(Eclipse会在瞬间自动生成这些方法)来实现所有方法。
- Now extend
AbstractResultSet
. The subclass can override only the methods you're interested in implementing. - 现在扩展AbstractResultSet。子类只能覆盖您有兴趣实现的方法。
#2
8
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
.
这是一个稍微左侧的解决方案,但您可以使用模拟框架(例如JMock)。这些框架通常用于创建单元测试接口的模拟实现,但我认为没有理由使用它来创建java.sql.ResultSet的“部分实现”。
For example, say you only wanted to implement the getString()
method, and nothing else:
例如,假设您只想实现getString()方法,而不是其他:
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
相当不正统,相当繁琐,但它应该工作
#3
3
You could take a look at the CachedRowSet interface:
您可以查看CachedRowSet接口:
http://java.sun.com/j2se/1.5.0/docs/api/javax/sql/rowset/CachedRowSet.html
http://java.sun.com/j2se/1.5.0/docs/api/javax/sql/rowset/CachedRowSet.html
which allows you work in disconnected mode.
这允许您在断开连接模式下工作。
#4
2
java.sql.ResultSet is an interface, so you could create your own class that implements that interface.
java.sql.ResultSet是一个接口,因此您可以创建自己的类来实现该接口。
#5
1
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).
我的第一个选择是重构代码,以便它采用List
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).
如果这不可行或时间有效,那么“有趣”的黑客就是查询内存中的H2数据库以获取ResultSet。如果没有为ResultSet找到合理的存根,则可以轻松实例化,它可能比滚动自己更快(拉入jar,编写20行代码来创建数据库并填充表)。
#6
0
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.
我通常不会回答java问题,因为我不是java开发人员,但如果你需要从代码创建一个sql对象以传递给方法来回收方法,这似乎是一个架构缺陷。我想你会想让你的接收方法接受一些其他更具体的输入形式(比如自定义的对象数组)以使其可重用,然后将ResultData解析为该格式。
#1
15
- Create your own
AbstractResultSet
class, one that (like AbstractQueue) implements all methods by throwing UnsupportedOperationException (Eclipse autogenerates these methods in a split second). - 创建自己的AbstractResultSet类,它(如AbstractQueue)通过抛出UnsupportedOperationException(Eclipse会在瞬间自动生成这些方法)来实现所有方法。
- Now extend
AbstractResultSet
. The subclass can override only the methods you're interested in implementing. - 现在扩展AbstractResultSet。子类只能覆盖您有兴趣实现的方法。
#2
8
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
.
这是一个稍微左侧的解决方案,但您可以使用模拟框架(例如JMock)。这些框架通常用于创建单元测试接口的模拟实现,但我认为没有理由使用它来创建java.sql.ResultSet的“部分实现”。
For example, say you only wanted to implement the getString()
method, and nothing else:
例如,假设您只想实现getString()方法,而不是其他:
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
相当不正统,相当繁琐,但它应该工作
#3
3
You could take a look at the CachedRowSet interface:
您可以查看CachedRowSet接口:
http://java.sun.com/j2se/1.5.0/docs/api/javax/sql/rowset/CachedRowSet.html
http://java.sun.com/j2se/1.5.0/docs/api/javax/sql/rowset/CachedRowSet.html
which allows you work in disconnected mode.
这允许您在断开连接模式下工作。
#4
2
java.sql.ResultSet is an interface, so you could create your own class that implements that interface.
java.sql.ResultSet是一个接口,因此您可以创建自己的类来实现该接口。
#5
1
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).
我的第一个选择是重构代码,以便它采用List
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).
如果这不可行或时间有效,那么“有趣”的黑客就是查询内存中的H2数据库以获取ResultSet。如果没有为ResultSet找到合理的存根,则可以轻松实例化,它可能比滚动自己更快(拉入jar,编写20行代码来创建数据库并填充表)。
#6
0
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.
我通常不会回答java问题,因为我不是java开发人员,但如果你需要从代码创建一个sql对象以传递给方法来回收方法,这似乎是一个架构缺陷。我想你会想让你的接收方法接受一些其他更具体的输入形式(比如自定义的对象数组)以使其可重用,然后将ResultData解析为该格式。