gpt4 book ai didi

java - printf 方法创建标题

转载 作者:行者123 更新时间:2023-12-01 13:50:30 26 4
gpt4 key购买 nike

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

最佳答案

只需在循环之前打印

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();

关于java - printf 方法创建标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19997520/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com