Ensure data integrity by grouping multiple SQL statements into a single atomic transaction.
A database transaction is a sequence of operations performed as a single logical unit of work. The key property of a transaction is that it is atomic, meaning either all of the operations within it are successfully completed, or none of them are. If any operation fails, the entire transaction is 'rolled back', and the database is returned to the state it was in before the transaction began. This is crucial for maintaining data integrity. A classic example is transferring money between two bank accounts. This involves two separate UPDATE operations: debiting one account and crediting another. If the debit succeeds but the credit fails (e.g., due to a system crash), you would have lost money. By wrapping both operations in a transaction, you guarantee that both must succeed for the changes to be made permanent. In JDBC, transaction control is managed through the `Connection` object. By default, JDBC connections are in auto-commit mode, meaning each individual SQL statement is treated as its own transaction and is automatically committed upon execution. To manage transactions manually, you must first disable auto-commit by calling `conn.setAutoCommit(false);`. After this, you can execute a series of SQL statements. If all statements execute without error, you make the changes permanent by calling `conn.commit();`. If an error occurs at any point, you can revert all changes made since the transaction began by calling `conn.rollback();` in a `catch` block.