Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8666593
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T17:46:08+00:00 2026-06-12T17:46:08+00:00

I have the following sql tables: Authors: Id Name 2 John Smith Books: Id

  • 0

I have the following sql tables:

Authors:

Id           Name

2         John Smith

Books:

Id        AuthorID       Title
 1           2          Shreak

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:

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

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T17:46:09+00:00Added an answer on June 12, 2026 at 5:46 pm

    Assuming you’re working with Swing (i.e. JComboBox and JTextField).

    You are not assigning the values to the ? variables correctly. First, AuthorID is an integer and you need to assign the ID of the author, not the selected text.

    One way to go around it is to create your own simple class to hold ID and name of an author, with the corresponding toString() method:

    class OneAuthor extends AbstractMap.SimpleEntry<Integer, String> {
        public OneAuthor(Integer id, String name) {
            super(id, name);
        }
    
        public String toString() {
            return getValue();
        }
    }
    

    Then, when you’re populating your JComboBox with authors’ names, use this class instead of String:

    ResultSet rs = con.createStatement().executeQuery("SELECT id, name FROM authors ORDER BY name");
    
    while(rs.next()) {
        author_names_combo.addItem(new OneAuthor(rs.getInt(1), rs.getString(2));
    }
    

    And then you can easily specify the correct author’s ID:

    pst.setInt(1, ((OneAuthor)author_name_combo.getSelectedItem()).getKey());
    

    Second, instead of using getSelectedText(), you need to use getText() method on the book title text field. getSelectedText() will only return the text highlighted in the field, while getText() will return the whole text. Thus, your full code will be something like this:

    pst = con.prepareStatement(sql);
    pst.setInt(1, ((OneAuthor)author_name_combo.getSelectedItem()).getKey());
    pst.setString(2, book_name.getText());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following tables in my SQL Database: authors: name varchar id int
I have the following tables in SQL Server 2005 ReceiptPoint: ID (PK), Name GasIndexLocation:
I have the following 3 classes(mapped to sql tables). Places table: Name(key) Address Capacity
I have 4 tables Titles having the following fields, id (PK), Name, AuthorId(FK) &
Suppose i have the following 2 SQL tables: Foo Column DataType --------------------------- Title NVARCHAR(20)
i have the following tables in sql server: photoalbumsTable: album_ID album_caption albumtagmap id album_id
I am using VB.NET with LINQ to MS SQL. I have two following tables.
I have the following SQL problem. Scenario: I have two tables: Change and ChangeTicket.
I have the following tables: Post (id, title, author_id) etc Tags Post_tags Author The
I have the following sql tables oitems table +---------+-----------+----------+ | orderid | catalogid |

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.