使用集合对象构建结果集

发布于 2024-07-12 06:22:55 字数 153 浏览 4 评论 0原文

我在使用 Java 构建结果集时遇到了问题。

我正在存储一个从结果集对象中按行组织的集合对象,并将集合对象(存储为向量/数组列表)放入缓存中并尝试检索相同的集合对象。

这里我需要使用集合对象再次构建结果集。 现在我的疑问是以这种方式构建结果集是否可行?

I had an issue in building the resultset using Java.

I am storing a collection object which is organized as row wise taken from a resultset object and putting the collection object (which is stored as vector/array list) in cache and trying to retrieve the same collection object.

Here I need to build back the resultset again using the collection object. Now my doubt is building the resultset in this way possible or not?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

仙女山的月亮 2024-07-19 06:22:55

如果您使用集合代替缓存,最好的主意是使用 CachedRowSet 而不是 ResultSet。 CachedRowSet是ResultSet的子接口,但是数据已经被缓存了。 这比将所有数据写入 ArrayList 简单得多。
CachedRowSets 也可以自行查询。

CachedRowSet rs;
.......................
.......................
Integer id;
String name;

while (rs.next())
{               
     if (rs.getInt("id") == 13)
     {
          id   = rs.getInt("id");
          name = rs.getString("name")); 
     }              
}

因此,只要需要信息,您只需调用 CachedRowSet 即可。 它几乎和切片面包一样好。 :)

编辑:
ResultSet 没有 set 方法,但有 Update 方法。 使用 Update 方法来重建 ResultSet 的问题是它需要选择要更新的行。 一旦 ResultSet 释放自身,所有行都将设置为 null。 无法调用空引用。 列表的列表模仿 ResultSet 本身,或者更准确地说,数组的数组模仿 ResultSet。

虽然向量是线程安全的,但它们会带来巨大的开销。 请改用 ArrayList。 当创建每个嵌套列表并将其放入外部嵌套列表时,以这种方式插入它。

nest.add(Collections.unmodifiableList(nested));

插入所有嵌套列表后,也将嵌套列表作为可修改列表返回。 这将为您提供一个线程安全的集合,而无需向量的开销。

The best idea if you are using a collection in place of a cache is to use a CachedRowSet instead of a ResultSet. CachedRowSet is a Subinterface of ResultSet, but the data is already cached. This is far simpler than to write all the data into an ArrayList.
CachedRowSets can also be queried themselves.

CachedRowSet rs;
.......................
.......................
Integer id;
String name;

while (rs.next())
{               
     if (rs.getInt("id") == 13)
     {
          id   = rs.getInt("id");
          name = rs.getString("name")); 
     }              
}

So you just call the CachedRowSet whenever you need the info. It's almost as good as sliced bread. :)

EDIT:
There are no set methods for ResultSet, while there are Update methods. The problem with using the Update method's for the purpose of rebuilding a ResultSet is that it requires selecting a Row to update. Once the ResultSet has freed itself, all rows are set to null. A null reference cannot be called. A List of Lists mimics a ResultSet itself, or more correctly, an array of arrays mimic a ResultSet.

While Vectors are thread safe, there is a huge overhead attached to them. Use the ArrayList instead. As each nested List is created and placed into the outer nest List, insert it in this manner.

nest.add(Collections.unmodifiableList(nested));

After all of the nested Lists are inserted, return the nest List as an umodifiableList as well. This will give you a thread-safe collection without the overhead of the vectors.

得不到的就毁灭 2024-07-19 06:22:55

请查看此页面。 尝试查看 SimpleResultSet 类是否适合您的需求。

如果您组合 其源 到一组独立的类中,它应该可以解决问题。

Take a look at this page. Try to see if the SimpleResultSet class is fine for your needs.

If you combine its source into a standalone set of classes, it should do the trick.

寂寞花火° 2024-07-19 06:22:55

据我所知,您的代码可能是这样的:

List collection = new ArrayList();
collection.add(" A collection in some order");

List cache = new ArrayList();
cache.add(collection); ...

现在,当您检索时,我认为您将按顺序获得集合,因为您已经使用了 List。

如果这不是您所期望的,请发表评论。

From what I could get, your code may be like this:

List collection = new ArrayList();
collection.add(" A collection in some order");

List cache = new ArrayList();
cache.add(collection); ...

Now when you retrieve I think you'll get your collection in order, since you have used List.

If this is not what you were expecting, do comment.

旧话新听 2024-07-19 06:22:55

我建议您使用 CachedRowSet。 请参阅 http://www.onjava.com/pub /a/onjava/2004/06/23/cachedrowset.html这篇文章了解有关CachedRowSet的更多信息。 创建此 CachedRowSet 后,您可以断开与数据库的连接,对缓存数据进行一些更改,甚至可以打开数据库连接并将更改提交回数据库。

I will advise you to use CachedRowSet. Refer http://www.onjava.com/pub/a/onjava/2004/06/23/cachedrowset.html this article to know more about CachedRowSet. Once you create this CachedRowSet, you can disconnect from the database, make some changes to the cached data and letter can even open the DB connection and commit the changes back to the Database.

雅心素梦 2024-07-19 06:22:55

您应该考虑的另一个选择是重构代码以接受 Collection 而不是 ResultSet。

我假设您将该 ResultSet 传递给迭代它的方法。 您不妨更改方法来迭代 ArrayList...

Another option you should consider is just to refactor your code to accept a Collection instead of a ResultSet.

I'm assuming you pass that ResultSet to a method that iterates over it. You might as well change the method to iterate over an ArrayList...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文