- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java Connection getAutoCommit() method with example
If you commit a database, it saves all the changes that have been done till that particular point. By default, some databases commits/saves the changes done automatically. You can turn off/on the auto-commit using the setAutoCommit() method of the Connection interface.
The getAutocommit() method of the Connection interface is used to get the current value of the auto-commit in this connection.
To get the auto-commit value −
Register the driver using the registerDriver() method of the DriverManager class as −
//Registering the Driver DriverManager.registerDriver(new com.mysql.jdbc.Driver());
Get the connection using the getConnection() method of the DriverManager class as −
//Getting the connection String url = "jdbc:mysql://localhost/mydatabase"; Connection con = DriverManager.getConnection(url, "root", "password");
Turn off/on the auto-commit using the setAutoCommit() method as −
//Setting the auto commit on con.setAutoCommit(true); //Setting the auto commit off con.setAutoCommit(false);
Get the current auto-commit value using the getAutoCommit() method as −
con.getAutoCommit();
Following JDBC program establishes a connection with the database and turns off the auto-commit and retrieves the current auto-commit value (which is false ).
Example
import java.sql.Connection; import java.sql.DriverManager; public class Connection_getAutoCommit { 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......"); //Setting auto-commit false con.setAutoCommit(false); //Retrieving the current value of the auto-commit in this connection boolean autoCommit = con.getAutoCommit(); System.out.println("Auto commit value is: "+autoCommit); } }
Output
Connection established...... Auto commit value is: false
To Continue Learning Please Login
Login with Google