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

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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T00:42:45+00:00 2026-06-14T00:42:45+00:00

I am using JDBC prepared statement for bulk insertion. I am calling ps.execute() method.

  • 0

I am using JDBC prepared statement for bulk insertion. I am calling ps.execute() method. If it fails then i am calling a method where i am passing the list of parameters and the prepared statement. I am using merge sort technique to divide the list and then trying to insert the records, but not successfull.

Here is my code;

from executePrepareStatement method i am calling

this.executeInsertStatement(query, myCollection, 0, myCollection.size());

// executeInsertStatement method

public void executeInsertStatement1(String query, List myCollection, int sIndx, int eIndx) throws DBException,SQLException {

        int startIndx = sIndx, endIndx =  eIndx, mid = 0;

        try {
            try{
                if(conn.isClosed())
                    new DataService(CoreConstants.TARGET);
            } catch (Exception e) { }           
            if(startIndx >= endIndx) {
                return;
            }           
            conn.setAutoCommit(false);
            if (query != null) {
                mid = (startIndx + endIndx) / 2;
                ps = conn.prepareStatement(query);
                executeInsertStatement(query, myCollection, startIndx, mid);
                executeInsertStatement(query, myCollection, mid+1, endIndx);
                //int end_low = mid;
                //int start_high = mid + 1;
                if(mid < endIndx)
                    endIndx = mid;
                for (int i = 0; i < endIndx; i++) {
                    List list = (List) myCollection.get(i);
                    int count = 1;      
                    for (int j = 0; j < list.size(); j++) {
                        if(list.get(j) instanceof Timestamp) {
                            ps.setTimestamp(count,  (Timestamp) list.get(j));       
                        } else if(list.get(j) instanceof java.lang.Character) {
                            ps.setString(count, String.valueOf(list.get(j)));
                        }
                        else {
                            ps.setObject(count, list.get(j));
                        }
                        count++;
                    }
                    try {
                        ps.execute();   
                    } catch (Exception e) {
                        rollback();
                    }                   
                }
            }
        } catch (Exception e) {
            rollback();
        } finally{ 
            try {
                if (ps != null) {
                    ps.close();
                    ps = null;
                }
            } catch (Exception ex) {
            }
        }
    }

Thanks

  • 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-14T00:42:46+00:00Added an answer on June 14, 2026 at 12:42 am

    I don’t think you’re on the right track by using a merge sort. I understand that you’re trying to achive your solution using the concept of divide and conquer but I think you’re making the problem harder (and more confusing/complex) than it really needs to be.

    If I understand correctly, you have got a data set that you would want to insert into your database. And you would want to do it in bulk. PreparedStatement let’s you do that by using a couple of neat methods: addBatch() and executeBatch()

    Here’s an outline of how I would have tried to implement your requirement:

    • I would set a batch limit i.e. the number of statements in my batch when I would like to execute the batch. Unless I reach this limit (which I can very well track by using a counter), I would continue to add to the batch

    • Once I hit the limit, I execute the batch, reset the counter, clear the batch and redo Step #1

    • This would continue till I’m done with my entire data set. Finally I would end by either committing the data to the database or even performing a rollback, based on my requirement.

    Have a look at this answer for an example of how you might go about and implement this.

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

Sidebar

Related Questions

I am using following prepared statement : Class.forName(com.mysql.jdbc.Driver); con=DriverManager.getConnection(jdbc:mysql://localhost:3306/mysql,root,root); String query=select ename from ?
When I create a prepared statement like this in java (using JDBC): pStmt =
I know in Java, when using PreparedStatement with parameters, some JDBC drivers will optimize
I'm using the JDBC template and want to read from a database using prepared
The JDBC 3.0 spec talks about Connection (and Prepared Statement) pooling. We have several
I want to update the string date into MySQL database using prepared statement. I
I am trying to execute an SQL Function using Spring JDBC. My code is
I'm using JDBC for very simple database connectivity. I have created my connection/statement and
I'm using jdbcTemplate to make JDBC connections to a mySQL DB prepared statements to
Hi I am trying insert data into the database using prepared statement but I

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.