Advanced Java Interview Questions – JDBC
1. [Asked in TCS] What is JDBC?
Answer:
Java Database Connectivity (JDBC) is an API in Java that enables Java applications to interact with databases. JDBC allows Java applications to connect, send SQL queries, update data, and fetch data from databases in a simple and standardized way.
2. [Asked in Infosys] Explain the basic steps to connect Java with a database using JDBC.
Answer:
Steps include:
- Load JDBC Driver:
Class.forName("driver_class"); - Create Connection:
Connection con = DriverManager.getConnection(url, user, pass); - Create Statement:
Statement stmt = con.createStatement(); - Execute Query:
ResultSet rs = stmt.executeQuery(query); - Process Results: Retrieve data from result set.
- Close Connection:
con.close();
3. [Asked in Cognizant] What is a JDBC Driver? Why is it required?
Answer:
A JDBC driver enables Java applications to interact with databases. It translates Java API calls into database-specific calls, allowing Java applications to communicate with various databases seamlessly.
4. [Asked in Infosys] Can you list different types of JDBC drivers?
Answer:
There are four JDBC driver types:
- Type 1 (JDBC-ODBC Bridge Driver) โ platform dependent.
- Type 2 (Native API driver) โ Uses native libraries installed on the machine.
- Type 3 (Network Protocol Driver) โ Uses middleware or server.
- Type 4 (Thin Driver) โ Pure Java driver, database-independent.
5. [Asked in Wipro] What is the difference between Statement and PreparedStatement in JDBC?
Answer:
| Statement | PreparedStatement |
|---|---|
| Used for static SQL queries. | Used for dynamic queries with parameters. |
| Vulnerable to SQL injection attacks. | Provides protection against SQL injection. |
| Slower performance with repeated queries. | Faster due to pre-compilation and caching. |
6. [Asked in Cognizant] What is ResultSet in JDBC?
Answer:
A ResultSet is a JDBC interface used to store the results obtained after executing a query. It allows Java programs to iterate through database records retrieved by SELECT queries.
7. [Asked in Amazon] Explain briefly the difference between executeQuery(), executeUpdate(), and execute() methods.
Answer:
executeQuery(): Executes SELECT queries; returnsResultSet.executeUpdate(): Executes INSERT, UPDATE, DELETE queries, returns number of affected rows.execute(): Executes any SQL query and returns true if the result is a ResultSet object or false otherwise.
8. [Asked in IBM] Why do we need JDBC connection pooling?
Answer:
JDBC connection pooling improves application performance by reusing existing database connections. It avoids the overhead of repeatedly opening and closing connections, resulting in faster database interactions and efficient resource usage.
9. [Asked in Microsoft] What is ResultSet in JDBC? How can you move through it?
Answer:
A ResultSet is an object representing data retrieved from the database. You can navigate through it using methods like:
next(): moves to next row.previous(): moves to previous row (if ResultSet is scrollable).first(),last()to navigate quickly through records.
10. [Asked in Deloitte] Mention a common problem faced with JDBC programming and its solution.
Answer:
A common JDBC issue is memory leaks caused by not closing ResultSet, Statement, or Connection objects properly.
Solution: Always close these objects explicitly in the finally block or use try-with-resources to handle automatic closing.
11. [Asked in Flipkart] What do you understand by JDBC Transactions? Why are they important?
Answer:
A JDBC transaction groups multiple SQL operations into a single unit. Transactions ensure that either all operations complete successfully (commit) or none of them do (rollback). This maintains data consistency and integrity.
