sqlite 要水平显示的Java JTableModel

cxfofazt  于 2022-12-13  发布在  SQLite
关注(0)|答案(1)|浏览(122)

我有一个查询结果集(日期和房间)某种矩阵。
9间客房和日期介于(2022年1月1日至2022年1月31日)。

try {
    String query="select  a.Tarikh AS 'TARIKH',a.Room AS 'ROOM',b.Room as 'BOOKED'"
            + "from     (Select * from Tarikh Cross Join Room) as a\n"
            + "left join Booking as b\n"
            + "on        a.Tarikh = b.Tarikh\n"
            + "and       (a.Room = b.Room)\n"
            + "WHERE        (a.Tarikh BETWEEN '2022-01-01' AND '2022-03-01');";
    PreparedStatement pst=connection.prepareStatement(query);
    ResultSet rs = pst.executeQuery();

    // this line below implement the usage of net.proteanit.sql.DbUtils (import library)    
    table_1.setModel(DbUtils.resultSetToTableModel(rs));
    // ---      

    pst.execute();  
    rs.close();
} catch (Exception e) {
    e.printStackT
}

问题是,如何使JtableModel从这个

Room    Date            Booked
101 2022-01-01  2022-01-01
102 2022-01-01  2022-01-01
103 2022-01-01  2022-01-01
104 2022-01-01  2022-01-01
105 2022-01-01  null
106 2022-01-01  null
107 2022-01-01  null
108 2022-01-01  null
109 2022-01-01  null
101 2022-01-02  null
102 2022-01-02  2022-01-02
103 2022-01-02  null
104 2022-01-02  null
105 2022-01-02  null
106 2022-01-02  null
107 2022-01-02  null
108 2022-01-02  null
109 2022-01-02  null
and so on....

我希望记录是:

0101  0201 0301 0401 0501 and so on....
101      X 
102      X     X
103      X 
104      X
105
106
107
108
109

有人知道吗,请帮忙。TQ

2ul0zpep

2ul0zpep1#

您需要解析ResultSet数据以手动创建TableModel。
我可以从ResultSet数据创建一个TreeMap开始,房间号是键,ArrayList可以用于每个非空的预订日期。
完成后,您就可以创建一个DefaultTableModel,其中包含“x”行和32列。行数将基于TreeMap中的条目数。第一列将是房间号。接下来的31列将是该月的日期。
现在,您需要从TreeMap读取每个键。每个键将表示TableModel中的一行。读取每个键时:
1.在第一列中设置房间号:setValueAt(roomNumber, currentRow, 0)
1.循环遍历ArrayList中的每个预订日期并解析出日期。因此“2022-01-01”将成为第1列。现在您可以使用setValueAt("X", currentRow, column)更新模型中的值
1.递增currentRow
对TreeMap中的所有键重复上述步骤。

相关问题