System.out.println()从数据库变成表

Lon*_*ent 1 java user-interface ms-access

目前,我将数据从MS Access数据库和java应用程序I system.out.print中提取到控制台.我使用/ t来分隔数据,但它显得很笨拙.我想将它放在某种桌子上以获得更令人愉悦的外观.任何帮助都会非常感谢.

          while(rs.next())
      {
          System.out.print(rs.getInt("season_number")+"\t");
          System.out.print(rs.getInt("season_episode_number")+"\t");
          System.out.print(rs.getInt("series_episode_number")+"\t");
          System.out.print(rs.getString("title")+"\t");
          System.out.print(rs.getString("directed_by")+"\t");
          System.out.print(rs.getString("written_by")+"\t");
          System.out.print(rs.getDate("origional_air_date")+"\t");
          System.out.println(rs.getFloat("viewing_figures")+"\t");

      }
   }
Run Code Online (Sandbox Code Playgroud)

Cos*_*lis 7

为了理顺你的输出,最好的解决方案就是使用System.out.printf().

由于printf与C'printf非常相似,因此您可以使用控制字符来格式化输出.像这样:

System.out.printf( "%-3.2d %-20.20s\n", rs.getInt("season_number"), rs.getString("title") );
Run Code Online (Sandbox Code Playgroud)

检查此链接.


Ham*_*run 6

我是 Java 新手,作为我学习 Java 以及使用 IDE、Javadoc、版本控制、许可、开源等努力的一部分。我编写了一个简单的实用程序类,它可以打印DBTablePrinter给定表中的行或java.sql.ResultSet打印到标准输出,格式化为看起来像一个带有边框的行和列的表格。我希望它对某人有用。

以下是 GitHub 上代码存储库的链接:https: //github.com/htorun/dbtableprinter

这是基本用法:

// Create a connection to the database
Connection conn = DriverManager.getConnection(url, username, password);

// Just pass the connection and the table name to printTable()
DBTablePrinter.printTable(conn, "employees");
Run Code Online (Sandbox Code Playgroud)

它应该打印如下内容:

Printing 10 rows from table(s) EMPLOYEES
+--------+------------+------------+-----------+--------+-------------+
| EMP_NO | BIRTH_DATE | FIRST_NAME | LAST_NAME | GENDER |  HIRE_DATE  |
+--------+------------+------------+-----------+--------+-------------+
|  10001 | 1953-09-02 | Georgi     | Facello   | M      |  1986-06-26 |
+--------+------------+------------+-----------+--------+-------------+
|  10002 | 1964-06-02 | Bezalel    | Simmel    | F      |  1985-11-21 |
+--------+------------+------------+-----------+--------+-------------+
    .
    .
Run Code Online (Sandbox Code Playgroud)

我感谢所有对这个问题以及 stackoverflow.com 做出贡献的人。您的回答对我帮助很大,并且仍然有帮助。