how can i pass an sql query as a parameter to an other sql query in java?

Refresh

April 2019

Views

30 time

0

Im writing a code to manage a cafe so in the frame i added a JTable where everytime i choose an item from a JComboBox it should show in the JTable. for this purpose i added a button and for its action i made sure every time i click it, it stores the item clicked from the JComboBox in a database and then from the database its fetched in the Jtable. But the problem is that the JTable has 2 columns while the choices from the JComboBox have only one. so the other column must be called from an other database which means i have to call an sql query inside of an other one and i don't know how to do that. Ive tried the code below but it gives me this error "You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'produit' at line 1"

b2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {

            try {
                Class.forName("com.mysql.jdbc.Driver");
                Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/café", "root",
                        "ata1");

                Statement statement = connection.createStatement();
                ResultSet resultat = statement.executeQuery ( "SELECT prix FROM commande_existantes WHERE "+ cb.getSelectedItem() + "IN produit" );
                Statement stmt = connection.createStatement();
                stmt.executeUpdate("INSERT INTO commande_existante VALUES ('" + cb.getSelectedItem() + "'," +resultat+ ")");

    } catch (Exception a) {
        a.printStackTrace();
    }
    }
    });

0 answers