Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Java JDBC MySQL multiple INSERTS in one executeUpdate()

I’m using JDBC to make my Java application handle SQL statements to my MySQL database.
For some reason it can’t do multiple insert statements in one executeUpdate() method.

My code looks like this:

PreparedStatement statement = connection().prepareStatement(sql);
boolean success = statement.executeUpdate() > 0;

if (doClose)
    closeConnection();
return success;

where sql is a string of the SQL statement.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

It tells me there is a sql syntax error, where the new insert statement begins.

The SQL looks like this:

INSERT INTO subscriptions(user_id,`status`,subscription_type,offer_type,offer_expires,offer_effect,card_id) 
VALUES (18,'ACCEPTED','FREEMIUM',NULL,NULL,NULL,NULL); 
INSERT INTO contact_informations(user_id,email,first_digits,phone_number,phone_is_mobile,street,floor,postal,city,country_title,country_indexes) 
VALUES (18,'cool@gmail.com',45,57104998,false,'Nørrevang 15','5. th.','2528','Holbæk','Tyskland','DE');

It works perfectly, when I split the two statement, but in my MySQL workbench editor, it handles the statement in one take.

Is there a way to perform both in one JDBC statement?

>Solution :

You should try adding ?allowMultiQueries=true to your JDBC connection URL.

Example:
jdbc:mysql://server-name:server-port/database-name?allowMultiQueries=true

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading