title: Writing result set to csv
body: <p>I am trying to write the resultset to a csv file using the following code,</p>
  
  <pre><code>public static String getstaffid(String var_1, Connection connection,
          FileWriter fw) throws SQLException, Exception
  // Create a statement
  {
      String record = null;
      ResultSet rs = null;
      Statement stmt = connection.createStatement();
      boolean empty = true;
      try {
          rs = stmt
                  .executeQuery("select username, firstname, lastname, middlename, street, city, stateorprovince, ziporpostalcode, countryorregion, fax, phone, extension, mobile, pager, title, primaryemail, secondaryemail, officename, description, comments, suspendeddate, userdata, employeeid, createuser, updateuser, createdate, updatedate, employeetype, servicedeskticketnumber, startdate, enddate, manager, businessapprover, technicalapprover, delegate, location, jobcodes, customproperty1, customproperty2, customproperty3, customproperty4, customproperty5, customproperty6, customproperty7, customproperty8, customproperty9, customproperty10 from globalusers where username = '"+ var_1 + "'");
          ResultSetMetaData metaData = rs.getMetaData();
          int columns = metaData.getColumnCount();
  
  
              while (rs.next()) {
              empty = false;
              record = rs.getString(1) + " " + rs.getString(2) + " " + rs.getString(3) + " " + rs.getString(4) + " " + rs.getString(5) + " " + rs.getString(6) + " " + rs.getString(7) + " " + rs.getString(8) + " " + rs.getString(9) + " " + rs.getString(10) + " " + rs.getString(11) + " " + rs.getString(12) + " " + rs.getString(13) + " " + rs.getString(14) + " " + rs.getString(15) + " " + rs.getString(16) + " " + rs.getString(17) + " " + rs.getString(18) + " " + rs.getString(19) + " " + rs.getString(20) + " " + rs.getString(21) + " " + rs.getString(22) + " " + rs.getString(23) + " " + rs.getString(24) + " " + rs.getString(25) + " " + rs.getString(26) + " " + rs.getString(27) + " " + rs.getString(28) + " " + rs.getString(29) + " " + rs.getString(30) + " " + rs.getString(31) + " " + rs.getString(32) + " " + rs.getString(33) + " " + rs.getString(34) + " " + rs.getString(35) + " " + rs.getString(36) + " " + rs.getString(37) + " " + rs.getString(38) + " " + rs.getString(39) + " " + rs.getString(40) + " " + rs.getString(41) + " " + rs.getString(42) + " " + rs.getString(43) + " " + rs.getString(44) + " " + rs.getString(45) + " " + rs.getString(46) + " " + rs.getString(47);
              fw.append(rs.getString(1));
              fw.append(',');
              fw.append(rs.getString(2));
              fw.append(',');
              fw.append(rs.getString(3));
              fw.append('\n'); 
              }
              System.out.println(record);
          }
           finally {
          fw.flush();
          rs.close();
          stmt.close();
      }
  
      return record;
  }
  </code></pre>
  
  <p>This works perfectly fine. But to retrieve a string record, i had to write something like
  record = rs.getString(1) + " " +........+ " " + rs.getString(47). If for like say tomorrow if the query request for more attributes i have to add more rs.getString(48) and so on.</p>
  
  <p>So i wrote a function like the below,</p>
  
  <pre><code>for (int i = 1; i &lt;= columns; i++) {
                   record = rs.getString(i) + " ";
                    }
  </code></pre>
  
  <p>where the value of columns,</p>
  
  <pre><code>ResultSetMetaData metaData = rs.getMetaData();
      int columns = metaData.getColumnCount();
  </code></pre>
  
  <p>But now i just get </p>
  
  <p>null
  null
  .
  .
  .
  null.</p>
  
  <p>Also instead of appending fw.append(rs.getString(1)).....fw.append(rs.getSting(n)), is there a small function i can write to avoid adding more of these append.</p>
  
  <p>Many Thanks</p>