将矢量行插入JTable?

时间:2017-05-03 03:23:24

标签: java mysql swing vector jtable

我正在尝试将矢量中的项目添加到我的JTable中,我似乎无法正确使用它。 JTable只显示我数据库中的第一行值,它们一次显示在同一列中。将单个列值插入表格后,如何将其分隔?如何一次将多行值放入表中?

connect = DriverManager.getConnection(url,user,pass);
        System.out.println("Connected to database.\n");

        statement = connect.createStatement();
        ResultSet result = statement.executeQuery("SELECT * FROM aboyer_tickets_1");

        //get values for table
        System.out.println("Adding data to table model...\n");

        Vector<Vector<Object>> data = new Vector<Vector<Object>>();
        while (result.next()) {//model
            Vector<Object> row = new Vector<Object>();//row

            for (int columnIndex = 1; columnIndex <= 6; columnIndex++) {
                row.add(result.getObject(columnIndex));  
            }
            System.out.println(row + "\n");
            data.add(row);
            model.addRow(data);
        }

        System.out.println("Done.\n");

控制台输出:     连接到数据库。

Adding data to table model...

[1, 531961, user1, M, PH 109 Computer wont turn on, O]

[2, 502492, user1, H, wifi connectivity issue, O]

[3, 469432, admin, L, mouse replacement - MSV, O]

[4, 140627, user1, H, Lost login for WH 121 computer, O]

Done.

表中的输出(尽我所能绘制):

             1                |  2  |  3  |  4  |  5  |  6  
[1, 531961, user1, M, PH.., O]
[1, 531961, user1, M, PH.., O]
[1, 531961, user1, M, PH.., O]
[1, 531961, user1, M, PH.., O]

1 个答案:

答案 0 :(得分:1)

所以,基于JavaDocs for DefaultTableModel#addRow(Vector)

  

在模型的末尾添加一行。除非指定了rowData,否则新行将包含空值。将生成正在添加的行的通知。

model.addRow(data)应该是model.addRow(row)