2015-08-28 92 views
-1

String sql =「SELECT TOP 10 id,Trailer,Block,Location,Day,SetTime,Comment FROM TrailerLocation」 +「ORDER BY id DESC」;SQL查詢輸出到一個Joptionpane

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只顯示一次,所有數據都不是十次。

+0

這個問題無關,與任何SQL。 –

回答

0

您可以移動JOptionPane.showMessageDialog(NULL,...)後while循環。 然後,在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());