0

我想知道如何使用 printf 方法为我的列创建标题:

 Statement stmt = conn.createStatement();
    ResultSet rset = stmt.executeQuery("select country_id,country_name,region_id,last_name from countries");

    while (rset.next())
        System.out.printf("%-5s %-25s %5s \n", rset.getString("country_id"),rset.getString("country_name"),rset.getString("region_id"));

    stmt.close();
    conn.close();

输出是这样的,我想要每列上方的标题!我怎样才能做到这一点?

AR    Argentina                     2 
AU    Australia                     3 
BE    Belgium                       1 
BR    Brazil                        2 
CA    Canada                        2 
CH    Switzerland                   1 
CN    China                         3 
DE    Germany                       1 
DK    Denmark                       1 
EG    Egypt                         4 
FR    France                        1 
IL    Israel                        4 
IN    India                         3 
IT    Italy                         1 
JP    Japan                         3 
KW    Kuwait                        4 
ML    Malaysia                      3 
MX    Mexico                        2 
NG    Nigeria                       4 
NL    Netherlands                   1 
SG    Singapore                     3 
UK    United Kingdom                1 
US    United States of America      2 
ZM    Zambia                        4 
ZW    Zimbabwe                      4 
4

5 回答 5

1

只需在循环之前打印它

Statement stmt = conn.createStatement();
ResultSet rset = stmt.executeQuery("select country_id,country_name,region_id,last_name from countries");

System.out.printf("%-5s %-25s %5s \n", "country_id","country_name","region_id");
while (rset.next())

    System.out.printf("%-5s %-25s %5s \n", rset.getString("country_id"),rset.getString("country_name"),rset.getString("region_id"));
stmt.close();
conn.close();
于 2013-11-15T09:24:01.487 回答
0

如果您正在寻找所有列的通用标题,您应该像这样打印它:

System.out.println("Country_ID Country_Name Region_ID");

while在循环之前添加它。

或者,如果您正在寻找在每行列组之前打印相同的标题,您应该尝试以下操作:

while (rset.next())
    {

        System.out.println("Country_ID Country_Name Region_ID");
        System.out.printf("%-5s %-25s %5s \n", rset.getString("country_id"),rset.getString("country_name"),rset.getString("region_id"));
    }
于 2013-11-15T09:27:36.933 回答
0

你只会有另一个System.out.println()声明,就像这样:

Statement stmt = conn.createStatement();
ResultSet rset = stmt.executeQuery("select country_id,country_name,region_id,last_name from countries");
System.out.printf("%-5s %-25s %5s \n", "Country Id", "Name", "Region Id");
while (rset.next())

System.out.printf("%-5s %-25s %5s \n", rset.getString("country_id"),rset.getString("country_name"),rset.getString("region_id"));
stmt.close();
conn.close();
于 2013-11-15T09:24:46.623 回答
0

只需添加以下内容:

Statement stmt = conn.createStatement();
    ResultSet rset = stmt.executeQuery("select country_id,country_name,region_id,last_name from countries");
    System.out.printf("%-5s %-25s %5s \n", "Country Id", "Name", "Region Id");
    while (rset.next())

    System.out.printf("%-5s %-25s %5s \n", rset.getString("country_id"),rset.getString("country_name"),rset.getString("region_id"));
    stmt.close();
    conn.close();
于 2013-11-15T09:28:57.453 回答
0

您可以使用:

Statement stmt = conn.createStatement();

ResultSet rset = stmt.executeQuery("select country_id,country_name,region_id,last_name from countries");

System.out.printf("%-5s %-25s %5s \n", "country_id","country_name","region_id");

System.out.printf("%-5s %-25s %5s \n", "----------","------------","---------");
while (rset.next())
System.out.printf("%-5s %-25s %5s \n", rset.getString("country_id"),rset.getString("country_name"),rset.getString("region_id"));
stmt.close();
conn.close();
于 2013-11-15T09:29:10.583 回答