Lesson content
Read, practise, then check your understanding
JDBC standardizes relational database access. Prefer a configured pooled DataSource; close connections, statements, and result sets with try-with-resources.
Parameters and transactions
String sql = "select name from users where id = ?";
try (Connection connection = dataSource.getConnection();
PreparedStatement statement = connection.prepareStatement(sql)) {
statement.setLong(1, id);
try (ResultSet rows = statement.executeQuery()) {
if (rows.next()) return rows.getString("name");
}
}
Prepared statements separate values from SQL and prevent injection. For multi-step work, disable auto-commit, commit success, and roll back failure. Define isolation, timeouts, batch behavior, null mapping, and generated-key handling. Never share a Connection across concurrent operations unless explicitly supported.
Knowledge check
Answer every question correctly to complete this chapter.
0 of 10 checks passed
Your progress is saved on this device.