2012-03-26 1 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

間文字列のサイズに関してパディングを追加し、入力文字列の最大サイズを確認。

例:

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.printの代わりにSystem.out.printlnを使用すると、各レコードに新しい行が表示されます。

0

添加しながら、次いでSystem.out.formatを見て、各対または列

関連する問題