Java SQL输出格式表结果

发布于 2024-12-06 09:37:44 字数 627 浏览 5 评论 0原文

我一切正常,它打印了带有标题(列名称)和值的正确结果。然而,我想将简单且无组织的结果更改为非常好的输出表格式,就像带有查询中的虚线的 MySql 表结果一样。我该怎么做?

Statement statement = conn.createStatement();
ResultSet rs = statement.executeQuery("SELECT * FROM quarter");
ResultSetMetaData rsMetaData = rs.getMetaData();
int numberOfColumns = rsMetaData.getColumnCount();
for(int i = 1; i <= numberOfColumns; i++)
{
    System.out.print(rsMetaData.getColumnLabel(i) + " ");
}
System.out.println();
while (rs.next())
{
    for(int e = 1; e <= rsMetaData.getColumnCount(); e++)
    {
        System.out.print(rs.getString(e) + " ");
    }
    System.out.println();
}

I got everything working and it prints the right result with header (column name) and value. However I wanted to change a plain and unorganized result into a really nice output table format just like MySql table result with dash lines from query. How do I do that?

Statement statement = conn.createStatement();
ResultSet rs = statement.executeQuery("SELECT * FROM quarter");
ResultSetMetaData rsMetaData = rs.getMetaData();
int numberOfColumns = rsMetaData.getColumnCount();
for(int i = 1; i <= numberOfColumns; i++)
{
    System.out.print(rsMetaData.getColumnLabel(i) + " ");
}
System.out.println();
while (rs.next())
{
    for(int e = 1; e <= rsMetaData.getColumnCount(); e++)
    {
        System.out.print(rs.getString(e) + " ");
    }
    System.out.println();
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

薄情伤 2024-12-13 09:37:44

您可以将 String.format() 方法与适当的 格式字符串

例如,

StringBuilder sb=new StringBuilder();

for(int i = 1; i <= numberOfColumns; i++)
{
    sb.append(String.format("| %-10s",rsMetaData.getColumnLabel(i)));
}

You can use String.format() method with appropriate format string.

For instance,

StringBuilder sb=new StringBuilder();

for(int i = 1; i <= numberOfColumns; i++)
{
    sb.append(String.format("| %-10s",rsMetaData.getColumnLabel(i)));
}
等风来 2024-12-13 09:37:44

你可以有这个功能。调用时根据需要更改参数。

public static void printHeader(ResultSet rs, char symbol, int width) throws Exception 
{
    ResultSetMetaData rsmd = rs.getMetaData();
    int nCols = rsmd.getColumnCount();
    for(int i = 1; i <= nCols; ++i)
          System.out.printf("%-10s ", rsmd.getColumnLabel(i));

     System.out.printf("\n");
     for(int i = 0; i < width; ++i)
        System.out.printf("%c", symbol);
}

//call <br>
printHeader(rs,'-',70);

You can have function for that. Change the parameter as need while calling.

public static void printHeader(ResultSet rs, char symbol, int width) throws Exception 
{
    ResultSetMetaData rsmd = rs.getMetaData();
    int nCols = rsmd.getColumnCount();
    for(int i = 1; i <= nCols; ++i)
          System.out.printf("%-10s ", rsmd.getColumnLabel(i));

     System.out.printf("\n");
     for(int i = 0; i < width; ++i)
        System.out.printf("%c", symbol);
}

//call <br>
printHeader(rs,'-',70);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文