JDBC (Java Database Connectivity) is the standard API for SQL databases from Java. Real apps use a JDBC driver (PostgreSQL, MySQL), connection pools (HikariCP), and often an ORM like JPA/Hibernate on top.
Typical flow
- Load driver / use JDBC 4+ auto-loading
DriverManager.getConnection(url, user, pass)PreparedStatementwith?placeholdersexecuteQuery/executeUpdate- Close in try-with-resources
Playground simulation
This lesson prints mock row data—mirrors what ResultSet iteration would show. Never concatenate user input into SQL; always bind parameters.
Important interview questions and answers
- Q: Statement vs PreparedStatement?
A: PreparedStatement binds parameters safely and caches execution plans—prevents SQL injection. - Q: JDBC vs JPA?
A: JDBC is low-level SQL; JPA maps objects to tables with less boilerplate.
Self-check
- Why use placeholders instead of string concatenation?
- What interface represents a SQL query result row cursor?
Tip: Always bind SQL parameters with PreparedStatement—never concatenate user input into query strings.
Interview prep
- PreparedStatement vs Statement?
PreparedStatement binds parameters safely and efficiently—prevents SQL injection and avoids parsing SQL on every execution.