2012-03-26 29 views
0

我遇到了試圖在控制檯中顯示數據的問題..我只是希望它顯示有組織或接近有組織。這就是我如何把數據如何格式化表格以便在使用java從mysql中提取數據時更好地顯示

// Loop through the result set 
     while(rs.next()) 
     { 
      System.out.print(rs.getInt(1)) ; 
      System.out.print(" | "); 
      System.out.print(rs.getString(2)); 
      System.out.print(" | "); 
      System.out.print(rs.getString(3)); 
     } 

但這個數據顯示全國各地像這樣的地方..

123 Blaah St | Apt 2A | FL 
    Fort | null | FL 
    5601 N Dixie Hwy | null | FL 

是否有一個很好的提醒,以這種格式在Java更好嗎?

回答

1

System.out.format看看。

實施例:

String format = "|%1$-10s|%2$-10s|%3$-20s|\n"; 
System.out.format(format, "Short", "Other", "Field"); 
System.out.format(format, "Very long", "", "Text"); 
System.out.format(format, "Long too!", ":)", "Bye"); 
0

在while循環的最後一行使用System.out.println而不是System.out.print,因此您會爲每條記錄獲取新行。

0

同時加入,檢查輸入字符串的最大尺寸,然後相對於添加填充到每對或列之間的字符串的該尺寸

相關問題