Home > Database > Mysql Tutorial > body text

What is the use of setAutoCommit() method in JDBC?

WBOY
Release: 2023-08-28 15:21:02
forward
1289 people have browsed it

JDBC 中 setAutoCommit() 方法有什么用?

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);
Copy after login

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......");
   }
}
Copy after login

Output

Connection established......
Records inserted......
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!