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 6889983
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:13:29+00:00 2026-05-27T06:13:29+00:00

Sorry this is such a long question but it touches on a general issue

  • 0

Sorry this is such a long question but it touches on a general issue with databases:

I’m using sqlite/jdbc (in this case), and I’m trying to keep track of the automatic ID’s created when I add rows to a table. The database has a main table ‘Person’ and other tables called ‘training’ and ‘induction’. The idea is that the persons name and other basic details go into the ‘Person’ table, and that one person many have many ‘trainings’ and ‘inductions’. So, I can look up all the inductions for a given person by SELECT * FROM INDUCTIONS WHERE PERSON_ID = whatever.

The problem is in creating the database, I have to add a person, then keep track of the AUTOINCREMENTing ID they got and then create the entries in the other table with that ID as a foreign key:

So

int id = 0;    
PreparedStatement prepPerson = conn.prepareStatement("insert into persons values (?, ?);");
prepPerson.setString(2, p.getName());
prepPerson.addBatch();

Then

PreparedStatement prepInductions = conn.prepareStatement("insert into inductions values (?, ?, ?, ?);");

Then loop over all the inductions {
prepInductions.setInt(2, id);
prepInductions.setString(3, i.getSite());
prepInductions.setString(4, i.getExpiryDate());
prepInductions.addBatch();
}

I want the interactions with the data base to be ‘atomic’, so I want to do

prepPerson.executeBatch();
prepInductions.executeBatch();
id++;

I realise could instead add the person, do getGeneratedKeys() and then add the inductions with the correct row_id of the new person, but I don’t want an exception to occur after the Person is added but before the Inductions are. If that happens then I’ll have to roll back time and remove the person.

At the moment as I know I’m creating the database afresh everytime from an external source I’m keeping track of the row_id with a local integer variable, and I know that’s living dangerously.

Isn’t there a general solution to this sort of situation? Or am I worrying much about the ‘atomic’ interactions with the database?

  • 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-27T06:13:30+00:00Added an answer on May 27, 2026 at 6:13 am

    Do use getGeneratedKeys() to retrieve the generated ID.

    To keep data integrity, use transactions. Normally, JDBC uses ‘autoCommit’ mode, but in this case you’ll need to handle the transaction manually.

    Call .setAutoCommit(false) on your connection and use .commit() and .rollback() to commit or discard changes.

    (Check the Connection Javadoc).

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

Sidebar

Related Questions

Sorry for such a trivial question, but I'd feel silly building this if it
Sorry this is such a basic question, but I can't find a straight answer
Ok sorry this might seem like a dumb question but I cannot figure this
Sorry for this not being a real question, but Sometime back i remember seeing
Sorry if this sounds like a really stupid question, but I need to make
I've been thinking of this question very long, but really couldn't find the answer
sorry, i dont like to ask such instance specific questions but this is driving
My question (which will follow after this, sorry about the long intro, the question
Sorry if this is an obvious question, but I've found surprisingly few references on
I'm sorry if this question has been asked before, but I can't seem to

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.