将结果集数据放入文本文件的最佳方法是什么?

发布于 2024-08-29 15:06:03 字数 66 浏览 0 评论 0原文

我想将结果集中的所有数据以相同的顺序放入文本文件中。有没有什么方法可以一次获取所有行中的数据并写入文件或必须逐行写入?

I want to put all the data in the resultset into a text file in the same order. Is there any method to get data in all the rows at once and write to a file or have to write it row by row?

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

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

发布评论

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

评论(3

眼角的笑意。 2024-09-05 15:06:03

为什么要通过Java来做到这一点?许多 DBMS 都提供了开箱即用的此功能。

MySQL 示例:

select
   your_first_field,
   your_second_field
from
   your_favorite_table
into
   outfile
      '/path/to/favorite/file.csv'
   fields terminated by ','
   enclosed by '"'
   lines terminated by '\n'

Why go through Java to do this? Many DBMS provide this feature out of the box.

MySQL example:

select
   your_first_field,
   your_second_field
from
   your_favorite_table
into
   outfile
      '/path/to/favorite/file.csv'
   fields terminated by ','
   enclosed by '"'
   lines terminated by '\n'
七分※倦醒 2024-09-05 15:06:03

ResultSet 并不是你想象的那样。它只是对数据库中实际结果集的引用。您无法一次性转换它。您必须逐行迭代它。每当触发选择查询时,生成的结果都会保存在数据库缓存中,jdbc 会为其分配结果集引用,以便更轻松地访问数据。

因此,您的问题的答案是,是的,您需要逐行迭代,并且可能可以使用 CSV 文件来存储您的值。

ResultSet is not how you think it is. Its just a reference to the actual resultset in the database. You cannot convert it in one shot. You will have to iterate it row by row. Whenever a select query is fired, the result that is produced is held in the database cache for which jdbc allocates a resultset reference to make life easier to access data.

So, the answer to your question is, Yes you need to iterate row by row and probably you can use a CSV file to store your values.

生生漫 2024-09-05 15:06:03

1)这个问题的答案可以通过以下方式处理:

无论你得到什么结果集,迭代结果集,当你将数据存储到变量中时,通过文件I/O包将其发送到csv文件

2)如果你有数据库,从那里直接将数据导出到csv文件。

1) Answer to this question can be handled in the following way

What ever the resultset you have got, iterate through the result set and when you are storing the data into the variables, send it to the csv files through the File I/O Package

2) if you have the database, from there directly export the data to a csv files.

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