1

I insert the values of an entire row of jtable in oracle database, based on the ISBN inserted into txtField.

   Connection conn = Connessione.ConnecrDb();
   Statement stmt = null;
   ResultSet emps = null;


    try{

        String sql= "INSERT INTO PROGETTO.CARRELLO (ISBN, DISPONIBILITA, TITOLO, CASA_EDITRICE, CODICE_AUTORE, GENERE, PREZZO)"
                + "VALUES (?,?,?,?,?,?,?)  where isbn=?";

        pst=(OraclePreparedStatement) conn.prepareStatement(sql);

        pst.setString (1, agg_libro_carr.getText());    

     pst.execute();

      JOptionPane.showMessageDialog(null, "BOOK SAVED");

      }

    catch (Exception e)
     {
      JOptionPane.showMessageDialog(null,e);


    }        

But return error IN/OUT index 2 etc...

thank you

EDIT:

private void button_carrelloActionPerformed(java.awt.event.ActionEvent evt) {                                                

   Connection conn = Connessione.ConnecrDb();
   Statement stmt = null;
   ResultSet emps = null;


    try{

        String sql= "INSERT INTO PROGETTO.CARRELLO (ISBN, DISPONIBILITA, TITOLO, CASA_EDITRICE, CODICE_AUTORE, GENERE, PREZZO)"
                + "VALUES (?,?,?,?,?,?,?) where isbn=?";

        pst=(OraclePreparedStatement) conn.prepareStatement(sql);

        pst.setString (1, agg_libro_carr.getText()); 
        pst.setString (2, "DISPONIBILITA");  
        pst.setString (3, "TITOLO");  
        pst.setString (4, "CASA_EDITRICE");  
        pst.setString (5, "CODICE_AUTORE");  
        pst.setString (6, "GENERE");  
        pst.setString (7, "PREZZO");
        pst.setString (8, agg_libro_carr.getText());

     pst.execute();

      JOptionPane.showMessageDialog(null, "BOOK SAVED");

      }

    catch (Exception e)
     {
      JOptionPane.showMessageDialog(null,e);


    }    

I need that, having done the research (which works and fills the jtable). depending on the isbn inserted into txtField must insert into the table cart (oracle), the entire line that appears in the jtable. (jtable values are taken from the table "book" (oracle).

1 Answer 1

4

you set only one String in your PreparedStatement:

String sql= "INSERT INTO PROGETTO.CARRELLO (ISBN, DISPONIBILITA, TITOLO, CASA_EDITRICE, CODICE_AUTORE, GENERE, PREZZO)"
                + "VALUES (?,?,?,?,?,?,?)  where isbn=?";

        pst=(OraclePreparedStatement) conn.prepareStatement(sql);

        pst.setString (1, agg_libro_carr.getText()); 

what is with the other question marks?

 "VALUES (?,/*?,?,?,?,?,? The ones are not set!*/ )  where isbn=/*? That one is not set!*/";

as result of that you have an invalid Statement.

you need something like that:

pst.setString (1, agg_libro_carr.getText());  
pst.setString (2, "VALUE_OF_DISPONIBILITA");  
pst.setString (3, "VALUE_OF_TITOLO");  
pst.setString (4, "CASA_EDITRICE");  
pst.setString (5, "CODICE_AUTORE");  
pst.setString (6, "GENERE");  
pst.setString (7, "PREZZO");
pst.setString (8, agg_libro_carr.getText());

By the way(if ISBN is your primary key)... It makes no sence to insert something if the book with that ISBN already exists. In that case you should update the entry.

Sign up to request clarification or add additional context in comments.

1 Comment

yes I Know but the string to insert in database are 7. Gli altri punti interrogativi, sono per gli altri valori, che non vengono presi da txtfield ma dovrebbero essere presi in automatico dalla tabella book (nel database) e copiati nella tabella cart (nel database). I tried String data=jTable1.getValueAt(2).toString(); (etc) but don't work or String isbn=isbn.getText(); String disponibilita= disponibilita.getText(); String titolo= titolo.getText(); thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.