如何从没有数据库的自定义数据以编程方式创建 Java ResultSet

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2052745/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 03:02:09  来源:igfitidea点击:

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

javasql

提问by Chris Farmer

I have some existing code that accepts a java.sql.ResultSetthat contains info retrieved from an Oracle database. I would now like to reuse this code, but I'd like to pass it a ResultSetobject 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? ResultSethas 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.

我有一些现有的代码接受java.sql.ResultSet包含从 Oracle 数据库中检索到的信息的 。我现在想重用这段代码,但我想向它传递一个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 记录集对象类似的东西,在那里我可以创建字段,然后为每个字段填充行数据。这似乎是一个很容易用谷歌搜索的问题,但我一直找不到任何好的指示。

采纳答案by Joel Shemtov

  • Create your own AbstractResultSetclass, 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.
  • 创建您自己的AbstractResultSet类,该类(如 AbstractQueue)通过抛出 UnsupportedOperationException 来实现所有方法(Eclipse 会在一瞬间自动生成这些方法)。
  • 现在扩展AbstractResultSet。子类只能覆盖您有兴趣实现的方法。

回答by davek

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.

这允许您在断开连接的模式下工作。

回答by Nathan Wheeler

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 解析为该格式。

回答by alex

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<Map<String,Object>> 或适当的东西(即 List<Map<String,Object>> 如果正在移动的数据没有真正的结构或完全-typed 对象,如果它确实有结构)。

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 行代码来创建数据库和填充表)。

回答by rayd09

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

java.sql.ResultSet 是一个接口,因此您可以创建自己的类来实现该接口。

回答by skaffman

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

相当非正统,而且相当麻烦,但它应该可以工作