printf方法创建标题

时间:2013-11-15 09:21:02

标签: java printf

我想知道如何使用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 

5 个答案:

答案 0 :(得分: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();

答案 1 :(得分: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();

答案 2 :(得分: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"));
    }

答案 3 :(得分: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();

答案 4 :(得分: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();