如何修复Columnsize

时间:2018-05-23 07:56:38

标签: java sql jdbc resultset

我的结果集有问题。我无法获得固定长度的列。

ResultSetMetaData rsmd = rs.getMetaData();
int cols = rsmd.getColumnCount();
for(int i=1; i<=cols; i++)
    System.out.print(rsmd.getColumnLabel(i)+"\t");
System.out.println("\n--------------------------------------------------------------");
while(rs.next())
{

    for(int i=1; i<=cols; i++)
        System.out.print(rs.getString(i)+"\t");

    System.out.println();
}

表格看起来像这样

http://www.bilder-upload.eu/show.php?file=bf21f6-1527061884.png“表格”

列名称与数据不完全相同。

1 个答案:

答案 0 :(得分:0)

只需使用格式打印(调整到所需长度):

    SetContentView(Resource.Layout.Main);

        ActionBar.Tab tab = ActionBar.NewTab();
        tab.SetText("Animals");
        tab.SetIcon(Resource.Drawable.tab1_icon);
        tab.TabSelected += (sender, args) => {
                               //blalblabla
                           }
        ActionBar.AddTab(tab);

tab = ActionBar.NewTab();
        tab.SetText(Motorcycle);
        tab.SetIcon(Resource.Drawable.tab2_icon);
        tab.TabSelected += (sender, args) => {
                               // blablabla
                           }
        ActionBar.AddTab(tab);

        tab = ActionBar.NewTab();
        tab.SetText(Cars);
        tab.SetIcon(Resource.Drawable.tab2_icon);
        tab.TabSelected += (sender, args) => {
                               // blablabla
                           }
        ActionBar.AddTab(tab);

例如:

ResultSetMetaData rsmd = rs.getMetaData();
int cols = rsmd.getColumnCount();
//format begins after 20 characters.

String format = "%-20s";

for(int i=1; i<=cols; i++)
    System.out.printf(format, rsmd.getColumnLabel(i));
System.out.println("\n--------------------------------------------------------------");
while(rs.next())
{

    for(int i=1; i<=cols; i++)
        System.out.printf(format, rs.getString(i));

    System.out.println();
}

会回来:

enter image description here