Struggling with inserting multiple rows using a Java prepared statement

--UPDATE--After troubleshooting, we discovered the issue was related to our Shareplow installation. Appreciate everyone's efforts!

Following the advice found on Performance of MySQL Insert statements in Java: Batch mode prepared statements vs single insert with multiple values, I attempted to utilize bulk insert prepared statements. However, only the initial row is successfully being inserted.

Included below is a snippet of my code:

var sql2 = 'INSERT INTO ' + memtable2 + ' (' + locNameCol2 + ', ' + sectTypeCol + ', ' + sectPathCol + ') VALUES ';
var cntr = 0;
for (var key in polyObj) {
    if(cntr){
        sql2 += ',';
    }
    sql2 += '(?,?,?)';
    cntr = 1;
}

var s2 = con2.prepareStatement(sql2);
cntr = 0;
for (var key in polyObj) {
    indxOffset = 3*cntr;
    s2.setString(indxOffset+1 , fieldNameVal);
    s2.setString(indxOffset+2 , 'Poly');
    s2.setString(indxOffset+3 , polyObj[key]);
    rval += 'index offset ' + indxOffset;
    cntr++;
}
s2.execute();

The first row is inserted without any issues, but subsequent rows fail to be inserted. Upon examining the constructed sql2 string, it appears to be generated correctly for the number of properties in polyObj. For instance, if polyObj contains 3 properties, the SQL statement would resemble:

Insert into table (col1, col2, col3) values (?,?,?),(?,?,?),(?,?,?)
Could there possibly be a database setting preventing multiple row inserts? Or could I be overlooking something? Any insights would be greatly appreciated as I am at a loss.

Thank you in advance.

Answer №1

To optimize your database queries, it is recommended to use a PreparedStatement with executeBatch() instead.

String sql = "insert into foo (x,y,z) values (?,?,?)
PreparedStatement ps = connection.prepareStatement(sql);
ps.setObject(1, "a1");
ps.setObject(2, "b1");
ps.setObject(3, "c1");
ps.addBatch();
ps.setObject(1, "a2");
ps.setObject(2, "b2");
ps.setObject(3, "c2");
ps.addBatch();
ps.setObject(1, "a3");
ps.setObject(2, "b3");
ps.setObject(3, "c3");
ps.addBatch();
ps.executeBatch();

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How to retrieve a string from a regular expression in Javascript without [Object object] output

Within my code, there exists a parent form component and a child component used for auto-completing text input. The Parent component passes an array of objects named autoCompTxt, consisting of name and id fields, to the Child component. //Parent: const [ob ...

What is the best way to release an NPM package containing both commonjs and ES6 versions?

My goal is to publish a module on npm, but I've encountered outdated solutions and issues with babel configurations. Despite trying various examples, I still run into problems where the code doesn't work as intended. Ideally, I want to write my ...

Evaluating numerous array elements within an if statement

For my simple tic tac toe game, I am currently checking for various win states. The game board is stored in an array, and to check for a win with the top three spaces, I am using the following logic: if (tableArr[0].hasClass('userTaken') &&a ...

What is the best way to configure a process to run once another process has completed?

I have developed a PHP video uploader script that triggers an FFmpeg script for converting the uploaded file. Following the conversion process, the video details are inserted into a MySQL database with a status value of 0 to signify that the file is curren ...

Segmenting SQL query by any desired number of rows

I am tasked with analyzing periodic measurements stored in a SQL table. The goal is to calculate a summary, such as the sum of values, over a specified number of rows at a time. For example, given the following data: id | reading 1 10 5 14 7 ...

Tips for organizing your JSON Structure within ReactJs

In the given example, I have a JSON structure with information about different airlines. The Airline Name is dynamic and we need to separate the JSON into an expected array format. const arr = [ { Airline: "Goair", Departure: "01:50" ...

Clever ways to refresh the current page before navigating to a new link using ajax and jQuery

Here's a different perspective <a href="{{$cart_items->contains('id',$productItem->id) ? route('IndexCart'): route('AddToCart')}}" class="item_add" id="{{$productItem->id}}"><p class="number item_price ...

Cannot locate default Java Classes in Eclipse on Mac operating system

I am familiar with Android and C development in Eclipse, but I have not had much experience working strictly with Java. Recently, I tried adding code that compiles using "javac codename.java" to an Eclipse project, but I encountered errors. Eclipse was u ...

HTML not rendering Angular object as expected

Having trouble with my Angular project on LinkedInLearning. Can't seem to display object properties in my component, even though I can see them when logging the object. The boolean value renders fine, but not the object properties. Here is the snippe ...

I'm interested in setting up a table in MySQL with two columns that will automatically increment as integers

Is it possible to set two columns in MySql as auto increment (int)? The columns are S.NO and Q_id. I want both of them to be auto-incremented. How can I achieve this? ...

Oops! There seems to be an error with the <path> attribute. It looks like we were expecting a number, but received something different: "

I'm currently working on creating a basic line graph using d3.js and integrating it into a React component. However, I'm encountering this error: Error: <path> attribute d: Expected number, "MNaN,36.393574100…" Unfortunately, the similar ...

Using Vuex in the router: A comprehensive guide

I am having trouble accessing data from the store in the router. I have attempted three different methods, but none of them seem to be working correctly. Here are the methods I tried: // ReferenceError: store is not defined console.log(store.state); // ...

The method is encountering an error due to the missing .class element, or because integers cannot be dereferenced

I'm very close to achieving my goals in this class, but the getNumbers method keeps throwing errors. It's asking for ".class" where I've mentioned if numbers[i].isValid(i), and when I try changing it, I get an "int cannot be dereferenced" er ...

Transferring Data Straight to Google Cloud Storage

I need to download a PDF document in arraybuffer format from a third-party vendor and then directly upload it to Google Storage without saving a local copy of the file. const axios = require('axios'); const google_storage = new Storage({ keyFilen ...

Email alerts for unsuccessful scheduled tasks - MySQL

My stored procedures are typically scheduled to run at night due to long execution times, but occasionally they need to be adjusted during the day. With over 1000 lines of code, it's easy for small syntax errors to slip in unintentionally. I'm w ...

Storing data on your local machine using Electron

I am in need of help with my template files which have variable strings. I want to create a basic input form using Electron (https://www.electronjs.org/) and save the resulting output file on the user's device. Could someone recommend a module that e ...

Enhancing w3-import-html with JavaScript

<div id="import" includeHTML="page.html"></div> function getInclude() { var x = document.getElementById("import").includeHTML; //returns 'undefined' alert(x); } function modInclude() { document.getElementById("import") ...

Monitoring files in nested directories with Apache Camel

I am looking to create a camel route that can monitor files located in subfolders. Here is the structure of the file system: output folder1 file1 folder2 file2 file3 folder3 file4 etc. I have already attempted the following route: public cl ...

Interpret iOS toast notifications with Appium

Can someone please help me with the code to read a toast message in iOS? I have successfully read Android toast messages using AndroidElement toastElement = driver.findElementByXPath("//android.widget.Toast[1]"); String toastMessage = toastElement.getAttri ...

Ruby on Rails has denied access for user 'root' at 'localhost'

I'm completely new to the world of Ruby on Rails and I've been trying my best to follow along with this tutorial at tutorial. However, when attempting to run MySQL, I encountered the following error message: ERROR 1045 (28000): Access denied for ...