0

I want to print the query table results on a CSV file.

I tried using this code:

PrintWriter csvWriter = new PrintWriter(new File(path + "SqlClient_" + date + ".csv")) ;
ResultSetMetaData meta = rs.getMetaData() ; 
int numberOfColumns = meta.getColumnCount() ; 
String dataHeaders = "\"" + meta.getColumnName(1) + "\"" ; 
for (int i = 2 ; i < numberOfColumns + 1 ; i ++ ) { 
       dataHeaders += ",\"" + meta.getColumnName(i) + "\"" ;
    }
    csvWriter.println(dataHeaders) ;
    while (rs.next()) {
        String row = "\"" + rs.getString(1) + "\""  ; 
        for (int i = 2 ; i < numberOfColumns + 1 ; i ++ ) {
             row += ",\"" + rs.getString(i) + "\"" ;
        }
        csvWriter.println(row) ;
    }
    csvWriter.close();

This is what i obtain:

title1","title2","title3"
"date1","date2","date3"
....

This is write inside a single column, instead i want to write on different column, something like:

title1 | title2 | title3 
data1| data2 | data3 |

There is a special command to split the string into different columns?

4
  • If I understand you correctly, you want the columns separated by the character | instead of ","? Commented Dec 12, 2016 at 14:10
  • Ehm no xD xD The character | means that i's a different column. Commented Dec 12, 2016 at 14:11
  • So if I understand you, your csv file will look like: Title1 (one column), Title 2(another column), etc? Commented Dec 12, 2016 at 14:12
  • Yeah, that's correct. Commented Dec 12, 2016 at 14:14

2 Answers 2

1

why don't you use a StringBuilder for this. Lets say you obtain a result set with a list of type YourClass from database call. Call the following utility and pass writer to it.

Pseudo code:

public void writeToCSV(FileWriter writer){

StringBuilder sb=new StringBuilder();
for (YourClass data: yourresultSet){

sb.append("\"");
sb.append(data.getColumn1);
sb.append"\"";
sb.append(",\"")
sb.append(data.getColumn2);
sb.append("\"");
sb.append('\n'); //new line after the column

writer.write(sb.toString());
sb.delete(0,sb.length());

}

}

And to test your result

public class ReadCSV{

public static void main(String[] args) {

    String csvFile = "your.csv";
    BufferedReader br = null;
    String line = "";
    String cvsSplitBy = ",";

    try {

        br = new BufferedReader(new FileReader(csvFile));
        while ((line = br.readLine()) != null) {

            // use comma as separator
            String[] title= line.split(cvsSplitBy);

            System.out.println("Columns [Column1= " + title[0] + " , Column2=" + title[1] + "]");

        }

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (br != null) {
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}
}
Sign up to request clarification or add additional context in comments.

Comments

0

The CSV files use a delimiter in order to separate the columns, so I'm guessing that your delimiter will be , (comma).

What you should do is print the value of the headers separated by comma and then append a new line separator (\n) in order to create a new column. And the same with the row values.

See this example for further information about that.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.