Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to write records from a resultset into a txt file ???

843841Sep 5 2004 — edited Sep 6 2004
Hi, I am a newbie in java servlet technology. Currently, I am having a problem regarding how to write all my records from within a resultset into a text file.

First, I put all my records into a string variable and then append the string into a stringbuffer. After that, I create a bufferoutputstream and used the write method to write into a text file. Below are the code I used in my program.
Connection connection = null;
Statement statement = null;
ResultSet resultset = null;

// Connect to database
connection = getConnection();
StringBuffer str_buf = new StringBuffer();

statement = connection.createStatement();
resultset = statement.executeQuery("SELECT * FROM STUDENT");

String data_row = "";
str_buf.append("STUDENT_ID,NAME,PHONE,ADDRESS,RESULT");

while (resultset.next()) {
   data_row = "\n";
   data_row += resultset.getLong("STUDENT_ID");
   data_row += ",\"" + resultset.getString("NAME").trim() + "\"";
   data_row += ",\"" + resultset.getString("PHONE").trim() + "\"";
   data_row += ",\"" + resultset.getString("ADDRESS").trim() + "\"";
   data_row += ",\"" + resultset.getString("RESULT").trim() + "\"";
   
   str_buf.append(data_row);
}

BufferedOutputStream buf_out = null;

// Create a folder and write all records into info.txt
String fileName = "student/info.txt";

buf_out = new BufferedOutputStream(new FileOutputStream(fileName));

int str_len	= str_buf.length();

for (int i = 0; i < str_len; i++) {
   buf_out.write(str_buf.charAt(i));
}
So. is this a proper way to write information into a text file ?

The total records are around 150 000. Now, I get an exception which is "XServletError : System.lang.outofmemory". It happen if the total records are more than 60 000, within a while loop when I try to append a string into a stringbuffer.

How should I deal with this kind of situation?

Thanks in advanced and any advice is appreciated. Example is even better to improve understanding.

Thanks
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Oct 4 2004
Added on Sep 5 2004
6 comments
1,844 views