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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T09:32:06+00:00 2026-06-04T09:32:06+00:00

I have the following method. The commented methods saveOrUpdateToDatabase execute perfectly fine, but I

  • 0

I have the following method. The commented methods saveOrUpdateToDatabase execute perfectly fine, but I would like to use the executeBatch. What am I missing? The int[]r which receives the executeBatch() results is always empty…

public boolean saveOrUpdate(MonitoredData mData) {
        try {
            PreparedStatement prep;
            String timeID = this.getTimeLastRowID();

            for (CpuData o : mData.getCpu()) {
                prep = this.conn.prepareStatement(String.format(
                        INSERT_CPU_USAGE, this.nodeID, timeID, o.toString()));
                prep.addBatch();
                // saveOrUpdateToDatabase(String.format(INSERT_CPU_USAGE,
                // this.nodeID, timeID, o.toString()));
            }
            for (DiskData o : mData.getDisk()) {
                prep = this.conn.prepareStatement(String.format(
                        INSERT_DISK_USAGE, this.nodeID, timeID, o.toString()));
                prep.addBatch();
                // saveOrUpdateToDatabase(String.format(INSERT_DISK_USAGE,
                // this.nodeID, timeID, o.toString()));

            }
            for (NetworkData o : mData.getNet()) {
                prep = this.conn.prepareStatement(String
                        .format(INSERT_NETWORK_USAGE, this.nodeID, timeID, o
                                .toString()));
                prep.addBatch();
                // saveOrUpdateToDatabase(String.format(INSERT_NETWORK_USAGE,
                // this.nodeID, timeID, o.toString()));

            }

            prep = this.conn.prepareStatement(String.format(
                    INSERT_MEMORY_USAGE, this.nodeID, timeID, mData.getMem()
                            .toString()));
            // saveOrUpdateToDatabase(String.format(INSERT_MEMORY_USAGE,
            // this.nodeID, timeID, mData.getMem().toString()));

            conn.setAutoCommit(false);
            int[] r = prep.executeBatch();
            conn.setAutoCommit(true);

            return true;

        } catch (SQLException ex) {
            Logger.getLogger(HistoricalDatabase.class.getName()).log(
                    Level.SEVERE, null, ex);
        }
        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-06-04T09:32:07+00:00Added an answer on June 4, 2026 at 9:32 am

    What am I missing? The int[]r which receives the executeBatch() results is always empty…

    You are using the addBatch() method incorrectly. In your code you are doing:

    for (CpuData o : mData.getCpu()) {
        // WRONG!! you can not prepare a new query each time
        prep = this.conn.prepareStatement(INSERT_CPU_USAGE);
        prep.setObject(1, this.nodeID);
        prep.addBatch();
    }
    

    This replaces the prepared query each time. You should only be calling prepareStatement(...) once per batch. You should be doing something like the following. You will have to change your insert statements to have ? arguments:

    PreparedStatement prep = conn.prepareStatement(INSERT_CPU_USAGE);
    for (CpuData o : mData.getCpu()) {
        prep.setObject(1, this.nodeID);
        prep.setObject(2, timeID);
        prep.setObject(3, o);
        prep.addBatch();
    }
    prep.executeBatch();
    

    Notice that only one prepareStatement() call is being used. This has ? SQL argument entities in it that are then assigned in the loop with the prep.setString(1, ...). When the batch is ready to be executed you call prep.executeBatch() but this is without ever replacing the prep prepared statement which you are doing.

    If you are trying to perform a series of different insert statements in a row in the same batch then you should look into turning off auto-commit, doing your statements, call commit, and then turn auto-commit back on. Something like:

    conn.setAutoCommit(false);
    // statements prepared and executed here
    // maybe no need for batch operations
    ...
    conn.commit();
    conn.setAutoCommit(true);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an extension method that looks like the following: //[MethodImpl(MethodImplOptions.NoOptimization)] public static IEnumerable<char>
I have following method in wcf webenabled service Public Person AddPerson(Person p); As of
I have following method which I am using to load ActiveX control dynamically, Dim
I have the following method: private JobCard PopulateObject(JobCard jc, DataRow dataRow) { PropertyInfo[] proplist
I have the following method that is replacing a pound sign from the file
I have the following method public static void SerializeToXMLFile(Object obj,Type type, string fileName) {
I have the following method that is supposed to be a generic Save to
i have the following method in a usercontrol protected override void Render(HtmlTextWriter writer) {
I have the following method and interface: public object ProcessRules(List<IRule> rules) { foreach(IRule rule
I have the following method: static double NewtonMethodModified(Func<double, double> f, double x0, double h)

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.