输出到ONE Joptionpane的SQL查询

时间:2015-08-28 13:36:46

标签: java sql joptionpane

String sql =" SELECT TOP 10 id,Trailer,Block,Location,Day,SetTime,Comment From TrailerLocation"                       +" ORDER BY id DESC";

   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只显示一次所有数据不是十次。

1 个答案:

答案 0 :(得分:0)

您可以在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());