Results 1 to 2 of 2
  1. #1
    mak556 is offline Member
    Join Date
    Mar 2012
    Posts
    1
    Rep Power
    0

    Smile Writing the contents of Result Set object to a CSV file

    I want to write the contents of the ResultSet to a CSV file. I have used the following code to for the same:

    Java Code:
    ResultSet rs=null;
    query="select * from client where client name= "+custName;
    st=con.prepareStatement(query);
    rs=st.executeQuery(query);
    System.out.println(query);
    while(rs.next())
    {
    int colNo=rs.getMetaData().getColumnCount();
    
    File f=new File(fname);
    FileWriter fstream = new FileWriter(f);
    bufferedWriter = new BufferedWriter(fstream);
    
    for(int i=1;i<(colNo+1);i++)
    {
    bufferedWriter.append(rs.getMetaData().g
    System.out.println(rs.getMetaData().getC
    if(i<colNo)
    {
    bufferedWriter.append(",");
    }
    else
    {
    bufferedWriter.append("\r\n");
    }
    }



    **************************************
    I have used buffered writer to write the file as you can see above. Please tell me if using buffered writer the best way to write in a CSV file. I want to use a very efficient IO code, since this code gets deployed on server. Plz help.
    To write the contents of ResultSet into a file, is there any oter better way?

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: Writing the contents of Result Set object to a CSV file

    You are looking in the wrong place for performance enhancements.
    BufferedWriter is the correct tool for the job, however why are you opening it each time round the 'while' loop?
    Why are you (ab)using a PreparedStatement simply with a concatenated SQL string?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. get Object contents from session variable
    By bekir in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 04-05-2011, 01:13 PM
  2. Replies: 1
    Last Post: 02-27-2010, 01:29 AM
  3. Replies: 9
    Last Post: 10-20-2009, 10:52 AM
  4. Replies: 6
    Last Post: 12-12-2008, 03:35 PM
  5. Replies: 9
    Last Post: 07-10-2008, 04:52 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •