Java SQL输出格式表结果
我一切正常,它打印了带有标题(列名称)和值的正确结果。然而,我想将简单且无组织的结果更改为非常好的输出表格式,就像带有查询中的虚线的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以将
String.format()
方法与适当的 格式字符串。例如,
You can use
String.format()
method with appropriate format string.For instance,
你可以有这个功能。调用时根据需要更改参数。
You can have function for that. Change the parameter as need while calling.