What is the use of setAutoCommit() method in JDBC?
If you commit the database, it will save all changes made up to that specific point.
You can use the commit() method to commit the database. Whenever any problem occurs, you can use the rollback() method to restore the database to this point. Some databases automatically commit the database by default. However, when managing transactions, you need to manually commit the database.
In this case, you can use the setAutoCommit() method. This method belongs to the Connection interface and accepts a Boolean value.
If you pass true to this method it will turn on the database's auto-commit feature, if you pass false to this method it will turn on the database's auto-commit feature. Turn off the automatic submission function of the database.
You can use this method to turn off the auto-commit feature of the database:
Con.setAutoCommit(false);
Example
The following program uses batch processing to insert data into this table. Here we set autocommit to false, add the required statements to the batch, execute the batch, and commit to the database ourselves.
import java.sql.Connection; import java.sql.DriverManager; import java.sql.Statement; public class BatchProcessing_Statement { public static void main(String args[])throws Exception { //Getting the connection String mysqlUrl = "jdbc:mysql://localhost/sampleDB"; Connection con = DriverManager.getConnection(mysqlUrl, "root", "password"); System.out.println("Connection established......"); //CREATE TABLE Dispatches( Product_Name VARCHAR(255), Name_Of_Customer VARCHAR(255), Month_Of_Dispatch VARCHAR(255), Price INT, Location VARCHAR(255)); //Creating a Statement object Statement stmt = con.createStatement(); //Setting auto-commit false con.setAutoCommit(false); //Statements to insert records String insert1 = "INSERT INTO Dispatches( Product_Name , Name_Of_Customer , " + "Month_Of_Dispatch , Price, Location) VALUES " + "('KeyBoard', 'Amith', 'January', 1000, 'hyderabad')"; String insert2 = "INSERT INTO Dispatches( Product_Name , Name_Of_Customer , " + "Month_Of_Dispatch , Price, Location) VALUES " + "('Earphones', 'SUMITH', 'March', 500, 'Vishakhapatnam')"; String insert3 = "INSERT INTO Dispatches( Product_Name , Name_Of_Customer , " + "Month_Of_Dispatch , Price, Location) VALUES " + "('Mouse', 'Sudha', 'September', 200, 'Vijayawada')"; //Adding the statements to the batch stmt.addBatch(insert1); stmt.addBatch(insert2); stmt.addBatch(insert3); //Executing the batch stmt.executeBatch(); //Saving the changes con.commit(); System.out.println("Records inserted......"); } }
Output
Connection established...... Records inserted......
The above is the detailed content of What is the use of setAutoCommit() method in JDBC?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Reduce the use of MySQL memory in Docker

How do you alter a table in MySQL using the ALTER TABLE statement?

How to solve the problem of mysql cannot open shared library

Run MySQl in Linux (with/without podman container with phpmyadmin)

What is SQLite? Comprehensive overview

Running multiple MySQL versions on MacOS: A step-by-step guide

What are some popular MySQL GUI tools (e.g., MySQL Workbench, phpMyAdmin)?

How do I configure SSL/TLS encryption for MySQL connections?
