将结果集数据放入文本文件的最佳方法是什么?
我想将结果集中的所有数据以相同的顺序放入文本文件中。有没有什么方法可以一次获取所有行中的数据并写入文件或必须逐行写入?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为什么要通过Java来做到这一点?许多 DBMS 都提供了开箱即用的此功能。
MySQL 示例:
Why go through Java to do this? Many DBMS provide this feature out of the box.
MySQL example:
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.
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.