Note that there are some explanatory texts on larger screens.

plurals
  1. POUpdating AuthorID From ComboBOX value
    primarykey
    data
    text
    <p>I have the following sql tables:</p> <p>Authors:</p> <pre><code>Id Name 2 John Smith </code></pre> <p>Books:</p> <pre><code>Id AuthorID Title 1 2 Shreak </code></pre> <p>I am trying to add more books to the books table through a GUI which has a drop down box to display the authors from the authors table and a textbox for entry of new book and a save button. The followiing is that correspond to the save button:</p> <pre><code>pprivate void save_bookActionPerformed(java.awt.event.ActionEvent evt) { try { String sql = "INSERT into books (AuthorId,Title) VALUES (?,?)"; pst = con.prepareStatement(sql); pst.setInt(1, author_name_combo.getSelectedItem()); pst.setString(2, book_name.getText()); pst.executeUpdate(); JOptionPane.showMessageDialog(null, "New Book has been added"); } catch (Exception e) { JOptionPane.showMessageDialog(null, e); } } private void FillCombo(){ try { con = DriverManager.getConnection(url, user, password); String sql = "SELECT * FROM Authors"; pst = con.prepareStatement(sql); rs = pst.executeQuery(); while(rs.next()) { String author = rs.getString("Name"); author_name_combo.addItem(author); } } catch (Exception e) { JOptionPane.showMessageDialog(null, e); } } </code></pre> <p>I am a beginner and im struggling to populate the table with new data. the progamme compiles but the books table display NULL for Both AUTHOR ID and TITLE. how do I populate the table correctly so that author Id is automatically looked up from Authors table and given the approprate id number from there. please note author id is foreign key in books table. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload