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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T01:23:32+00:00 2026-06-01T01:23:32+00:00

Scenerio: I’m inserting a CSV file into my database. I’m using jdbc driver to

  • 0

Scenerio:

  1. I’m inserting a CSV file into my database.
  2. I’m using jdbc driver to iterate over the file and do the my inserts
  3. A name column is made unique so inserts do not change the table when same value arrives
  4. I’m using INSERT INTO IGNORE to keep the iteration from breaking when event happens
  5. I’m using LAST_INSERT_ID() to add as FK in next table of insert
  6. I get 0 when INSERT_IGNORE happens

How do I do fix this?

 Connection con = null;

    try
    {
      Class.forName("com.mysql.jdbc.Driver");
      con = DriverManager.getConnection(URL, USERNAME, PASSWORD);
      con.setAutoCommit(false);

      Statement s1 = (Statement) con.createStatement();
      s1.executeUpdate("INSERT IGNORE INTO ORIGIN (ORI_NAME) VALUES(\""
          + d.getOriginName() + "\")");

      Statement s2 = (Statement) con.createStatement();
      s2.executeUpdate("INSERT IGNORE INTO STOCK (ORI_ID,S_TITLE) VALUES ("
          + "(SELECT ORI_ID FROM ORIGIN WHERE ORI_ID =LAST_INSERT_ID()),\""
          + d.getStockTitle() + "\")");

     }

      con.commit();

    }
    catch (Exception e)
    {
      if (con != null)
        try
        {
          con.rollback();
        }
        catch (SQLException e1)
        {
          e1.printStackTrace();
        }

      e.printStackTrace();
    }
    finally
    {
      if (con != null)
        try
        {
          con.close();
        }
        catch (SQLException e)
        {
          e.printStackTrace();
        }
    }

Example of CSV file to be inserted into the database

US, P1, M, 200, 344
US, P1, L, 233, 344
US, P2, M, 444, 556
MX, Q4, L, 656, 777

Tables: http://sqlfiddle.com/#!2/b5752

  • 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-01T01:23:33+00:00Added an answer on June 1, 2026 at 1:23 am

    If you want to get an ID for a subsequent insert, your code has to test for a 0 value of LAST_INSERT_ID and if it gets it, do a SELECT on the table to find the right value for the FK for your insert.

    In pseudo-code, this would look like:

    $err = mysql("insert ignore into mytable values ($x, $y)")
    if($err){
        do something for an error
    }
    $id = mysql("select last_insert_id()");
    if($id == 0){
        $id = mysql("select id from othertable where condition is true for the record I want to reference in my insert");
        if($id == 0){
            do something for error
        }
    }
    mysql("insert into othertable VALUES ($id, other stuff)")
    

    You’ll have to translate that to your own application and language.

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

Sidebar

Related Questions

I am looking for a real world scenerio where using exec will be the
How to add data to dictonary from xml file scenerio: I've declared a dictonary
Scenerio: I'd like to run commands on remote machines from a Java program over
Hi i have a scenerio i have a database dump which i want to
Consider the scenerio when I have typed: file.f When I type Ctrl-X Ctrl-O the
I am using mockito as mocking framework. I have a scenerio here, my when(abc.method()).thenReturn(value)
A common scenerio we are running into with our current application is where we
Here is the scenerio: We have an application running on Webphere Portal Server 6.1
Scenario: Attempting to import many (>100), large(>1M recs) flat-files (csv). Problem: Many records are
Scenario: I'm currently writing a layer to abstract 3 similar webservices into one useable

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.