JDBC Interview Questions and Answers for freshers

100 JDBC Interview Questions and Answers for Freshers
  1. What is JDBC?

    • Answer: JDBC (Java Database Connectivity) is an API for connecting Java applications to relational databases. It provides a set of classes and interfaces to execute SQL statements, retrieve results, and manage database connections.
  2. What are the core interfaces of JDBC?

    • Answer: The core interfaces include `DriverManager`, `Connection`, `Statement`, `PreparedStatement`, `CallableStatement`, and `ResultSet`.
  3. Explain the role of `DriverManager` in JDBC.

    • Answer: `DriverManager` is responsible for managing database drivers. It establishes a connection to the database based on the provided JDBC URL, username, and password.
  4. What is a JDBC driver?

    • Answer: A JDBC driver is a software component that allows Java applications to connect to and interact with a specific database system (e.g., MySQL, Oracle, PostgreSQL).
  5. What are the different types of JDBC drivers?

    • Answer: There are four types: Type 1 (JDBC-ODBC Bridge), Type 2 (Native-API partly Java), Type 3 (Net-protocol all Java), and Type 4 (Native-protocol all Java).
  6. Explain the `Connection` interface.

    • Answer: The `Connection` interface represents a session with a specific database. It provides methods for creating `Statement`, `PreparedStatement`, and `CallableStatement` objects to execute SQL queries.
  7. What is the purpose of `Statement` interface?

    • Answer: The `Statement` interface is used to execute static SQL statements. It's simple but less efficient for repeated queries with varying parameters.
  8. What is `PreparedStatement` and why is it preferred over `Statement`?

    • Answer: `PreparedStatement` is used to execute pre-compiled SQL statements. It's more efficient than `Statement` for repeated queries with varying parameters because the database pre-compiles the statement only once.
  9. Explain `CallableStatement`.

    • Answer: `CallableStatement` is used to execute stored procedures in the database. It allows passing parameters to and receiving results from stored procedures.
  10. What is the `ResultSet` interface?

    • Answer: `ResultSet` holds the results of a database query. It's a table of data, and you can iterate through it using methods like `next()`, `getString()`, `getInt()`, etc.
  11. How do you handle database exceptions in JDBC?

    • Answer: Use `try-catch` blocks to handle `SQLException` and its subclasses. Properly close connections and resources within `finally` blocks to prevent resource leaks.
  12. Explain the concept of transactions in JDBC.

    • Answer: Transactions ensure atomicity, consistency, isolation, and durability (ACID properties) for database operations. They group multiple SQL statements into a single unit of work, either all succeeding or all rolling back in case of failure.
  13. How do you commit and rollback transactions in JDBC?

    • Answer: Use `connection.commit()` to save changes and `connection.rollback()` to undo changes made within a transaction.
  14. What is connection pooling? Why is it used?

    • Answer: Connection pooling is a technique of creating and managing a pool of database connections. It improves performance by reusing existing connections instead of creating new ones for each request, reducing the overhead of connection establishment.
  15. How do you handle `SQLException` in JDBC? Give an example.

    • Answer:
  16. How to execute an UPDATE statement using JDBC? Provide code example.

    • Answer: [Code example demonstrating UPDATE statement execution with error handling and resource closing]
  17. Difference between `executeUpdate()` and `executeQuery()` methods?

    • Answer: `executeUpdate()` is used for INSERT, UPDATE, and DELETE statements, returning the number of rows affected. `executeQuery()` is for SELECT statements, returning a `ResultSet`.
  18. Explain the concept of auto-commit in JDBC.

    • Answer: Auto-commit is a setting that determines whether changes are automatically saved to the database after each SQL statement. It's usually disabled for transactions.
  19. How to use metadata in JDBC?

    • Answer: JDBC metadata provides information about the database, tables, columns, etc. You can access this information using methods like `DatabaseMetaData`, `ResultSetMetaData`, etc.
  20. What are stored procedures? How to call them using JDBC?

    • Answer: Stored procedures are pre-compiled SQL code stored in the database. They are called using `CallableStatement` in JDBC.
  21. Explain the importance of closing database resources.

    • Answer: Closing connections, statements, and result sets is crucial to release database resources and prevent connection leaks, which can impact performance and stability.
  22. What is a batch update in JDBC?

    • Answer: Batch updates allow you to execute multiple SQL statements as a single unit, improving efficiency.
  23. How to use scrollable result sets in JDBC?

    • Answer: Scrollable result sets allow you to navigate through the result set in any order (forward, backward).
  24. Explain different types of ResultSet concurrency.

    • Answer: Types include `ResultSet.CONCUR_READ_ONLY` and `ResultSet.CONCUR_UPDATABLE`.

Thank you for reading our blog post on 'JDBC Interview Questions and Answers for freshers'.We hope you found it informative and useful.Stay tuned for more insightful content!