Database and Web Fundamentals· 40 min

SQL, JDBC, and Transactions

Connect Java to relational databases using prepared statements, pooling, transactions, and isolation levels.

Java interview lesson
Question 13 · Not reviewed

Database · persistence

Can you explain this clearly in an interview?

Use the strong answer as your reference, explain it aloud in your own words, then choose how soon you want to review it again.

Interview prompt

SQL, JDBC, and Transactions

Explain it aloud in your own words

Strong answer

JDBC gives Java explicit control over connections, statements, results, and transactions. Use prepared statements for parameters, close resources deterministically, keep transaction boundaries intentional, and understand the database isolation level rather than assuming every transaction is serializable.

Key points

  • PreparedStatement prevents injection and allows database plan reuse.
  • A connection pool limits expensive database connections; it does not remove the need for timeouts.
  • Commit and rollback should be owned by a clear service boundary.
  • Isolation is a trade-off between anomalies, concurrency, and performance.

Interviewer looks for

Connect Java code to database behavior: connection lifecycle, transaction ownership, isolation, locks, and failure handling.

Java example

1try (Connection connection = dataSource.getConnection();
2     PreparedStatement statement = connection.prepareStatement("select id from users where email = ?")) {
3  statement.setString(1, email);
4  try (ResultSet result = statement.executeQuery()) {
5    return result.next() ? result.getLong(1) : null;
6  }
7}

Topic interview questions

Revise this topic like an interviewer

These questions are tied to this lesson. Read each question aloud, then compare your explanation with the strong answer.

5 questions
Q1Intermediate

Why use PreparedStatement instead of string concatenation?

Strong answer

PreparedStatement binds values separately from SQL text, preventing injection and often allowing the database to reuse a query plan. It also handles type conversion more safely.

Q2Intermediate

Where should a transaction boundary usually live?

Strong answer

It should surround a coherent business use case so all required database changes commit or roll back together. Putting it around arbitrary repository calls can create partial updates or unnecessarily long locks.

Q3Intermediate

What does transaction isolation control?

Strong answer

Isolation controls which effects of concurrent transactions are visible, trading anomalies such as dirty or non-repeatable reads against concurrency and performance. The database's actual behavior depends on its engine and configuration.

Q4Intermediate

Why use a connection pool?

Strong answer

Creating database connections is expensive and database servers have finite connection capacity. A pool reuses connections and enforces limits, but callers still need timeouts, validation, and correct close behavior.

Q5Intermediate

What happens if a JDBC ResultSet or Connection is not closed?

Strong answer

Resources can remain occupied, exhausting the pool or database server and causing unrelated requests to fail. Try-with-resources should own the lifecycle of statements, result sets, and connections.

How well do you know this?

Your review choice is saved locally and synced to your learner profile when signed in.