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

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

Connection connection = null;
Statement statement = null;
ResultSet resultset = null;
// Connect to database
connection = getConnection();
StringBuffer str_buf = new StringBuffer();
String fileName = "student/info.txt";
FileWriter fw = new FileWriter(fileName);
statement = connection.createStatement();
resultset = statement.executeQuery("SELECT * FROM
STUDENT");
String data_row = "";
fw.write("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() + "\"";
fw.write(data_row);
fw.close();

Similar Messages

  • How to display records from a query into non-database field

    Hi
    I a have a problem:
    I have a query with many tables and 6 column(select a,b,c,d,e,f from x,y,z,t,s,g where conditions) and I use 3 parameters.
    I create 3 parameters :datai,:dataf and :partener and a button with a trigger when button is pressed.
    Then a create a manualy block with six field non-database a1,b1,c1,d1,e1,f1.
    Now I want to display all the records from my query into a1,b1,c1,d1,e1,f1 where a1=a,b1=b,etc. and all the records (if I have 20 record, it will display 20 records in non-database field) when I press the button.
    How I made:
    I create a cursor with query then
    begin open cursor
    loop
    fetch cursor into :a1,:b1,:c1,:d1,:e1,:f1;
    end loop;
    close cursor;
    end;
    It display one record in a1,b1,c1 only and it have to display 100 records and are date for all the fields.
    Can somebody help me in this problem?
    Thanks.
    Edited by: 928437 on Oct 1, 2012 2:55 AM

    Creating a view, and querying that into a database block is an excellent solution.
    To use the non-database block:
    You're missing the all-important Next_Record; command.
    <pre> Begin
    Go_block('X'); -- block X is the non-database block
    Clear_Block(No_Validate);
    open cursor X1;
    loop
    If :System.Record_status != 'NEW' then
    Next_Record;
    End if;
    fetch X1 into :a1,:b1,:c1,:d1,:e1,:f1;
    Exit when X1%NOTFOUND;
    end loop;
    close X1;
    end;</pre>

  • How to fetch records from the database into a combo box?

    Hi:
    I&acute;m really new with ABLBPM and I&acute;m trying to fetch records from the database to display them into a combo box as valid values for a presentation but I&acute;m using a dynamic method with this code:
    <em>for each row in SELECT campo1, campo2 from TABLE</em>
    <em>do</em>
    <em>solicitudes[] = [row.campo1, row.campo2]</em>
    <em>end</em>
    <em>return solicitudes
    </em>And the debugger says that SQL instructions can be used only in fuctions and procedures that are executed on the server.
    Do you know another way to do it?
    P.D. Sorry for my terrible english
    Greetings

    Hi Steve,
    Thank you, your idea is perfect, but when I try to run the screenflow where the combo should be filled I get this error:
    fuego.lang.ComponentExecutionException: No se ha podido ejecutar correctamente la tarea.
    Motivo: 'java.lang.NullPointerException'.
         at fuego.web.execution.InteractiveExecution.setExecutionError(InteractiveExecution.java:307)
         at fuego.web.execution.InteractiveExecution.process(InteractiveExecution.java:166)
         at fuego.web.execution.impl.WebInteractiveExecution.process(WebInteractiveExecution.java:54)
         at fuego.webdebugger.servlet.DebuggerServlet.redirect(DebuggerServlet.java:136)
         at fuego.webdebugger.servlet.DebuggerServlet.doPost(DebuggerServlet.java:85)
         at fuego.webdebugger.servlet.DebuggerServlet.doGet(DebuggerServlet.java:66)
         at javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
         at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
         at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
         at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
         at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:672)
         at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:463)
         at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:398)
         at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:301)
         at fuego.web.execution.servlet.ServletExternalContext.forwardInternal(ServletExternalContext.java:197)
         at fuego.web.execution.servlet.ServletExternalContext.processAction(ServletExternalContext.java:110)
         at fuego.webdebugger.servlet.DebuggerExecution.dispatchComponentExecution(DebuggerExecution.java:64)
         at fuego.web.execution.InteractiveExecution.invokePrepare(InteractiveExecution.java:351)
         at fuego.web.execution.InteractiveExecution.process(InteractiveExecution.java:192)
         at fuego.web.execution.impl.WebInteractiveExecution.process(WebInteractiveExecution.java:54)
         at fuego.web.execution.InteractiveExecution.process(InteractiveExecution.java:223)
         at fuego.webdebugger.servlet.DebuggerServlet.doDebug(DebuggerServlet.java:148)
         at fuego.webdebugger.servlet.DebuggerServlet.doPost(DebuggerServlet.java:82)
         at fuego.webdebugger.servlet.DebuggerServlet.doGet(DebuggerServlet.java:66)
         at javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
         at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
         at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
         at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
    Any ideas??
    Thanks and greetings

  • How to Print Bytes[] from a resultset in a text file? Thanks a Lot!!

    Hi all I'm having a prb in printing the Byte[] from a Resultset in a textfile . Every time I exceute this program different values is printing in the text file and in binary format like:
    [B@29c3,[B@347a,[B@3f2f,[B@49e6,[B@549d
    and I'm supposed to get the out put as :
    0000690D,00002107,00009F16,00003008,0000ED0F
    Here is the sample code!
    public static void main(String[] args) {
         try {
    Class.forName(drvName);
    Connection con = DriverManager.getConnection(url, SDR_userName, SDR_passWord);
    Statement stmt = con.createStatement();
    int count1 = 0;
    char c = 'c';
    ResultSet rs1 = stmt.executeQuery(_sqlQuery);
    FileOutputStream fos = new FileOutputStream(inputFileName);
    Writer out = new OutputStreamWriter(fos, "UTF8");
    while (rs1.next()) {
    byte[] _id = rs1.getBytes(1);
    String _name = rs1.getString(2);
    out.write("_id=="+id+"==name=="+_name);
    int len = sales_account_id.length;
    System.out.println("Length==" + len);
    String s = new String(_id, 0, len, "UTF8");
    char c1[] = s.toCharArray();
    for (int i = 0; i < s.length(); i++) {
    System.out.print("s1" + c1);
    System.out.println("_Id" + s.toString());
    try {
    for (int i = 0; i < len; i++) {
    c = (char) _id;
    System.out.println("_Id" + c);
    catch (Exception statemets...){}
    Thanks a lot in advance!!

    out.write("_id=="+id+"==name=="+_name);
    in this line you are printing the array _id. when you do that ( print array ) you get the address of that array. so the output is expected.
    what you can do is create a new String from the byte array and write that string to the file.
    String temp = new String( _id );
    out.write("_id=="+temp+"==_name=="+_name);
    hope i got your problem right

  • How to write data from planning folder into a planning cube in BPS

    Hi All,
    I have an issue in writing data from planning folder to planning cube.
    I updated the excel sheet in planning folder in UPSPL tcode.
    After clicking on save button in the excel sheet the data did not get updated into the cube.
    I set the real time cube to planning mode.
    Please let me know if there are any settings to save the data to cube or how can it be done.
    Thanks in advance.
    Regards,
    Lavanya.

    Hi Lavanya,
    What do you mean by "the data did not get updated into the cube" ?
    Don't you find a yellow request in the cube administration transaction ? What happened if you set it to green ?
    Regards,
    Fred

  • Insert records from report program into R3 table

    Hi
    I wanted to insert records from report program into R3 table.
    here is my code
    data : itab type standard table of zemp initial size 10 with header line.
    itab-EMPNO = '012'.
    itab-ENAME = 'XXXX'.
    itab-JOB = 'XXXX'.
    APPEND itab.
    insert ztable from table itab.
    but i am getting the following error
    the type of the data base table and work area/internal table "ITAB" are no unicode-converible.
    how can I insert records from report program into R3 table
    should I have to write move corresponding
    pls guide
    thanks
    manian

    Hi,
    itab-EMPNO = '012'.
    itab-ENAME = 'XXXX'.
    itab-JOB = 'XXXX'.
    APPEND itab.
    insert ztable from table itab.
    Do one thing
    Data : itab type table of ztable with header line.
    itab-EMPNO = '012'.
    itab-ENAME = 'XXXX'.
    itab-JOB = 'XXXX'.
    APPEND itab.
    insert ztable from table itab.
    error will resolve, then try to make structure similar to ZTABLE

  • How to read and write a string into a txt.file

    Hi, I am now using BEA Workshop for Weblogic Platform version10. I am using J2EE is my programming language. The problem I encounter is as the above title; how to read and write a string into a txt.file with a specific root directory? Do you have any sample codes to reference?
    I hope someone can answer my question as soon as possible
    Thank you very much.

    Accessing the file system directly from a web app is a bad idea for several reasons. See http://weblogs.java.net/blog/simongbrown/archive/2003/10/file_access_in.html for a great discussion of the topic.
    On Weblogic there seems to be two ways to access files. First, use a File T3 connector from the console. Second, use java.net.URL with the file: protocol. The T3File object has been deprecated and suggests:
    Deprecated in WebLogic Server 6.1. Use java.net.URL.openConnection() instead.
    Edited by: m0smith on Mar 12, 2008 5:18 PM

  • In Mail 7.3 on my Retina MacBook Pro on OS 10.9.4, how can I type from the keyboard into a mail message using the Apple Symbols font?

    In Mail 7.3 on my Retina MacBook Pro on OS 10.9.4, how can I type from the keyboard into a mail message using the Apple Symbols font?
    When I select the Apple Symbols font from the list accessed from under Format/"Show fonts"/collection-all fonts/Apple Symbol, and then continue typing, I get normal letters, if in a slightly different font.
    How can I type using the Apple Symbol font?  I want to use that font to include keyboard and cursor selections of actual screen symbols to help write easily understood explicit computer lesson emails to my 92 year-old mom from a few hundred miles away.
    Steve

    In Mail 7.3 on my Retina MacBook Pro on OS 10.9.4, how can I type from the keyboard into a mail message using the Apple Symbols font?
    When I select the Apple Symbols font from the list accessed from under Format/"Show fonts"/collection-all fonts/Apple Symbol, and then continue typing, I get normal letters, if in a slightly different font.
    How can I type using the Apple Symbol font?  I want to use that font to include keyboard and cursor selections of actual screen symbols to help write easily understood explicit computer lesson emails to my 92 year-old mom from a few hundred miles away.
    Steve

  • How to extract data from info cube into an internal table using ABAP code

    HI
    Can Anyone plz suggest me
    How to extract data from info cube into an internal table using ABAP code like BAPI's or function modules.
    Thankx in advance
    regds
    AJAY

    HI Dinesh,
    Thankq for ur reply
    but i ahve already tried to use the function module.
    When I try to Use the function module RSDRI_INFOPOV_READ
    I get an information message "ERROR GENERATION TEST FRAME".
    can U plz tell me what could be the problem
    Bye
    AJAY

  • How to update Records from Internal table to u2018Zu2019 table?

    Hi Friends,
    How to update Records from Internal table to u2018Zu2019 table.
    I have records in Internal table , that records want to update on u2018Zmarau2019 Table.
    ( my internal table & u2018 Zu2019 table structures are same.)
    Thanking you.
    Regards,
    Subash

    Hi,
    loop at internal table.
    modify <Z- table > from values < internal table Workarea>.
    if sy-subrc = 0.
      COMMIT work.
    else.
      ROLLBACK waork.
    endif.
    endloop.
    or
    UPDATE <Z- table > from table < internal table Workarea>.
    if sy-subrc = 0.
      COMMIT work.
    else.
      ROLLBACK waork.
    endif.
    Prabhudas

  • HT4906 how to import photos from photo stream into an event?

    How to import photos from photo stream into an event?

    How to import photos from photo stream into an event?
    In iPhoto 9.4.3 the following works:
    If you have not auto imported the photos from the Photo Stream, as Larry suugests, select the photos that you want to import in the Photo Stream, and drag them to the "Events" view in the Source List. They will be imported as new events, auto split with respect to your preferences.
    Regards
    Léonie

  • How to write exception information into a txt file??

    i got a program which generate lots of exception information. its hard to see from the screen, is there any way to make the input into some txt file?
    when i use
    try
    }catch(Exception ex)
    // to do something here and make the output into a txt file?
    }

    depending upon the version of jdk being used one of the options will be to fetch the StackTraceElement array and writing each of the element using the standard file io operation
                StackTraceElement[] elements = ex.getStackTrace();
                int noOfElements = elements.length;
                for (int i=0; i<noOfElements;i++)
                      //fetch the stacktrace element and write to a file.
                }Thanks
    Rahul

  • How can i record from radio to mp3 format?

    How can i record from radio to mp3 format?
    because i want to record a radio program, and save it, how can i do it?

    If you can, the instructions will be in the user manual - iPod Manuals

  • How do I extract multiple tracks from a CD into a single file in Audition CS6?

    I use Audition to edit my Pastor's messages. The message CD is made with five minute tracks. I want to extract the tracks from the CD into a single file for editing and altering the track lengths. I have done this before in Audition CS6, but I haven't been able to find instructions for it. I only found a note stating this is available in AuditionCC.

    I have discovered an answer to my problem. The way to bring multiple tracks into Audition CS6 from a CD is to use the FILE / OPEN APPEND / TO NEW command. This brings the separate tracks into a single file in Audition CD6. Thanks to everyone who commented.

  • How to export from a mysql table into a txt file?

    I an trying to export the result of a select query into a txt file, and I used the command:
    SELECT * INTO OUTFILE "D:\OUTFILE.TXT" FROM USERS;
    It replies: access denied.
    Do I need some special privilege to do the operation? Or is there anything wrong with the command? Can anybody please help me?

    I tried the statement and it worked for me.
    SELECT * INTO OUTFILE "C:\OUTFILE.TXT" FROM `certificationtype`
    If I did it on the drive MySQL was installed on (C:) and the file ended up in the directory C:\mysql\data.
    SELECT * INTO OUTFILE "D:\OUTFILE.TXT" FROM `certificationtype`
    If I did it to drive D: it ended up in the root.
    Note the command will fail if the output file exists.
    Do you have rights to the root of d:?
    rykk

Maybe you are looking for

  • MS word .doc or .docx attachments won't send or receive in Thunderbird

    I'm running Mac OS X 10.9.5 and Thunderbird 31.5.0. When I attach a .doc or .docx file in Thunderbird to an outgoing message, the attachment disappears. The outgoing message sends as normal, but when I go into my sent folder, there's no attachment, a

  • Import-Pssession is not importing cmdlets when used in a custom module

    I have a PowerShell script/function that works great when I use it in my PowerShell profile or manually copy/paste the function in the PowerShell window. I'm trying to make the function accessible to other members of my team as a module. I want to ha

  • DW 4 not seeing GoLive .site file!

    I have several sites created in GoLive 7. I downloaded the GoLive 9 free trial, brought them over into GL9. Then I did the "Convert Site to New Structure" command as I have seen suggested. Downloaded the GoLive to DW migration extension and installed

  • 802.1x Wireless Implementation with NPS - Guest computers access

    Hi guys, I have a 802.1x network using NPS services in Windows server 2012 that I am testing right now with Windows 7 machines. Everything seems to be fine with corporate computer (connection and authentication are good). But I have an issue with gue

  • IPhone + international use

    Hello, I am wondering if it is possible to use the iphone in europe with european carriers. I live in canada but goto school in europe so my handset right now is unlocked so I just switch sim cards as I move between Europe and North America. I realiz