-1

I am using the following code from the internet to get data from a database:

ResultSet rs = statement.executeQuery();
ResultSetMetaData rsmd = rs.getMetaData();
System.out.println("querying SELECT * FROM XXX");
int columnsNumber = rsmd.getColumnCount();
while (rs.next()) {
    for (int i = 1; i <= columnsNumber; i++) {
        if (i > 1) System.out.print(",  ");
        String columnValue = rs.getString(i);
        System.out.print(columnValue + " " + rsmd.getColumnName(i));
    }
    System.out.println(""); //<-------------
}

Inside the while loop there is the empty println (arrow ^)

If I remove this empty println, the data doesnt show in my console, but if I leave it there it does (I have only one line in my database, so probably it just skips the last line if I have more data).

Why doesnt it show my single line of data if i leave out the empty println???

Thx!

Randy
  • 9,419
  • 5
  • 39
  • 56
  • The reason is just as you say, you have one line in your database. Have you tried to use `System.out.println(columnValue + " " + rsmd.getColumnName(i));` in your loop? Even try to move the `println` out of your while loop. – Pierre Oct 21 '14 at 13:53
  • I totaly missed the print vs println, assuming everyone like me would use that because it gives cleaner console output in most cases. THX! – Randy Oct 21 '14 at 13:58
  • Look at `@JoopEggen`s answer – Pierre Oct 21 '14 at 13:59

1 Answers1

2

The command line System.out is buffered, and only println does a flush(), emptying the buffer to the console.

In the loop you might do:

   if (i > 1) System.out.print(",  ");
   if (i % 5 == 1) System.out.println();
Joop Eggen
  • 107,315
  • 7
  • 83
  • 138