Write a program to demonstrate use of commit and rollback methods in JDBC programming.
To demonstrate the use of commit and rollback methods in JDBC (Java Database Connectivity) programming, we need to follow these steps:
- Establish a connection to the database.
- Disable auto-commit mode, so we can manually control transactions.
- Perform a series of database operations (e.g., insertions, updates).
- Use the
commitmethod to save the changes if all operations succeed. - Use the
rollbackmethod to undo changes if any operation fails.
Here’s a complete Java program that demonstrates this process:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class JDBCCommitRollbackDemo {
public static void main(String[] args) {
// Database URL, username, and password
String url = "jdbc:mysql://localhost:3306/testdb";
String user = "root";
String password = "password";
Connection conn = null;
PreparedStatement pstmt1 = null;
PreparedStatement pstmt2 = null;
try {
// Step 1: Establish the connection to the database
conn = DriverManager.getConnection(url, user, password);
// Step 2: Disable auto-commit mode
conn.setAutoCommit(false);
// Step 3: Prepare SQL statements
String sql1 = "INSERT
________ _____ _____ _________ __________ _______ _______ _________ __________ _______.
________ ___ ___ __________ _____ ______ _____.
_____ ____ _______ __________ _________ ______ _____ _______ _______ _________ ____ ___.
__________ ______ ____ _____ _____ _______ _____ _______ ______ __________ ____.
___ _________ ____ __________ _________ _____ __________ __________ ________ _________ _____.
__________ _____ ___ __________ _________ ______ _________ ________.
_________ ______ ___ ________ ______ _______ ___.
_______ _______ __________ ________ _________ __________.
_________ ________ _________ ____ _______ ____ ________ ___ ___.
________ ______ ___ __________ ________ __________ _________.
________ __________ ____ ___ __________.
_______ ___ ___ _____ ______ _____ _______ _____ ____ _____.
_________ __________ ______ ____ _______ _____ _____ ______ ____ _______ _________ _________.
_____ ___ _____ ____ ______ ______ ___ ________.
__________ ______ _____ ______ ____ ______ _________ ________.
_____ _____ __________ _____ _________ _____ ______ _______ _________ ___ _________ ______.
___ ____ _________ ______ _____ ___ __________ ____ ______.
_____ _____ _________ _______ _____ ____ ____ _______ __________ _____.
___ __________ ______ _____ ______.
______ ______ ___ _________ __________ _____.
______ ____ ______ _______ ____ __________ __________.
________ ______ _____ _________ ________ _________.
_________ _________ ______ ____ ____ _____ ___ _____.
______ ________ ___ ________ ____ ______.
_______ _______ ____ _____ ___ ____ _________ _____ ________ ____ ____.
_______ ___ ________ ____ ______ _______ __________ _______ _____.
________ ___ _________ _____ ___.
______ ________ ______ ______ _____ _________ ___ _____ ___ _______ _________ ______.
___ ______ __________ ___ _________ ______ ___ _______ _______ __________.
_____ __________ ___ _______ ___ ___ __________ _____.
______ ___ _______ ____ ______ ________ ____.
_____ ____ ____ __________ ___ ________ _____ ______ _________.
__________ _______ ______ _________ __________ __________.
_______ ______ _________ ________ _______ ______ ___ ______ _____ ___ ______ ________.
______ ______ __________ ________ ____ ________ ________ ________ ______ ____ _________ _______.
______ _________ ______ ____ _______.
________ ______ _______ __________ _______ _______ _____ ___ _______ ________ ____.
_______ ____ ___ ___ ________.
__________ ___ ___ _____ _________ _________ ___ _____ _______ _________ _____ _____.
_____ _____ ____ _________ ________ ________.
__________ ___ __________ __________ _________ ___ __________ _____ ______.
_______ ____ __________ _________ _________ ________ ______ _________ _______.
__________ _____ _______ ___ ________ ______.
___ _____ __________ ___ ______.
_________ _____ ___ _________ ____ __________ ______ ____ ______ ____ ___ __________.
__________ _________ _________ _________ ________ _________ ______ _________.
_________ _____ ____ ____ _______ ______ ___ _________ ________ ________ ___.
____ ________ _____ ______ ___ ______ _________ ___ ______.
______ ______ __________ ___ _____ ___ ______ _____ ______ ________.
_____ ______ ____ ____ __________ ___ _______ ___ ________ ______ _____ ________.
__________ ___ ______ ______ __________ ____ _______ ______.
__________ __________ ________ ____ __________ ________ __________ ____ _____ ____ ___ ________.
_____ ____ _____ _________ ______ _______ ________ _______ ______ _____.
_________ ______ _______ ____ ____ _______ __________.
___ ________ ______ ____ _______ _______ _________ ______ ________ ____ ____.
__________ ___ ___ ________ ______ ____ __________ ____ _________.
________ ____ ___ ________ ___.
______ ______ _______ ______ _________ ______ _______ __________ __________ _________.
____ _____ _____ __________ ______ _________ ____ _________.
________ __________ ____ _______ _______ _____ __________.
_____ _______ _______ ________ _____.
___ _____ _______ _________ __________.
____ _______ ______ ____ _______ ______.
____ ______ __________ ________ _____.
______ ____ __________ _____ _____ __________ __________ ____.
_________ ___ ____ __________ ______ ________ _____ ____ _____ ______ ______.
____ _______ ___ ________ ___ _______ _______ __________.
______ ________ _______ __________ _________.
________ _____ __________ _______ ________ _____ _______ _____ _________.
________ ___ ___ _____ _________.
___ _________ _________ _________ ________ _________ ________ _________ ________.
____ ___ __________ _________ __________ ____ ________ ____ ___ ____.
_____ __________ ___ ________ _________.
__________ _________ ________ _______ _____ ___ ___.
______ _________ ____ ______ ________.
______ ______ ______ __________ __________ __________ __________ __________ _____ ___.
_____ ____ _______ ______ __________ _________.
_______ _____ ________ _____ _________ _____ __________ _________ ________ ____.
____ _____ ________ ____ ________ ________.
___ _________ ______ _________ ___ _________ ___ ___.
_______ __________ _______ __________ ________.
___ _________ ________ __________ _____ _________ __________ ___ ______ ______ ___ _________.
__________ ________ ____ _____ ____ ___ ________.
_____ ___ __________ _____ _____ __________ __________ _______ __________.
__________ ____ ________ _____ ______ ___ _______.
____ _____ ____ ________ ________ _________ __________ ____.
_____ _______ _________ ______ ________ ____.
________ _________ _______ ____ ________.
- Solved Assignments
- Study Notes
- Guess Papers
- Question Papers
✓ Unlock up to 60% of the answer
✓ No payment required