HuntExams Academy logo
JavaBeginner#jdbc

What is JDBC and what are its main components?

Answer

Java Database Connectivity is an API for connecting Java applications to relational databases. Key components: DriverManager (obtains connections), Connection (session with the DB), Statement/PreparedStatement (executes SQL), and ResultSet (holds query results).

Example

Connection conn = DriverManager.getConnection(url, user, pass);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM users");

Detailed explanation

JDBC stands for Java Database Connectivity. It is the standard Java API for connecting to databases, executing SQL, and processing results. Most commonly, it connects Java applications to relational databases such as MySQL, PostgreSQL, and Oracle Database. JDBC provides common interfaces, so application code follows a similar structure across database products. However, connection URLs, SQL dialects, and supported features can differ. Its core types are available in java.sql, with additional facilities in javax.sql.

Under the hood, a database-specific JDBC driver implements these interfaces and translates JDBC calls into communication with the database. DriverManager selects a suitable registered driver and obtains a Connection through getConnection(url, user, password). Modern JDBC drivers normally register automatically when their driver JAR is available at runtime. A Connection represents a session with the database and provides methods for creating statements and controlling transactions, including commit() and rollback(). In production applications, a DataSource is commonly used to obtain connections, often through a connection pool.

Statement executes SQL supplied as a string. PreparedStatement represents parameterised SQL, using question-mark placeholders whose values are supplied through methods such as setString() or setInt(). This separates parameter values from SQL syntax, helping prevent SQL injection when untrusted input is bound correctly. CallableStatement supports stored procedure calls. For a SELECT query, executeQuery() returns a ResultSet: a cursor-based view of query results, not necessarily a collection containing every row in memory. Its cursor initially sits before the first row; next() advances it, and getter methods read column values. For INSERT, UPDATE, or DELETE, executeUpdate() normally returns the affected-row count.

In the example, DriverManager obtains the connection, conn.createStatement() creates the SQL execution object, and executeQuery() returns the users query results. The next steps are to iterate over rs and release resources. Use try-with-resources to close ResultSet, Statement, and Connection reliably, and handle SQLException appropriately. Interviewers usually expect this execution flow, clear responsibilities for each component, and awareness that the JDBC API and the database driver are different things.

Key points to remember

  • JDBC is Java’s standard database-access API; a database-specific driver implements it.
  • DriverManager obtains a Connection, which represents a database session and supports transaction control.
  • Statement executes SQL; PreparedStatement binds parameter values; CallableStatement invokes stored procedures.
  • ResultSet exposes query rows through a cursor advanced with next().
  • Use try-with-resources for cleanup and SQLException for JDBC error handling.

Common mistakes candidates make

  • Calling JDBC a database or a driver instead of distinguishing the API from its driver implementation.
  • Concatenating user input into SQL instead of binding values through PreparedStatement.
  • Reading ResultSet values before calling next(), or forgetting to close JDBC resources.

Likely follow-up questions

How is PreparedStatement different from Statement?

Statement executes SQL strings, while PreparedStatement supports SQL with placeholders and separately bound values. Prefer PreparedStatement for user-supplied values because binding keeps those values separate from SQL syntax; performance benefits depend on the driver and database.

What is the difference between executeQuery() and executeUpdate()?

executeQuery() is used for SQL that returns a single ResultSet, typically SELECT. executeUpdate() returns an update count for operations such as INSERT, UPDATE, and DELETE, or zero for statements that return no count.

How do you manage transactions with JDBC?

Connections normally start in auto-commit mode, where each statement is committed as its own transaction when it completes. Call setAutoCommit(false) to group operations, then use commit() on success or rollback() on failure.

Interview tip: Define JDBC first, then explain the flow DriverManager → Connection → Statement or PreparedStatement → ResultSet using one SELECT query.

Related Interview Questions

1
JavaBeginner#jdbc

What is the difference between Statement and PreparedStatement?

Open
2
JavaIntermediate#jdbc

What is a CallableStatement used for?

Open
3
JavaIntermediate#jdbc

What is connection pooling and why is it important?

Open