![]() |
VOOZH | about |
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.
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.
We use JDBC to perform database operations directly from Java code. It helps Java applications store, retrieve, update, and delete data from relational databases.
JDBC follows a standard workflow for connecting and executing queries. These steps are common in almost every JDBC program.
JDBC drivers are classified into four types based on implementation. Type 4 is widely used because it is pure Java and efficient.
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.
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.
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.
ResultSet represents the table-like data returned after executing a SELECT query. It maintains a cursor to move through the rows of the result.
ResultSet stays connected to the database, while RowSet can be disconnected and serialized. RowSet is easier to transfer between classes.
Stored procedures support different parameter modes to pass input and receive output. JDBC supports these through CallableStatement methods.
Stored procedures are executed using CallableStatement. It supports IN, OUT, and INOUT parameters for passing and returning values.
JDBC works using a few important classes and interfaces. These components help establish connection, execute queries, and process results.
Type 4 (Thin Driver) is mostly used in real-world applications. It directly connects to the database using pure Java and provides better performance.
Connection is an interface that represents a connection between Java application and database. It is used to create statements and manage transactions.
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.
JDBC throws exceptions mainly related to SQL execution, connection issues, and batch operations. These help identify DB errors clearly.
JDBC supports two-tier and three-tier architecture models. Two-tier connects directly, while three-tier uses an application server in between.
Locking prevents multiple users from updating the same row simultaneously. It ensures data is not lost due to concurrent updates.
Transaction management ensures multiple SQL statements execute as one unit and maintain consistency. It helps commit changes or rollback in case of failure.
JDBC provides built-in methods to control transactions. These methods help developers manage commits, rollbacks, and savepoints.
Isolation levels define how one transaction is separated from another transaction. Higher isolation prevents inconsistent reads but reduces concurrency.
A table can be created by executing a CREATE TABLE query using executeUpdate(). This is useful in admin tools or dynamic DB applications.
executeQuery() is used to execute SELECT statements. It returns a ResultSet object which contains the data.
JDBC follows a standard workflow for connecting and executing queries. These steps are common in almost every JDBC program.
Class.forName() is used to load the JDBC driver class manually. In older JDBC versions it was required, but now drivers are auto-loaded.
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.
Batch processing means executing multiple SQL statements together in one batch. It improves performance because it reduces database calls.
Connection pooling is a technique where database connections are reused instead of creating new ones repeatedly. It improves performance and reduces server load.
Auto-commit is the default mode where every SQL query is committed automatically. If you want manual transaction control, you must disable it.
Databases use BLOB and CLOB for large objects. BLOB stores binary data like images, while CLOB stores large text content.
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.