Explain use of commit () and rollback() in JDBC programming with the help of a small program
In JDBC programming, `commit()` and `rollback()` methods are crucial for managing transactions, ensuring data integrity in a database.
`commit()`: This method finalizes changes made during a transaction. It makes all modifications permanent in the database. For instance, if you insert multiple records into a database, calling `commit()` saves all the changes.
`rollback()`: This method undoes changes made during a transaction if an error occurs or if you decide not to keep the modifications. It reverts the database to its previous state.
Example Program:
```java
import java.sql.*;
public class JdbcTransactionExample {
public static void main(String[] args) {
Connection conn = null;
try {
// Establishing connection
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "user", "password");
conn.setAutoCommit(false); // Start transaction
// Executing SQL commands
Statement stmt = conn.createStatement();
__________ _______ _________ _____ __________ _______.
___ ________ ___ ____ ________ _____.
__________ _____ _____ __________ _________ ___.
____ ________ _________ ___ ___ ____ ___ ________ ______ ______ ______ _____.
_______ ________ ____ _______ ______ _________.
_____ ___ ___ ________ _____ ____ _____ ______ _________.
______ _______ _________ ________ ______ _____ _________ ______ __________ _______ ______ ___.
___ ________ _____ ___ _________ ______ ____ _____ ______.
________ _____ _________ _________ _________.
__________ ______ _______ ___ _________ _________ ________ _____.
____ _______ ____ _______ ___.
_______ ______ ________ _____ ___ ___ _________.
_________ ___ _______ _____ __________ _________ ______ ______ ____.
____ _____ _________ __________ __________ _________.
_____ _______ _______ _______ ______ ________ ______ _______ ____ ________.
_____ ______ ___ ______ ____ _____ _____ ____ _________ ___.
_________ ___ ______ _________ ___ _______ ________ ___ _____ _____ __________ _____.
_____ _______ _______ ___ ______ _____ _________ ___ ____ ______ _________ ____.
________ ___ _________ ____ _______ ___.
_____ _____ _______ ____ ___ _______ _____ _______ ____ ______ ______.
___ ____ __________ __________ _________ ________ ______.
___ __________ ___ _________ __________ ______ ______ ____ ____ ___ ___ ___.
____ _________ _______ ____ ______ ____ ______ ______ _______ _________ ______.
__________ _________ ________ _______ __________ _______ ________ _________ _______.
_____ ______ __________ ___ __________ ___ ____ ___ ______.
_____ _________ ________ _____ ___ ____ _____ _______ __________.
_________ _________ ____ __________ ______ ________ ___ ______ _________ __________ __________ _______.
________ ______ ____ ____ _______ __________ _________ _____ _______ ______ ______.
__________ _____ ___ _____ ____ ________ __________ _______ _________ __________ _______ _______.
_______ __________ _______ _______ _______ ___.
_____ _____ ______ __________ __________ ______ __________.
________ __________ ___ _____ ___ ___ _____ ____.
___ _________ ___ ___ ____ ______ _____.
- Solved Assignments
- Study Notes
- Guess Papers
- Question Papers
✓ Unlock up to 60% of the answer
✓ No payment required