JDBC - Part 1  «Prev  Next»


JDBC Interview Questions and Answers

  1. 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.

  2. 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");
        }
      }
    }
          



  3. 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.

  4. What are the common tasks when using JDBC?

    1. Obtain a connection (prefer DataSource or DriverManager).
    2. Create a statement (PreparedStatement is recommended).
    3. Execute SQL (executeQuery/executeUpdate/execute).
    4. Process the ResultSet if applicable.
    5. Manage transactions (setAutoCommit(false), commit()/rollback()).
    6. Close resources via try‑with‑resources.

  5. 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.


  6. 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.

  7. 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.

  8. 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.

  9. 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
    }
          

  10. What are the steps to establish a connection (modern approach)?

    1. Choose a driver (typically Type‑4) and add it to the classpath.
    2. Get a connection:
      • Standalone: DriverManager.getConnection(url, user, pass)
      • Jakarta EE: Inject a pooled DataSource via JNDI/@Resource
    3. 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;
          

Best Practices


SEMrush Software