0

sorry I am newbie to Java, I am trying to get values from Transaction table in my database which contains set_ID , Buying_Rate and Selling_Rate. I only want to show Buying_rate and Selling_rate from the Transaction table. I want to display data from Transaction table on to column Sell Rate and column Buy Rate. here is my attempt

 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

mohamed nur
  • 331
  • 1
  • 15
  • 1
    sorry, don't understand what you are trying to do. Nor does the screenshot looks like it were created by the snippet which seems to add double the number of rows as columns .. – kleopatra Mar 16 '13 at 11:35
  • 1
    1. looks like as you mixing MM rates with FX rates, 2. use standard ISO currecny names, 3. you can to add any number of columns to the XxxTableModel, 4. then is possible to remove any of Columns from JTables view and data are still presented in the XxxTableModel, column(s) is/are removed only from view, 5. seach here for getColumnClass, use Double, 6. for why reason you use Resultset.getString(Column) are data stores in DB as String 7. this ways you can't get SELECT from two tables in database, 8. this is basic Sql query get data from two tables – mKorbel Mar 16 '13 at 12:05

1 Answers1

0

I needed to insert && condition in my while loop and use 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

thank you guys

mohamed nur
  • 331
  • 1
  • 15