I’m tryin to write a large ResulSet (~1mm rows) to a single file. Is there a preferred/efficient way to do this in Java 1.6?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
That depends on the JDBC driver used. You need to instruct the JDBC driver to not load the entire
ResultSetinto Java’s memory beforehand, but instead load it on a per-row basis on everynext()call. Then, inside theResultSet#next()loop, you need to write the data immediately to the file instead of holding it inListor something.It’s unclear what JDBC driver you’re using, but for example the MySQL JDBC driver could be instructed to serve the resultset on a per-row basis the following way as per the MySQL JDBC driver documentation:
Here’s a concrete kickoff example:
By the way, I’d first check if the DB doesn’t have builtin SQL support for this which can do this much more efficiently. For example, MySQL has a
SELECT INTO OUTFILEconstruct for this.