在DefaultTableModel的下两列中显示输出

时间:2013-03-16 10:33:51

标签: java swing jtable resultset defaulttablemodel

抱歉,我是Java的新手,我正在尝试从我的数据库中的事务表获取值,其中包含 set_ID Buying_Rate Selling_Rate 即可。我只想在Transaction表中显示 Buying_rate Selling_rate 。我希望将交易表中的数据显示为列销售率列买入率。这是我的尝试

 DefaultTableModel model2 = (DefaultTableModel) provider.p1.getModel();

        try {
          Connection conn = DriverManager.getConnection("jdbc:mysql://remote-mysq3003.servage.net:3306/alpha?zeroDateTimeBehavior=convertToNull", "username", "password");

            Statement st = conn.createStatement();
            Statement st_cus = conn.createStatement();
            String query = "Select *from currency";
            ResultSet rs = st.executeQuery(query);
            String fx_rates = "Select Buying_Rate, Selling_Rate from Transaction";
            ResultSet rsx = st.executeQuery(fx_rates);
            while (rs.next()) {
                String d1 = rs.getString("currency_code");
                String d2 = rs.getString("Rate");
                String d3 = rsx.getString("Buying_Rate");
                String d4 = rsx.getString("Selling_Rate ");
                model2.addRow(new Object[]{d1, d2});
                model2.addColumn(new Object[]{d3, d4});
            }
            rs.close();
            rsx.close();
            st.close();
            st_cus.close();
            conn.close();
        }
        catch (Exception e) {
            JOptionPane.showMessageDialog(this, "erro in Database");
         }

enter image description here

1 个答案:

答案 0 :(得分:0)

我需要插入&&在我的while循环中使用 model2.addRow(new Object [] {d1,d2,d3,d4}); dispose add column。

try {
          Connection conn = DriverManager.getConnection("jdbc:mysql://remote-mysq3003.servage.net:3306/alpha?zeroDateTimeBehavior=convertToNull", "username", "password");

            Statement st = conn.createStatement();
            Statement stx = conn.createStatement();
            String query = "Select currency_code,Rate from currency";
            String queryx = "Select Buying_Rate, Selling_Rate from Transaction";
            ResultSet rs = st.executeQuery(query);
            ResultSet rsx = stx.executeQuery(queryx);
            while (rs.next()&& rsx.next()) {
                String d1 = rs.getString("currency_code");
                String d2 = rs.getString("Rate");
                String d3 = rsx.getString("Selling_Rate");
                String d4 = rsx.getString("Buying_Rate");
                model2.addRow(new Object[]{d1, d2,d3,d4});

            }               
            rsx.close();
            rs.close();
            stx.close();
            st.close();
            conn.close();
        }
        catch (Exception e) {
            JOptionPane.showMessageDialog(this, "erro in Database");
         }

enter image description here

谢谢你们