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

  • SEARCH
  • Home
  • 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 7641633
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T08:58:25+00:00 2026-05-31T08:58:25+00:00

I’m attempting to create a database application for a bookstore. In the spec: After

  • 0

I’m attempting to create a database application for a bookstore.

In the spec:
“After registration, a user can order one or more books. The total amount of the order should be reported to him/her. A user may order multiple copies of a book, one or more times.”

The total amount bit doesn’t seem that bad.

Here are the two relevant tables:

Order(orderID: INT(11), loginName: VARCHAR(45), date: DATETIME)

BooksOrdered(orderID: INT(11), ISBN: VARCHAR(45), count: INT(11))

The bolded items are the PKs.

Now, here’s the deal: Our database doesn’t allow FKs for performance issues, so I’m supposed to mimic them in the application layer. BooksOrdered.orderID is really a FK, just not stated as much. Additionally, Order.orderID is autoincrementing.

I’m trying to figure out how to make sure the orderID’s match up (mimic the foreign key constraint) when inserting new orders.

Basically, I don’t understand how I can efficiently insert an order given an ArrayList (BookOrder will simply hold an ISBN and a count).

One solution I’ve thought of is:

Every time I want to add a row into BooksOrdered, simply grab the largest orderID from Order. This seems inefficient, and I think there might be other issues with it (concurrent access/insertions?).

Therefore I ask you, how should I go about mimicking this autoincrementing FK? Or would you suggest I restructure my tables?

Or is there a way I can magically grab the last orderID I insert into Order… say in the process of inserting?

EDIT:

Alright, so I have the following incomplete function:

public int orderBooks(String loginName, ArrayList<BookOrder> bookOrders) throws Exception{

    int id = -1;
    //Working on the date situation
    java.util.Date today = new java.util.Date();
    Timestamp sqlDate = new java.sql.Timestamp(today.getTime());

    Connector con=null;
    String query = "INSERT INTO Order (loginName, date) " +
            " VALUES ('"+loginName+"', "+sqlDate+")";
    ResultSet rs; 


    try{
        con= new Connector();
        con.stmt.executeUpdate(query, Statement.RETURN_GENERATED_KEYS);
        rs = con.stmt.getGeneratedKeys();
        if (rs.next()) {
            id = rs.getInt(1);
        } else {
            throw(new Exception()); // mmm...
            // throw an exception from here
        }
        con.stmt.close();
    } catch(Exception e) {
        System.err.println("Unable to execute query:"+query+"\n");
        System.err.println(e.getMessage());
        throw(e);
    }
    finally
     {
         if (con != null)
         {
             try
             {
                 con.closeConnection();
             }
             catch (Exception e) { /* ignore close errors */ }
         }
     }




    return id;
}

Now, where should I put id = stmt.executeUpdate(query, Statement.RETURN_GENERATED_KEYS);? Did I put it in the right spot? (continues to work on building a testable prototype)

  • 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-05-31T08:58:27+00:00Added an answer on May 31, 2026 at 8:58 am

    Which language are you using? It will likely have a function to retrieve the last autoincremented value from your current database handle. In PHP, for example, you would use mysql_insert_id() (doc). In Perl DBI you can use last_insert_id(). (DBI Doc)

    For have you want to set the flag in executeUpdate:

    id = stmt.executeUpdate(query, Statement.RETURN_GENERATED_KEYS);
    

    There’s also the stmt.getGeneratedKeys() function. See the docs. The executeUpdate entry is right below getGeneratedKeys.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
In my XML file chapters tag has more chapter tag.i need to display chapters
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
Does anyone know how can I replace this 2 symbol below from the string

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.