I have seen many people using different jdbc connections to execute two or more transactions. There could be a valid reason, sometime, to do this; However, In this post, I ll explain that how can you use a single JDBC connection to execute 2 insert queries.

Steps to follow:

1) Disable the autocommit on the fJDBC connection.

2). Execute the sql statements using execute() function

3). commit the transactions manually.

Refer to the code below for the usage:

 

// This turns the auto commit mode to false
connection.setAutoCommit(false);

try {
    // First SQL insert statement
    stmt1.execute();
	
	// Second SQL insert statement
    stmt2.execute();
    
	//Close the connection
	connection.commit();
} catch (Exception ex) {
    connection.rollback();
}

Enter your email address:

Delivered by FeedBurner



Sandeep Joshi
Mathematics, Technology and Programming are my passion. I am a part of Java Ecosystem and through this blog, I contribute to it. I am here to blog about my interests, views and experiences.
I am on Google+ and Facebook.
I feel proud to be listed as a "National Memory Record Holder" in the Limca Book of Records, 2009 and have attempted for an International Memory record in the Guiness Book of Records. I can remember the value of PI upto 10,000 digits after the decimal (3.1415.....). You can contact me on javagenious.com(At)gmal.com ; I would like to hear from you :)