My requirement is to fetch almost 0.5 million records from the db2 database using Java.These fetched records I am going to write in text file. Currently I preferred BufferedWriter to do this as below -
Considering performance which includes execution time,faster I/O operations and less memory usage Which option I should go for?.
Test1 - Where I fetched almost 3200 records from database BufferedWriter - takes 9 sec OutputStramWriter - takes 8 sec
But I am confused between those as I have to fetch almost 0.5 million records in single shot where performance is the key factor!!.
try { Statement stmt = new Statement(); Connection con = getConnection(); BufferedWriter bw = getWriter(); String query = "select field1,field2,field3,field4,field5 from table1 WITH UR"; stmt = con.createStatement(); stmt.setFetchSize(10000); ResultSet rs = stmt.executeQuery(query); StringBuffer sb = new StringBuffer(); while (rs.next()) { sb.append(rs.getString(field1)); sb.append(rs.getString(field2)); sb.append(rs.getString(field3)); sb.append(rs.getString(field4)); sb.append(rs.getString(field5)); bw.write(sb.toString()); bw.newLine(); sb.setLength(0); } bw.flush(); } catch(SQLException ex) { ex.printstackTrace(); } catch(Exception ex2) { ex2.printstackTrace(); } finally() { stmt.close(); con.close(); bw.close(); } protected static BufferedWriter/OutputStreamWriter getWriter() { //here I have tow options //1. //FileOutputStream fos = new FileOutputStream(file); //OutputStreamWriter osr= new OutputStreamWriter(fos); // return osr; //2. //BufferedWriter out = new BufferedWriter(new FileWriter(file)); //return out; }