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:
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?
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?