I have some codes line as
Class.forName("com.mysql.cj.jdbc.Driver");
connect = DriverManager
.getConnection("jdbc:mysql://localhost/"+Contant.DB_NAME+"?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC&"
+ "user="+Contant.MYSQL_USER+"&password="+Contant.MYSQL_PASS);
statement = connect.createStatement();
preparedStatement = connect.prepareStatement("select * from history where status = 0");
re = preparedStatement.executeQuery();
while (re.next()) {
System.out.print("uid__"+re.getString("uid"));
}
In my database, I have 2 rows that meet the condition status = 0
.
When executing System.out.print("uid__"+re.getString("uid"));
it only shows one row. What am I doing wrong?
By default the
PrintWriter
assigned toSystem.out
is buffered, which means it will collect characters and flush them if its buffer is full, or if content with a new-line character was added.As you use
System.out.print
, there is no new-line character. To get your prints to show, you need to print more so the buffer is full, or you need to useSystem.out.println("uid__"+re.getString("uid"));
(so each uid is on its own line, and flushed automatically), or - after the loop - add aSystem.out.println();
(which will flush automatically) orSystem.out.flush()
(an explicit flush).