JDBC Interview Questions and Answers for 10 years experience
-
What is JDBC?
- Answer: JDBC (Java Database Connectivity) is an API for Java that allows Java applications to interact with relational databases. It provides a standard way to execute SQL statements, retrieve results, and manage database connections.
-
Explain the different JDBC drivers.
- Answer: JDBC supports four types of drivers: Type 1 (JDBC-ODBC Bridge), Type 2 (Native-API partly Java), Type 3 (Network Protocol), and Type 4 (Pure Java). Type 4 is generally preferred for its portability and performance.
-
What is the role of DriverManager class?
- Answer: The `DriverManager` class is responsible for managing connections to databases. It provides methods to register drivers, establish connections using connection strings, and retrieve connections.
-
How do you establish a database connection using JDBC?
- Answer: You establish a connection using `DriverManager.getConnection(url, username, password)`, where the URL specifies the database type, location, and name.
-
Explain the significance of Connection, Statement, and ResultSet interfaces.
- Answer: `Connection` represents a database connection. `Statement` executes SQL queries. `ResultSet` holds the results of a query, allowing you to iterate through the data.
-
What is PreparedStatement and its advantages?
- Answer: `PreparedStatement` is a pre-compiled SQL statement. It improves performance by preventing SQL injection vulnerabilities and reusing the compiled statement for multiple executions with different parameters.
-
What is CallableStatement?
- Answer: `CallableStatement` is used to execute stored procedures in the database.
-
How do you handle exceptions in JDBC?
- Answer: JDBC exceptions are handled using `try-catch` blocks, typically catching `SQLException` and its subclasses.
-
Explain the importance of connection pooling.
- Answer: Connection pooling improves performance by reusing database connections instead of creating new ones for each request. This reduces the overhead of connection establishment.
-
Name some popular connection pooling libraries.
- Answer: Apache Commons DBCP, HikariCP, c3p0 are popular choices.
-
How do you handle transactions in JDBC?
- Answer: Transactions are managed using the `Connection` object's methods like `setAutoCommit(false)`, `commit()`, and `rollback()`. This ensures data integrity.
-
What are the different transaction isolation levels?
- Answer: Read Uncommitted, Read Committed, Repeatable Read, and Serializable. Each level offers varying degrees of concurrency and data consistency.
-
What is the difference between `getInt()` and `getObject()` in ResultSet?
- Answer: `getInt()` retrieves data as an integer. `getObject()` retrieves data as a generic object, allowing for more flexible type handling.
-
How do you handle large result sets efficiently?
- Answer: Use `Scrollable ResultSet` to navigate efficiently. For extremely large datasets, consider processing data in batches or using stream processing techniques.
-
Explain the use of metadata in JDBC.
- Answer: DatabaseMetaData provides information about the database itself (e.g., tables, columns, procedures). ResultSetMetaData provides information about a specific ResultSet.
-
How do you prevent SQL injection attacks?
- Answer: Always use parameterized queries (`PreparedStatement`) to avoid direct string concatenation of user inputs into SQL statements.
-
What are stored procedures, and how do you call them using JDBC?
- Answer: Stored procedures are pre-compiled SQL code stored in the database. They are called using `CallableStatement`.
-
Explain the concept of row locking and its implications.
- Answer: Row locking prevents concurrent modifications of the same row, ensuring data consistency but potentially impacting concurrency.
-
Describe different types of JDBC batch updates.
- Answer: JDBC supports adding multiple SQL statements to a batch using `addBatch()` and executing them together with `executeBatch()`, significantly improving performance.
-
How to handle BLOB and CLOB data types using JDBC?
- Answer: BLOB (Binary Large Object) and CLOB (Character Large Object) data are handled using specialized methods provided by `ResultSet` and `PreparedStatement`.
-
What is optimistic locking and pessimistic locking?
- Answer: Optimistic locking assumes conflicts are rare and checks for changes before committing. Pessimistic locking assumes conflicts are frequent and locks rows proactively.
-
How to optimize JDBC performance?
- Answer: Use connection pooling, prepared statements, batch updates, efficient queries, and appropriate transaction isolation levels.
-
Explain the concept of a cursor in JDBC.
- Answer: Cursors allow you to iterate through a ResultSet more than once, accessing rows in different orders, or accessing specific rows.
-
What is the difference between `executeUpdate()` and `executeQuery()`?
- Answer: `executeUpdate()` is used for INSERT, UPDATE, and DELETE statements, returning the number of rows affected. `executeQuery()` is used for SELECT statements, returning a ResultSet.
-
How do you close JDBC resources properly?
- Answer: Use a `finally` block to ensure that `ResultSet`, `Statement`, and `Connection` objects are closed to release database resources, even if exceptions occur. Consider using try-with-resources for automatic resource management.
-
What are the advantages of using a framework like Spring JDBC over raw JDBC?
- Answer: Spring JDBC simplifies JDBC operations, handling resource management, exception handling, and providing features like template classes for cleaner code and better maintainability.
-
Explain how you would handle different database types (e.g., MySQL, Oracle, PostgreSQL) with JDBC.
- Answer: You need the appropriate JDBC driver for each database type. The connection URL changes to specify the database system. The core JDBC API remains the same, allowing for relative ease of switching between different database systems.
Thank you for reading our blog post on 'JDBC Interview Questions and Answers for 10 years experience'.We hope you found it informative and useful.Stay tuned for more insightful content!