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
________ ___ ____ _____ _____ _________ _______ ___.
___ ________ _________ ___ ________ _________ _______ ____ _________ ____.
________ _____ __________ _____ ___ ____ _______ _______ ______ ________ _____ ________.
_____ ___ ____ __________ _________ ___ ____ _______.
____ _____ _______ _________ ____ _______ _________.
___ _________ ______ _____ ______ ________ _____ ______ ________.
____ ____ _________ __________ ___.
__________ _________ _________ ______ ______ _____.
__________ _____ __________ ________ ____ ______ __________ _____ _______.
_______ ____ ___ ____ _________.
________ __________ __________ _____ ____ ______ ______.
__________ ___ ________ ____ _____ ____ __________.
________ _________ _________ _______ ____ ______ ______ _____ __________ _________.
_________ ______ _________ _______ _____ _____ _______ _____ ________ __________ _______ ________.
_________ ___ ______ ________ ___ ____ _______ ________ __________ ________.
_______ _________ _____ _____ __________ ________ __________ ______ _____ ________ ________ ___.
______ _____ _______ ______ __________ __________ ________ ________ ______ _____.
_____ _____ ___ _____ _______ ____ _______ _________ ____ ______.
______ _______ _______ ____ ______ ____ ____ ________ ________ ____.
________ _________ __________ __________ _______ ____ _______ ____ _________ ________ _____.
_________ ______ _________ ________ _________ ____ _________ _________ _____ __________.
_________ ___ ______ __________ ________.
________ _____ ______ ______ __________ _________ ______ _____.
_____ ________ ___ _________ _________ _______.
____ ________ __________ _____ ____ _________ _______ ____.
__________ ___ ________ _______ ___ _____.
_________ ______ _____ __________ ___ ___ ___ _________.
____ _____ ___ ___ ___ ____ _____.
________ _______ ___ __________ _________ _________ ________ __________ _____.
______ __________ ______ _________ _____ ___ ______ __________ _______.
__________ _______ ______ _________ _______ __________ __________ _______ ______ __________ __________.
________ ______ _________ ____ __________ _______ ________.
_________ _____ ___ __________ _________ _________ _____ ________ ________.
________ ___ ____ _____ _______ ______.
________ _______ __________ _______ ______ _______ ________.
_________ ___ ______ ___ ____ _______ ________ _______ _______ __________.
__________ ___ _________ ____ _____ ________ ___ ____ _________ ________.
______ ________ _____ ______ ___ _________.
___ _______ _______ _______ _____ ___ ____ _______ ___ ______ _______.
_____ ___ __________ _____ ____ ________ ________ ____ ____.
_____ __________ ______ __________ _______ _______ __________ ________.
____ _______ _______ _____ ______ ____.
_____ ___ ____ __________ _________ _______ ________ __________ ___ _________.
_________ _________ _________ ______ ______ _______ ________ __________ _________ ___ _____.
__________ ________ ______ ___ _____ _______ ____ ___ ______ _________.
_______ ___ _____ _________ ________.
__________ ___ ___ __________ _____.
___ __________ __________ _________ __________.
___ ___ __________ ____ _______ _______ ___ _______.
____ ________ _______ _______ ____ __________ _______ _______ ____.
___ _________ ______ ___ _______ ____ _____ ___ __________ _________ ____ ______.
_______ ______ _____ ___ ________ _______ _______ ________ _________ ____ ________.
______ __________ _______ _____ _________.
________ __________ ___ ________ ___ _______ _____ _________ ____ _____ _______.
___ _____ _______ ______ ______ _________ __________.
_____ _______ ______ ____ _______.
_____ ___ _____ ____ ______ _________ ___ __________ _______.
________ _______ _____ _____ ___ ____ _________ _______ ____ _________.
___ ________ ____ _______ _______ ___ _______ _____ ___ _____ ________.
__________ ______ _________ _______ _________.
____ _________ __________ ____ _______ ___ _______ ________ _________ __________.
______ _____ _____ ___ ________.
__________ ____ _____ _____ ____ _____ _______ ______.
_____ _______ ___ _____ ____ _____.
__________ _________ _____ ___ ____ _________.
__________ ___ ______ __________ ______.
_________ ___ _________ __________ _____ _________ ____ ________ _________ _______ ______.
__________ ________ _________ ______ ____ ________ _____.
___ ____ ___ ____ _______.
__________ ______ __________ _____ ___ _______.
_________ ________ ____ ______ ____.
_____ _____ _________ _______ ________ ____.
_______ _____ _____ ____ ________ _________ ___ _____ ________.
_________ ___ _________ _______ ________ _____ __________ _______.
________ __________ ______ __________ _________.
________ __________ _______ _____ _____ ________ _________ ______.
__________ __________ ________ _________ ________ ________ ________ ___ ______.
_________ _______ ______ ___ ____ _____ _____ _______ ______ ________.
____ _________ _______ ______ ______.
_____ ______ ________ _______ ______ __________ ____ ________ ____ ___ ____.
_________ __________ ____ ________ ________ ____ ___ ________ _________ _______.
______ ___ __________ __________ _______.
________ ______ __________ ______ ___ ______ _________ _______.
__________ _________ _________ ___ ___.
____ ___ ________ _____ _________ _________ ________ __________.
___ _____ ___ _________ __________ _____ _______ ________ __________ _____ ___.
_____ ________ ________ ______ ________.
_______ ________ _____ ____ ___ _________ ___ ____.
_____ _________ ________ _________ ____ ______ ______ _____ _______ _____.
_______ ___ ________ ___ _______.
_____ __________ _______ _______ _______ ____ ________.
_______ _____ _____ _____ ________ _________ ________ __________.
_________ ___ _______ _________ _____ ______ ___ ____.
__________ _________ __________ ___ ___ _______.
___ ________ __________ _________ ___ _________ _____ ________ ___.
_____ ________ ______ ______ _____ ___ ___ _____ ____ ______ __________.
______ ______ ____ ___ ___ ____ _______ ________.
Get Full Answer on WhatsApp