JTable结合内部Join语句

时间:2014-08-01 08:30:32

标签: java mysql jdbc

我有2个数据库表,我想将它们加入到JTable中。首先,我为2个数据库表创建了2个JTable,但我想尝试将它们放入1个JTable中,并将它们组合在一起。我的第一个数据库表“控制台”由3列(id,名称和成本)组成,我的第二个表“硬件”由4列(id,CPU,内存和HDD)组成。

我的问题在于循环。我写的时候

while (rs.next()) {
    a = rs.getString(1);
    b = rs.getString(2);
    c = rs.getString(3);
    while (rss.next()) {
        aa = rss.getString(2);
        bb = rss.getString(3);
        cc = rss.getString(4);
    }
    model.addRow(new Object[] { a, b, c, aa, bb, cc });
}

我得到了aa,bb,cc相同的值,即第二张桌子的CPU,内存和硬盘。

我写的时候

while (rs.next()) {
    a = rs.getString(1);
    b = rs.getString(2);
    c = rs.getString(3);
    while (rss.next()) {
        aa = rss.getString(2);
        bb = rss.getString(3);
        cc = rss.getString(4);
        model.addRow(new Object[] { a, b, c, aa, bb, cc });
    }
}

我得到a,b,c的相同值,它们是第一张表的id,名称和成本

有什么想法吗?

这是我的完整代码:

model = new DefaultTableModel();
table = new JTable(model);
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
model.fireTableDataChanged();
model.addColumn("id");
model.addColumn("name");
model.addColumn("cost");
model.addColumn("CPU");
model.addColumn("Memory");
model.addColumn("HDD");

try {
    conn = DriverManager.getConnection("jdbc:mysql://localhost/test1?user=me&password=12345");
    stmt = conn.createStatement();
    stmtt = conn.createStatement();
    rs = stmt.executeQuery("SELECT * FROM consoles INNER JOIN hardware ON consoles.id=hardware.id");
    rss = stmtt.executeQuery("SELECT * FROM hardware");
    while (rs.next()) {
        // a = id, b = name, c = cost
        a = rs.getString(1);
        b = rs.getString(2);
        c = rs.getString(3);
        while (rss.next()) {
            // rss.getString(1) is the id,dont need that
            // aa = CPU, bb = Memory, cc = HDD
            aa = rss.getString(2);
            bb = rss.getString(3);
            cc = rss.getString(4);
            model.addRow(new Object[] { a, b, c, aa, bb, cc });
        }

    }

    model.fireTableDataChanged();
    table.setCellSelectionEnabled(true);
    table.setColumnSelectionAllowed(true);
    table.setFillsViewportHeight(true);
    table.setSurrendersFocusOnKeystroke(true);
    table.setBounds(218, 59, 529, 360);
    frame.getContentPane().add(table);
    model.fireTableDataChanged();

    conn.close();
    stmt.close();
    stmtt.close();
    rs.close();
    rss.close();

}
catch (SQLException case1) {
    case1.printStackTrace();
}
catch (Exception case2) {
    case2.printStackTrace();
}

2 个答案:

答案 0 :(得分:2)

您的结果集应返回每个结果所需的所有字段。您不必使用两个查询。选择下面的单个字段

select c.field1, c.field2, h.field1 ... 
from consolas c inner join hardware h 
on c.someField = h.someField

我从未见过有人做过你想做的事情。也许你应该做一些sql教程。

当你同时做这件事时会发生什么,这不是你所期待的。您的第一个查询将返回所有字段。这是加入的重点。您可能无法获得rs.getXxx()的所有结果,因为您可能没有预料到它们。

您可以使用ResultSetMetaData类进行简单测试以获取列数,并打印出所有值。像

这样的东西
ResultSet rs = ...
ResultSetMetaData md = rs.getMetaData();
int columnCount = md.getColumnCount();

然后使用列计数作为最大循环迭代遍历结果集。例如

while(rs.next()) {
    for (int i = 1; i <= columnCount; i++) {
        System.out.print(rs.getObject(i) + ", ");
    }
    System.out.println();
}

<强>更新

这是实现您正在尝试做的事情的通用方法

ResultSet rs = ...
ResultSetMetaData md = rs.getMetaData();
int columnCount = md.getColumnCount();

String[] cols = new String[columnCount];
for (int i = 1; i <= columnCount; i++) {
    col[i - 1] = md.getColumnName(i);
}

DefaultTableModel model = new DefaultTableModel(cols, 0);

while(rs.next()) {
    Object[] row = new Object[columnCount];
    for (int i = 1; i <= columnCount; i++) {
        row[i - 1] = rs.getObject(i);
    }
    model.addRow(row);
}

table.setModel(model);

答案 1 :(得分:0)

<强>已更新

ResultSet rs = stmt.executeQuery("SELECT * FROM consoles INNER JOIN hardware ON consoles.id=hardware.id");
            ResultSetMetaData md = rs.getMetaData();
            int columnCount = md.getColumnCount();
            String[] cols = new String[columnCount];
            for (i=1;i<= columnCount;i++)
            {
                cols[i-1] = md.getColumnName(i);
                // you had missed an "s" here, nothing important
            }

            DefaultTableModel model = new DefaultTableModel(cols,0);

            while (rs.next())
             {
                Object[] row = new Object[columnCount];
                  for (i = 1 ; i <= columnCount ; i++)
                  {
                    row[i-1] = rs.getObject(i);
                  }
                  model.addRow(row);
              }
            table = new JTable(model);// table.setModel(model) created error
            model.fireTableDataChanged();