0

I am trying to write an entire sql table in .csv format. The table is 6500 lines long, but only 600 or so lines are written in the file. The number of lines written varies a bit every time I run the method

Connection con = Panel.getConnection();
String query = "SELECT * from group3.merged_person";    

PreparedStatement statement = con.prepareStatement(query);

ResultSet srs = statement.executeQuery();

int totalColumns = srs.getMetaData().getColumnCount();
FileWriter writer = new FileWriter("merged_person.csv");

for(int i = 1; i <= totalColumns; i++) {
    writer.write(srs.getMetaData().getColumnLabel(i) + ",");
}

writer.write("\n");
int lines  = 0;

while(srs.next()) {
    lines++;

    for(int j = 1; j <= totalColumns; j++) {
        writer.write(srs.getString(j) + ",");
    }

    writer.write("\n");
}

System.out.println("lines written " + lines);
writer.close();
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

3 Answers3

1

you might consider adding:

writer.flush();

before the

writer.close();

this should force any buffered content to be written to the destination file.

stjohnroe
  • 3,168
  • 1
  • 27
  • 27
  • 1
    Close makes an implicit flush. It doesn't explain why only 10% are written. – Lluis Martinez Oct 09 '13 at 16:50
  • @LluisMartinez, not sure that the interface actually specifies that. – stjohnroe Oct 09 '13 at 18:34
  • 1
    Not the interface but the class implementation does it. – Luiggi Mendoza Oct 09 '13 at 22:58
  • @LluisMartinez : Ok, just checked, and you are right: http://docs.oracle.com/javase/6/docs/api/java/io/OutputStreamWriter.html#close%28%29 . However I was sure that I had seen behaviour contradicting this and digging further, this behaviour was only introduced in 1.6, look at the docs for 1.5 and earlier and this does not apply: http://docs.oracle.com/javase/1.5.0/docs/api/java/io/OutputStreamWriter.html#close%28%29 – stjohnroe Oct 10 '13 at 09:24
0

It doesn't look to me like the problem is in this part of the code. Sounds almost like your connection's dying prematurely. Since you're not closing it in this method, check srs.isClosed() and con.isClosed() at the end of this method and see if they're getting closed somewhere else.

Josh
  • 1,563
  • 11
  • 16
0

Perform a SELECT count(*) from group3.merged_person at the beginning to be sure of how many rows will be read. Maybe other people is modifying that table.

Lluis Martinez
  • 1,963
  • 8
  • 28
  • 42