在Java中,将数据保存在ResultSet中还是转换为数组更好?

发布于 2024-11-07 14:08:53 字数 194 浏览 6 评论 0原文

我有两个类,一个服务器和一些客户端。服务器类对数据库进行查询并检索一定数量的行。客户端请求这些行的一部分,每一行都是不同的部分。

在服务器类中,最好是在 ResultSet 中维护查询结果并在客户端类发出请求时将其转换为对象数组,还是在请求发生时将所有对象转换为对象数组并部分发送给他?

我认为在 ResultSet 中维护更容易做到和控制。

I have two classes, a server and some clients. The server class make a query to database and retrieve a certain number of rows. The clients request a part of these rows, each one a different part.

In the server class, is better I maintain the result of the query in the ResultSet and transforming in a array of objects when the client class make the request or transforming all in a array of objects and sending him partially when the request occur?

I think that maintain in the ResultSet is more easier to do and control.

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

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

发布评论

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

评论(3

雪落纷纷 2024-11-14 14:08:53

不将其保留在 ResultSet 中的最大原因是它不会将连接释放回池(除非我大错特错了)。因此,您最终会保留本应释放用于其他操作的资源。

The biggest reason to not leave it in the ResultSet is that it doesn't release the connection back to the pool (unless I'm sorely mistaken). As such you end up holding onto resources that should be freed up for other operations.

谎言月老 2024-11-14 14:08:53

实际上我不确定我是否理解你的方法。在具有持久层的典型客户端/服务器场景中,客户端 (1) 调用远程方法,此 (2) 触发数据库操作,服务 (3) 使用请求的对象进行响应。

在您的场景中,服务器看起来像是从数据库读取数据,缓存数据并等待客户端请求块。

如果我做对了,并且服务器正在缓存数据库对象,那么它应该将结果集中的数据转换为对象,我更喜欢使用集合或映射来存储“行”。结果集是一种相当脆弱的数据结构 - 就像某些结果集不支持重新读取集合条目(= 你不能迭代两次)。我总是从集合中获取数据并尽快关闭它。

Actually I'm not sure if I understood your approach. In typical client/server scenarios with persistance layers, a client (1) calls remote method, this (2) triggers a database operation and the service (3) responds with the requested objects.

In your scenario, it looks like the server reads data from a database, caches it and waits for clients to request chunks.

If I got it right, and the server is caching database objects, then it should transform the data from the result set in objects, I'd prefer a collection or a map to store the "rows". A result set is a rather fragile data structure - like some result sets don't support re-reading of set entries (= you can't iterate twice). I'd always take the data from the set and close it as soon as possible.

留蓝 2024-11-14 14:08:53

如果您打算将结果集的内容传递给其他类或方法,那么最好从结果集中取出数据并将其存储在某种形式的数据结构(数组/列表等)中。如果不这样做,您将开始将 java.sql 包的依赖项添加到实际上不需要参与数据库访问的类中。它还使得这些类/方法的单元测试变得更加困难。

如果 ResultSet 仅在非常有限的范围(即单个方法)中使用,那么将其卸载到不同的数据结构中并没有真正的好处。

If you intend to pass the contents of the result set to other classes or methods then it is definitely better to take the data out of the result set and store it in some form of data structure ( array / List etc ). If you do not do this you will start adding dependencies on the java.sql package to classes that really do not need to be involved in database access. It also makes unit testing of those classes / methods much harder.

If the ResultSet is only being used in a very restricted scope ( ie a single method ) then there's no real benefit in unloading it in to a different data structure though.

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