A Connection between code and database
DriverManager opens a Connection between your code and the database. Connection is a java.sql interface; the driver implements it. The try-with-resources closes it; otherwise it is a lost resource.
DriverManager opens a Connection between your code and the database.
Connection is a java.sql interface; the driver implements it.
The try-with-resources closes it; otherwise it is a lost resource.
DriverManager opens a Connection between your code and the database. Connection is a java.sql interface; the driver implements it. The try-with-resources closes it; otherwise it is a lost resource.
The classic mistake: the email the user typed is concatenated into the SQL.
The database receives WHERE email = '' OR '1'='1', always true.
The data became code: it returns every user. That is SQL injection.
The classic mistake: the email the user typed is concatenated into the SQL. The database receives WHERE email = '' OR '1'='1', always true. The data became code: it returns every user. That is SQL injection.
With PreparedStatement, only the template travels first, with a ? for the value.
The database compiles it and fixes its structure: one parameter, and where it goes.
With PreparedStatement, only the template travels first, with a ? for the value. The database compiles it and fixes its structure: one parameter, and where it goes.
setString(1, email) sends the value apart, as data. Indexes start at 1.
The database looks for the literal email ' OR '1'='1 and finds nobody.
setString(1, email) sends the value apart, as data. Indexes start at 1. The database looks for the literal email ' OR '1'='1 and finds nobody.
A transfer is two dependent UPDATEs: debit $5000, then credit it.
With autocommit, the debit is confirmed at once. Then the network drops and the credit fails.
The money left one account and never reached the other.
A transfer is two dependent UPDATEs: debit $5000, then credit it. With autocommit, the debit is confirmed at once. Then the network drops and the credit fails. The money left one account and never reached the other.
Same balances, with setAutoCommit(false): both UPDATEs stay pending.
The credit fails again. The catch calls rollback() and the database undoes everything.
Same balances, with setAutoCommit(false): both UPDATEs stay pending. The credit fails again. The catch calls rollback() and the database undoes everything.
In another run, both the debit and the credit reach the database.
Only then does commit() confirm both together: $15000 and $8000.
The finally restores setAutoCommit(true): the connection is left as we found it.
In another run, both the debit and the credit reach the database. Only then does commit() confirm both together: $15000 and $8000. The finally restores setAutoCommit(true): the connection is left as we found it.
JDBC connects, and PreparedStatement with ? fixes the structure before the data exists.
A transaction is all or nothing: either both things happen, or neither.
JDBC connects, and PreparedStatement with ? fixes the structure before the data exists. A transaction is all or nothing: either both things happen, or neither.