Preparing for a JDBC (Java Database Connectivity) interview can be challenging, given the depth and breadth of the topic. Whether you’re a fresher aiming to break into Java development or an experienced professional seeking to brush up your knowledge, this guide covers the most commonly asked questions to help you succeed.
📌 Benefits of Reviewing JDBC Interview Questions
- Comprehensive Coverage: Understand core concepts and advanced topics.
- Confidence Boost: Familiarity with questions enhances self-assurance.
- Efficient Preparation: Focused study saves time and effort.
- Career Advancement: Demonstrates readiness for database-related roles.
- Skill Validation: Reinforces your knowledge and identifies areas for improvement.
🔍 Top 50 JDBC Interview Questions and Answers
1. What is JDBC?
JDBC stands for Java Database Connectivity. It’s a Java API that enables Java applications to interact with relational databases by executing SQL statements.
2. What are the main components of JDBC?
- DriverManager: Manages database drivers.
- Driver: Interface for database drivers.
- Connection: Establishes a connection with the database.
- Statement: Executes SQL queries.
- ResultSet: Represents the result set of a query.
- SQLException: Handles SQL errors.
3. What is a JDBC Driver?
A JDBC Driver is a software component that enables Java applications to interact with a database. There are four types:
- Type 1: JDBC-ODBC Bridge Driver.
- Type 2: Native-API Driver.
- Type 3: Network Protocol Driver.
- Type 4: Thin Driver (Pure Java Driver).
4. How do you establish a JDBC connection?
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/dbname", "user", "password");
5. What is the difference between Statement and PreparedStatement?
- Statement: Used for executing static SQL statements.
- PreparedStatement: Used for executing precompiled SQL statements with parameters, offering better performance and security.
6. What is ResultSet in JDBC?
ResultSet is an object that holds the data retrieved from a database after executing an SQL query using Statement or PreparedStatement.
7. What are the types of ResultSet?
- TYPE_FORWARD_ONLY: Cursor moves forward only.
- TYPE_SCROLL_INSENSITIVE: Cursor can move both forward and backward; changes made to the database after the ResultSet was created are not visible.
- TYPE_SCROLL_SENSITIVE: Cursor can move both forward and backward; changes made to the database after the ResultSet was created are visible.
8. What is the difference between execute(), executeQuery(), and executeUpdate()?
- execute(): Executes any SQL statement.
- executeQuery(): Executes SELECT statements.
- executeUpdate(): Executes INSERT, UPDATE, DELETE statements.
9. What is batch processing in JDBC?
Batch processing allows grouping multiple SQL statements into a batch and executing them together, improving performance.
10. How do you handle transactions in JDBC?
con.setAutoCommit(false);
// Execute SQL statements
con.commit(); // or con.rollback();
11. What is Savepoint in JDBC?
Savepoint allows setting a point within a transaction to which you can later roll back.
12. What is the role of DriverManager in JDBC?
DriverManager manages a list of database drivers and establishes a connection between a Java application and the database.
13. How do you register a JDBC driver?
Class.forName("com.mysql.cj.jdbc.Driver");
14. What is the difference between java.sql.Date and java.util.Date?
- java.sql.Date: Represents SQL DATE value.
- java.util.Date: Represents both date and time.
15. What are the advantages of using PreparedStatement?
- Prevents SQL injection.
- Improves performance through precompilation.
- Allows dynamic parameter binding.
16. What is connection pooling?
Connection pooling is a technique to maintain a cache of database connections for reuse, improving performance.
17. What is RowSet in JDBC?
RowSet is a wrapper around ResultSet that adds support for JavaBeans components and can be disconnected from the database.
18. What are the types of RowSet?
- JdbcRowSet
- CachedRowSet
- WebRowSet
- FilteredRowSet
- JoinRowSet
19. What is the use of getGeneratedKeys() method?
Retrieves auto-generated keys created by executing an INSERT statement.
20. How do you handle exceptions in JDBC?
By catching SQLException and using methods like getMessage(), getSQLState(), and getErrorCode() to retrieve error details.
21. What is the difference between CallableStatement and PreparedStatement?
- CallableStatement: Used to execute stored procedures.
- PreparedStatement: Used to execute parameterized SQL queries.
22. How do you call a stored procedure in JDBC?
CallableStatement cs = con.prepareCall("{call procedure_name(?, ?)}");
23. What is the use of setFetchSize() and setMaxRows()?
- setFetchSize(): Suggests the number of rows to fetch from the database when more rows are needed.
- setMaxRows(): Limits the number of rows returned by a query.
24. What is JDBC transaction management?
It allows grouping multiple SQL statements into a single transaction, ensuring data integrity.
25. What is the default isolation level in JDBC?
The default isolation level is determined by the database, commonly READ_COMMITTED.
26. What are the types of JDBC drivers?
- Type 1: JDBC-ODBC Bridge Driver.
- Type 2: Native-API Driver.
- Type 3: Network Protocol Driver.
- Type 4: Thin Driver (Pure Java Driver).
27. What is the difference between TYPE_SCROLL_INSENSITIVE and TYPE_SCROLL_SENSITIVE ResultSet?
- TYPE_SCROLL_INSENSITIVE: Does not reflect changes made to the database after the ResultSet was created.
- TYPE_SCROLL_SENSITIVE: Reflects changes made to the database after the ResultSet was created.
28. What is the use of setAutoCommit(false)?
Disables the default auto-commit mode, allowing manual transaction control.
29. What is a DataSource in JDBC?
DataSource is an alternative to DriverManager for establishing database connections, often used for connection pooling.
30. How do you perform batch updates in JDBC?
Statement stmt = con.createStatement();
stmt.addBatch("INSERT INTO table_name ...");
stmt.executeBatch();
31. What is the use of clearBatch()?
Clears all the commands added to the batch.
32. What is the difference between executeUpdate() and executeBatch()?
- executeUpdate(): Executes a single SQL statement.
- executeBatch(): Executes a batch of SQL statements.
33. What is the use of setNull() in PreparedStatement?
Sets a parameter to SQL NULL.
34. What is the difference between getConnection() and getConnection(String url)?
- getConnection(): Uses default connection parameters.
- getConnection(String url): Uses the specified URL to establish a connection.
35. What is the use of commit() and rollback()?
- commit(): Saves all changes made since the previous commit.
- rollback(): Undoes all changes made since the previous commit.
36. What is the difference between Statement and CallableStatement?
- Statement: Executes static SQL statements.
- CallableStatement: Executes stored procedures.
37. What is the use of setTransactionIsolation()?
Sets the isolation level for transactions.
38. What are the isolation levels in JDBC?
- TRANSACTION_READ_UNCOMMITTED
- TRANSACTION_READ_COMMITTED
- TRANSACTION_REPEATABLE_READ
- TRANSACTION_SERIALIZABLE
39. What is the use of getWarnings() in JDBC?
Retrieves the first warning reported by calls on this Connection object.
40. What is the difference between execute() and executeQuery()?
- execute(): Can execute any SQL statement.
- executeQuery(): Executes SELECT statements and returns a ResultSet.
41. What is the use of setCursorName()?
Sets the name of the SQL cursor that will be used by this Statement object.
42. What is the difference between absolute() and relative() in ResultSet?
- absolute(int row): Moves the cursor to the specified row number.
- relative(int rows): Moves the cursor a relative number of rows.
43. What is the use of isClosed() in Connection?
Checks whether the connection is closed.
44. What is the difference between close() and isClosed()?
- close(): Closes the connection.
- isClosed(): Checks if the connection is closed.
45. What is the use of getMetaData() in ResultSet?
Retrieves the number, types, and properties of the columns in a ResultSet.
46. What is the use of getColumnCount() in ResultSetMetaData?
Returns the number of columns in the ResultSet.
47. What is the use of getColumnName() in ResultSetMetaData?
Returns the name of the specified column.
48. What is the use of getColumnType() in ResultSetMetaData?
Returns the SQL type of the specified column.
49. What is the use of getColumnTypeName() in ResultSetMetaData?
Returns the database-specific type name of the specified column.