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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:58:08+00:00 2026-06-13T13:58:08+00:00

I think the question sums it up well enough, here’s my code at the

  • 0

I think the question sums it up well enough, here’s my code at the moment but it’s not giving me the required results. I want to return an int array containing the generated auto-increment id’s for each row created in that batch.

CREATE_APPOINTMENT = database.prepareStatement("INSERT INTO " + TABLE_APPOINTMENTS + " VALUES (?, ?, ?, ?, ?)");

   public static int[] createAppointment(Appointment... appointments) {
    int[] ids = new int[appointments.length];
    int count = 0;
    try {
        for (Appointment a : appointments) {
            CREATE_APPOINTMENT.setDate(2, a.date);
            CREATE_APPOINTMENT.setTime(3, a.time);
            CREATE_APPOINTMENT.setInt(4, a.duration);
            CREATE_APPOINTMENT.setString(5, a.clientName);
            CREATE_APPOINTMENT.addBatch();
        }
        CREATE_APPOINTMENT.executeBatch();

        ResultSet resultSet = CREATE_APPOINTMENT.getGeneratedKeys();
        if (resultSet != null)
            while (resultSet.next()) {
                ids[count++] = resultSet.getInt(1);
            }

    } catch (SQLException ex) {
        System.err.println(ex);
    }
    return ids;
}

I’ve tried creating 8 appointments and their id’s are created successfully according to the database but my returns array only contains the last id and the rest are zeros. Where am I missing something?

  • 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-13T13:58:09+00:00Added an answer on June 13, 2026 at 1:58 pm

    Actually it is impossible. stmt.getGeneratedKeys() will always return only one value because this method in sqlite-jdbc driver is implemented so:

    
        ResultSet getGeneratedKeys() throws SQLException {
                if (getGeneratedKeys == null)
                    getGeneratedKeys = conn.prepareStatement("select last_insert_rowid();");
                return getGeneratedKeys.executeQuery();
            }
    
    

    Your array is initialized with zero values, and then the last generated id is assigned to the first element of array.

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

Sidebar

Related Questions

I think my question is covered pretty well in my question, but here's some
I think my question might seem a bit odd, but here it goes; I'm
I think my question is pretty stupid, but here it goes: I am using
Following pseudocode sums up my question pretty well I think... class Owner { Bar
I think the question is clear enough but still - what's the difference between:
I think my question is clear enough, but I explain more. Simply, when we
I think the question really sums up what i'm trying to do. Here is
I think my question is somewhat similar to CaptainPicard's but dissimilar enough that I
I think my question is already clear enough, but to make it even more
Well the title Question pretty much sums it up, But I'd like to detail

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.