- What is JDBC?
JDBC (Java Database Connectivity) is the standard Java API for working with relational databases.
It provides a uniform set of interfaces so you can write database‑agnostic code:
connect, execute SQL, handle transactions, and read results without hard‑wiring vendor specifics.
- What are the two major components of JDBC?
- JDBC API — Interfaces/classes in
java.sql
and javax.sql
(e.g., Connection
, PreparedStatement
,
ResultSet
, DataSource
) for connections, SQL execution, and results.
- JDBC Driver — A vendor‑specific implementation that speaks a database’s protocol and implements the JDBC interfaces.
Modern drivers are Type‑4 (pure Java).
try (Connection c = DriverManager.getConnection(url, user, pass);
PreparedStatement ps = c.prepareStatement("SELECT id, name FROM users WHERE status = ?")) {
ps.setString(1, "ACTIVE");
try (ResultSet rs = ps.executeQuery()) {
while (rs.next()) {
int id = rs.getInt("id");
String name = rs.getString("name");
}
}
}
- What does a JDBC driver actually implement?
A JDBC driver registers itself (JDBC 4+ via Service Provider), implements java.sql.Driver
,
and provides concrete classes for interfaces like Connection
, Statement
, PreparedStatement
,
CallableStatement
, and ResultSet
.
- What are the common tasks when using JDBC?
- Obtain a connection (prefer
DataSource
or DriverManager
).
- Create a statement (
PreparedStatement
is recommended).
- Execute SQL (
executeQuery
/executeUpdate
/execute
).
- Process the
ResultSet
if applicable.
- Manage transactions (
setAutoCommit(false)
, commit()
/rollback()
).
- Close resources via try‑with‑resources.
- How do I connect to Microsoft Access with JDBC today?
The legacy JDBC‑ODBC bridge is removed from modern JDKs. Use a Type‑4 driver such as a third‑party
Access driver (e.g., UCanAccess) or a commercial JDBC‑ODBC bridge. Example URL pattern (driver‑specific):
// Example (driver-specific)
String url = "jdbc:ucanaccess://C:/data/mydb.accdb";
try (Connection c = DriverManager.getConnection(url)) {
// use the connection
}
In enterprise apps, prefer a pooled DataSource
configured by the container/app server.
- What are the four JDBC driver types, and which is used now?
- Type 1: JDBC‑ODBC bridge (obsolete/removed).
- Type 2: Native‑API (requires native libs; rare today).
- Type 3: Middleware/Network protocol driver (rare).
- Type 4: Thin driver (pure Java; standard in modern apps).
Use a Type‑4 driver whenever possible.
- Which packages does JDBC use?
java.sql
— core JDBC interfaces (Connection, DriverManager, Statement, PreparedStatement, ResultSet, etc.).
javax.sql
— optional extensions (DataSource, ConnectionPoolDataSource, RowSet) used heavily in Jakarta EE.
Tip: With JDBC 4+, drivers auto‑register; no need for Class.forName()
in most cases.
- What are the three main JDBC statement interfaces?
- Statement — ad‑hoc SQL; avoid for dynamic values.
- PreparedStatement — parameterized SQL; prevents SQL injection and can be precompiled.
- CallableStatement — calls stored procedures with IN/OUT parameters.
- What is the typical JDBC flow?
URL → getConnection → (setAutoCommit/tx) → PreparedStatement → executeQuery/executeUpdate → ResultSet → commit/rollback
try (Connection c = ds.getConnection()) {
c.setAutoCommit(false);
try (PreparedStatement ps = c.prepareStatement("UPDATE accounts SET balance = balance - ? WHERE id = ?")) {
ps.setBigDecimal(1, amount);
ps.setLong(2, fromId);
ps.executeUpdate();
}
// ... second statement for destination account ...
c.commit();
} catch (SQLException e) {
// rollback if needed
}
- What are the steps to establish a connection (modern approach)?
- Choose a driver (typically Type‑4) and add it to the classpath.
- Get a connection:
- Standalone:
DriverManager.getConnection(url, user, pass)
- Jakarta EE: Inject a pooled
DataSource
via JNDI/@Resource
- Use try‑with‑resources for automatic cleanup.
// Jakarta EE example
import jakarta.annotation.Resource;
import javax.sql.DataSource;
@Resource(lookup = "java:jboss/datasources/MyDS")
private DataSource ds;