Sql 输出到一个 Joptionpane 的查询

Sql query that outputs to ONE Joptionpane

字符串 sql = "SELECT TOP 10 id, Trailer, Block, Location, Day, SetTime, Comment FROM TrailerLocation" + "按编号顺序排序";

   rs = st.executeQuery(sql);

   while(rs.next()){
        //Retrieve by column name
            int id  = rs.getInt("id");
        String trailer = rs.getString("Trailer");
        String block = rs.getString("Block");
        String location = rs.getString("Location");
        String date = rs.getString("Day");
        String comment = rs.getString("Comment");

        //Display values
        JOptionPane.showMessageDialog(null,
              "ID: " + id
            + ", Trailer: " + trailer 
            + ", Block: " + block 
            + ", Location: " + location 
            + ", Date & Time: " + date 
            + ", Comment: " + comment);


    }

我希望Joptionpane只显示一次所有数据而不是十次。

您可以在 while 循环之后移动 JOptionPane.showMessageDialog(null, ...)。 并且,在 while 循环中,将您想要的信息添加到一个唯一的字符串中,您将在 JOptionPane 中显示该字符串。

rs = st.executeQuery(sql);
StringBuilder str = new StringBuilder();
while(rs.next()){

    //Retrieve by column name

    str.append("ID: " + rs.getInt("id"));
    str.append(", Trailer: " + rs.getString("Trailer"));
    str.append(", Block: " + rs.getString("Block"));
    str.append(", Location: " + rs.getString("Location"));
    str.append(", Date: " + rs.getString("Day"));
    str.append(", Comment: " + rs.getString("Comment"));

    //new line
    str.append("\n");
}

JOptionPane.showMessageDialog(null,str.toString());