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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T22:16:36+00:00 2026-05-16T22:16:36+00:00

I have a doubt regarding database operation.I have one insert query that should run

  • 0

I have a doubt regarding database operation.I have one insert query that should run for 10 times. the loop starts and inserted 4 or 5 val while inserting 6th, the db connection got failed for a while and again connected. then what will happen,
whether it skips that particular val or throws exception or roll back th entire operation?

EDIT : Sample Code

try
{
    String sql_ji_inser="insert into job_input values (?,?)";
    PreparedStatement pst_ji_inser=OPConnect.prepareStatement(sql_ji_inser);

    for(int i=0;i<v_new_data.size();i++)
            {
      Vector row=new Vector();
              row=(Vector)v_new_data.get(i);

              job_id=Integer.parseInt(row.get(0).toString());
              item_no=Integer.parseInt(row.get(1).toString());
              pst_ji_inser.setInt(1,job_id);
              pst_ji_inser.setInt(2,item_no);
              pst_ji_inser.addBatch();
            }
            System.out.println("No of rows inserted"+pst_ji_inser.executeBatch().length);
    }
    catch(Exception ex)
    {
           System.out.println("********Insert Exception*********************");
           ex.printStackTrace();
           return false;
    }

Is this the right way

try 
{
int count=0;// for checking no of inserting values
OPConnect.setAutoCommit(false);
String sql_ji_inser="insert into job_input values (?,?)";
PreparedStatement pst_ji_inser=OPConnect.prepareStatement(sql_ji_inser);
for(int i=0;i<v_new_data.size();i++)
    {
    job_id=Integer.parseInt(row.get(0).toString());
    item_no=Integer.parseInt(row.get(1).toString());
    pst_ji_inser.setInt(1,job_id);
    pst_ji_inser.setInt(2,item_no);
    pst_ji_inser.addBatch();
    count++;
    }
int norowinserted=pst_ji_inser.executeBatch().length;
if(count==norowinserted)
    {   
    OPConnect.commit();
    }
}
catch(Exception ex)
{
System.out.println("********Insert Exception*********************");
OPConnect.rollback();
ex.printStackTrace();
return false;
}
  • 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-16T22:16:37+00:00Added an answer on May 16, 2026 at 10:16 pm

    That depends on how you’re inserting the rows. If you’re inserting them in a single transaction on a connection which has auto-commit turned off by connection.setAutoCommit(false) and you’re commiting the connection after completing the insert queries using connection.commit() and you’re explicitly calling connection.rollback() inside the catch block, then the entire transaction will be rolled back. Otherwise, you’re dependent on environmental factors you have no control over.

    See also:

    • When to call rollback?

    Update: here’s a rewrite of your code. Note that the connection and statement should be declared before the try, acquired in the try and closed in the finally. This is to prevent resource leaking in case of exceptions.

    String sql = "insert into job_input values (?, ?)";
    Connection connection = null;
    PreparedStatement statement = null;
    
    try {
        connection = database.getConnection();
        connection.setAutoCommit(false);
        statement = connection.prepareStatement(sql);
    
        for (List row : data) {
            statement.setInt(1, Integer.parseInt(row.get(0).toString()));
            statement.setInt(2, Integer.parseInt(row.get(1).toString()));
            statement.addBatch();
        }
    
        statement.executeBatch();
        connection.commit();
        return true;
    } catch (SQLException e) {
        if (connection != null) try { connection.rollback(); } catch (SQLException logOrIgnore) {}
        e.printStackTrace();
        return false;
    } finally {
        if (statement != null) try { statement.close(); } catch (SQLException logOrIgnore) {}
        if (connection != null) try { connection.close(); } catch (SQLException logOrIgnore) {}
    }
    

    I am by the way not a fan of returning a boolean here. I’d just make the method void, let the catch throw e and put the calling code in a try-catch.

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

Sidebar

Related Questions

I have one doubt regarding casting. public void Test(out T a, out T b)
I have a very basic doubt regarding the method that gets executed when app
I have one doubt regarding audio processing for noise reduction. Is there any free
I have a small doubt regarding BEFORE INSERT TRIGGER in oracle, my trigger looks
I have a very basic doubt regarding cloud computing that is catching up pretty
I have a doubt regarding JNI function calls made from Android. Do they run
I have one doubt regarding xml reading using Stax Parser. Expected Result: hq_seq_mast {SEQ_VAL=SEQ_VAL,
I have a doubt regarding downloading data from a web service. One way is
I have a doubt regarding a database design, suppose a finance/stock software in the
I am reading linux device driver book of rubini,corbet and hartmen.I have doubt regarding

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.