打印结果集不会显示所有行

时间:2020-09-26 06:46:07

标签: java mysql jdbc resultset getstring

我有一些代码行

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"));
            }

在我的数据库中,我有2行符合条件status = 0

执行System.out.print("uid__"+re.getString("uid"));时仅显示一行。我在做什么错了?

1 个答案:

答案 0 :(得分:0)

默认情况下,分配给System.out的{​​{1}}被缓冲,这意味着它将收集字符并在其缓冲区已满或添加了换行符的内容时刷新它们。

使用System.out.print时,没有换行符。为了显示您的打印件,您需要打印更多以便缓冲区已满,或者您需要使用System.out.println("uid__"+re.getString("uid"));(这样每个uid都在自己的行上并自动刷新),或者-在循环之后-添加System.out.println();(将自动刷新)或System.out.flush()(显式刷新)。

相关问题