0

抱歉,我是 Java 新手,我正在尝试从我的数据库中包含 set_IDBuying_RateSelling_Rate的Transaction表中获取值。我只想从 Transaction 表中显示Buying_rateSelling_rate 。我想将 Transaction 表中的数据显示到 Sell Rate列 和Buy 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");
         }

在此处输入图像描述

4

1 回答 1

0

我需要在我的 while 循环中插入 && 条件并使用 model2.addRow(new Object[]{d1, d2,d3,d4}); 处置添加列。

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");
         }

在此处输入图像描述

感谢你们

于 2013-03-16T23:48:30.170 回答