JDBC (Java Database Connectivity) Interview Questions

Last Updated : 12 Feb, 2026

JDBC (Java Database Connectivity) is an important topic in Advanced Java interviews. It is used to connect Java applications with databases and perform operations like insert, update, delete, and select. Most freshers and experienced candidates are asked JDBC questions because it is a core part of backend development.

  • Covers JDBC architecture and workflow
  • Includes commonly asked interview questions for freshers and experienced candidates
  • Helps you understand database connectivity in real-time Java applications
jdbc-

1. What does JDBC stand for, and why is it used?

JDBC (Java Database Connectivity) is a Java API that helps Java applications connect with relational databases. It allows executing SQL queries and handling database data in Java programs.

  • Used for CRUD operations
  • Works with SQL databases like MySQL/Oracle
  • Uses JDBC drivers for connection
JDBC-Architecture

2. Why do we use JDBC?

We use JDBC to perform database operations directly from Java code. It helps Java applications store, retrieve, update, and delete data from relational databases.

  • Connect Java with the database
  • Execute SQL queries
  • Perform CRUD operations

3. What are the steps to connect JDBC?

JDBC follows a standard workflow for connecting and executing queries. These steps are common in almost every JDBC program.

  1. import package
  2. Load and register drivers
  3. Establish connection
  4. Create and Execute statement
  5. Retrieve the results
  6. Close the connection
jdbcSteps

4. What are the 4 JDBC driver types?

JDBC drivers are classified into four types based on implementation. Type 4 is widely used because it is pure Java and efficient.

jdbc_odbc_bridge

5. What is the role of a JDBC Driver?

A JDBC Driver is a library that enables communication between Java applications and a database. It converts JDBC method calls into database-specific protocol instructions.

  • Required to connect Java with DB
  • Different DB -> different driver
  • Provided by database vendors

6. What is DriverManager in JDBC?

DriverManager is a class that manages available JDBC drivers and helps in creating database connections. It selects the correct driver based on the JDBC URL.

  • Registers and loads drivers
  • Provides getConnection() method
  • Works with JDBC URLs

7. What is the difference between JDBC and ODBC?

ODBC is a general database API mostly built for native languages, while JDBC is designed specifically for Java. JDBC is platform-independent and performs better for Java applications.

  • JDBC is pure Java
  • ODBC depends on native drivers
  • JDBC is recommended for Java apps
JDBC-ODBC

8. What is a ResultSet in JDBC?

ResultSet represents the table-like data returned after executing a SELECT query. It maintains a cursor to move through the rows of the result.

  • Cursor starts before first row
  • next() is used for iteration
  • Data fetched using getXXX()

9. What is the difference between ResultSet and RowSet?

ResultSet stays connected to the database, while RowSet can be disconnected and serialized. RowSet is easier to transfer between classes.

  • ResultSet is connected
  • RowSet is disconnected
  • RowSet supports JavaBeans
resultrowset

10. What are stored procedure parameter types?

Stored procedures support different parameter modes to pass input and receive output. JDBC supports these through CallableStatement methods.

  • IN -> input only
  • OUT -> output only
  • INOUT -> both input + output

11. How can stored procedures be executed using JDBC?

Stored procedures are executed using CallableStatement. It supports IN, OUT, and INOUT parameters for passing and returning values.

  • Uses prepareCall()
  • Supports parameter binding
  • OUT params require registration

12. What are the main JDBC components?

JDBC works using a few important classes and interfaces. These components help establish connection, execute queries, and process results.

  • DriverManager
  • Connection
  • Statement / PreparedStatement
  • ResultSet

13. Which JDBC driver is mostly used in real projects?

Type 4 (Thin Driver) is mostly used in real-world applications. It directly connects to the database using pure Java and provides better performance.

  • Platform independent
  • Faster execution
  • Most widely supported

14. What is Connection in JDBC?

Connection is an interface that represents a connection between Java application and database. It is used to create statements and manage transactions.

  • Represents DB session
  • Used to create Statement objects
  • Used for commit/rollback

15. What is Statement in JDBC?

Statement is an interface used to execute simple SQL queries without parameters. It is mainly used when query is fixed and does not require dynamic input.

  • Executes normal SQL queries
  • Not secure for user input
  • Slower than PreparedStatement

16. What are common JDBC exceptions?

JDBC throws exceptions mainly related to SQL execution, connection issues, and batch operations. These help identify DB errors clearly.

  • SQLException -> base exception
  • BatchUpdateException -> batch failure
  • SQLWarning -> warning messages

17. What are the two main JDBC architectures?

JDBC supports two-tier and three-tier architecture models. Two-tier connects directly, while three-tier uses an application server in between.

  • Two-tier -> direct DB connection
  • Three-tier -> app server as middle layer
  • Three-tier used in enterprise apps

18. What is locking in JDBC?

Locking prevents multiple users from updating the same row simultaneously. It ensures data is not lost due to concurrent updates.

  • Optimistic locking checks at update time
  • Pessimistic locking locks immediately
  • Prevents lost updates
Lock

19. What is JDBC Transaction Management?

Transaction management ensures multiple SQL statements execute as one unit and maintain consistency. It helps commit changes or rollback in case of failure.

  • Supports ACID properties
  • commit() saves changes
  • rollback() cancels changes
acid_properties

20. What methods are used for handling transactions in JDBC?

JDBC provides built-in methods to control transactions. These methods help developers manage commits, rollbacks, and savepoints.

  • setAutoCommit(false)
  • commit() and rollback()
  • setSavepoint()

21. What are transaction isolation levels in JDBC?

Isolation levels define how one transaction is separated from another transaction. Higher isolation prevents inconsistent reads but reduces concurrency.

  • Prevents dirty reads
  • Controls phantom reads
  • Affects performance

22. How can a table be created dynamically in JDBC?

A table can be created by executing a CREATE TABLE query using executeUpdate(). This is useful in admin tools or dynamic DB applications.

  • Uses Statement object
  • Executes CREATE TABLE SQL
  • Table name can be user input

23. What is executeQuery()?

executeQuery() is used to execute SELECT statements. It returns a ResultSet object which contains the data.

  • Used only for SELECT
  • Returns ResultSet
  • Fetches database records

24. What are the steps to connect JDBC?

JDBC follows a standard workflow for connecting and executing queries. These steps are common in almost every JDBC program.

  1. import package
  2. Load and register drivers
  3. Establish connection
  4. Create and Execute statement
  5. Retrieve the results
  6. Close the connection

25. What is Class.forName() in JDBC?

Class.forName() is used to load the JDBC driver class manually. In older JDBC versions it was required, but now drivers are auto-loaded.

  • Loads driver class
  • Used in older versions
  • Optional in JDBC 4+

26. What is SQL Injection?

SQL Injection is a security issue where attackers inject SQL code through user input. This can allow them to access or modify database data illegally.

  • Security vulnerability
  • Happens in Statement
  • Prevented using PreparedStatement

27. What is batch processing in JDBC?

Batch processing means executing multiple SQL statements together in one batch. It improves performance because it reduces database calls.

  • Faster for multiple inserts
  • Reduces network calls
  • Used in bulk operations

28. What is Connection Pooling?

Connection pooling is a technique where database connections are reused instead of creating new ones repeatedly. It improves performance and reduces server load.

  • Reuses existing connections
  • Faster than creating new connection
  • Used in Spring Boot with HikariCP

29. What is auto-commit mode?

Auto-commit is the default mode where every SQL query is committed automatically. If you want manual transaction control, you must disable it.

  • Default is true
  • Commits after each query
  • Can be disabled

30. Which SQL types store images and large files?

Databases use BLOB and CLOB for large objects. BLOB stores binary data like images, while CLOB stores large text content.

  • BLOB -> image/audio/video
  • CLOB -> large text files
  • Used for large object storage

31. What is the difference between DriverManager and DataSource?

DriverManager creates a new connection every time, which is slower in large applications.DataSource is preferred because it supports connection pooling and is used in enterprise applications.

  • DriverManager = basic connection
  • DataSource = pooled connection
  • DataSource is faster and scalable
Comment
Article Tags:

Explore