Java j2EE (28) 

Welcome to our JDBC Interview Questions and Answers Page!

We have compiled a comprehensive collection of frequently asked JDBC interview questions and their detailed answers for you. Whether you are a beginner or an experienced professional, this resource will help you ace your JDBC interviews. Good luck and happy learning!

Top 20 Basic JDBC interview questions and answers

1. What is JDBC?
JDBC stands for Java Database Connectivity. It is a Java API that provides a standard way to interact with relational databases.

2. What are the key interfaces provided by JDBC?
The key interfaces provided by JDBC are Connection, Statement, ResultSet, and PreparedStatement.

3. What are the steps to establish a JDBC connection?
The steps to establish a JDBC connection are as follows:
– Load the JDBC driver class using `Class.forName()`.
– Create a connection using the `getConnection()` method of the `DriverManager` class.

4. What is the difference between a Statement and PreparedStatement?
A `Statement` is used to execute a static SQL query, while a `PreparedStatement` is used to execute a dynamic SQL query. PreparedStatement provides better performance and security.

5. What is the difference between execute(), executeQuery(), and executeUpdate() methods?
The `execute()` method is used to execute any SQL statement and it returns a boolean value indicating the type of result (true for ResultSet, false for update count). `executeQuery()` is used to execute SELECT statements and it returns a ResultSet object. `executeUpdate()` is used to execute INSERT, UPDATE, DELETE statements and it returns the count of affected rows.

6. What is a ResultSet?
A `ResultSet` in JDBC represents the result of a SQL query. It is a table of data representing a database result set, which is usually generated by executing a SELECT statement.

7. How to retrieve data from a ResultSet?
Data can be retrieved from a ResultSet using methods like `getString()`, `getInt()`, `getDouble()`, etc., by passing the column index or column name as a parameter.

8. What is the purpose of the JDBC DriverManager class?
The `DriverManager` class is responsible for managing JDBC drivers. It provides methods to connect to a database, manage JDBC drivers, and obtain connection objects.

9. What is the role of the JDBC driver?
A JDBC driver is a software component that allows Java applications to interact with the database. It handles the low-level communication with the database server.

10. What are the different types of JDBC drivers?
The different types of JDBC drivers are:
– JDBC-ODBC Bridge Driver
– Native-API/Partly Java Driver
– Network Protocol Driver
– Thin Driver

11. How to handle exceptions in JDBC?
In JDBC, exceptions can be handled using try-catch blocks or by throwing exceptions to the calling method using the `throws` keyword.

12. What is the use of the `setAutoCommit()` method?
The `setAutoCommit()` method is used to enable or disable the auto-commit mode. When auto-commit is enabled, each SQL statement is treated as a separate transaction and committed automatically. When disabled, you can commit multiple SQL statements as a single transaction.

13. What is a transaction?
A transaction is a sequence of one or more SQL operations treated as a single unit of work. It ensures the consistency and integrity of the database.

14. How to handle transactions in JDBC?
Transactions can be handled in JDBC by invoking the `commit()` method to commit the changes made in a transaction or invoking the `rollback()` method to undo the changes if an error occurs.

15. What is connection pooling in JDBC?
Connection pooling is a technique used to manage a pool of database connections that can be reused. It helps in avoiding the overhead of establishing a new database connection for every request.

16. How to retrieve metadata from a ResultSet?
Metadata from a ResultSet can be retrieved using methods like `getMetaData()`, which returns a `ResultSetMetaData` object containing information about the columns of the ResultSet.

17. What is a JDBC batch update?
A JDBC batch update allows you to group multiple SQL statements into a single batch and send them to the database server for execution. It improves performance by reducing the number of round trips to the database.

18. Explain the use of the `ResultSet.TYPE_SCROLL_INSENSITIVE` type.
The `ResultSet.TYPE_SCROLL_INSENSITIVE` type allows scrolling backward and forward through the ResultSet. It is insensitive to changes made by others while the ResultSet is still open.

19. What is an SQL injection attack?
An SQL injection attack is a type of security vulnerability where an attacker can manipulate the SQL queries sent to a database by injecting malicious SQL code. It can lead to unauthorized access or data manipulation.

20. How to handle NULL values in JDBC?
NULL values can be handled in JDBC using the `wasNull()` method to check if the last retrieved value was NULL and appropriate handling logic based on the result.

Top 20 Advanced JDBC interview questions and answers

1. What is JDBC?
JDBC (Java Database Connectivity) is a Java API for connecting and executing queries on a database. It provides a standard set of classes and interfaces for accessing relational databases.

2. What are the different types of JDBC drivers?
There are four types of JDBC drivers:
– Type 1: JDBC-ODBC bridge driver
– Type 2: Native-API/partly Java driver
– Type 3: All Java/net-protocol driver
– Type 4: All Java/native-protocol driver

3. What is the purpose of the DriverManager class?
The DriverManager class is responsible for managing multiple database drivers. It is used to establish a connection with the database by loading the appropriate driver.

4. What is the role of the java.sql package?
The java.sql package provides the necessary classes and interfaces for JDBC programming. It includes classes for database connectivity, executing SQL statements, and retrieving results.

5. What is a PreparedStatement?
PreparedStatement is a pre-compiled SQL statement that is stored in the database, saving compilation time for subsequent executions. It allows parameterized queries and is more efficient than Statement for executing multiple queries.

6. What is a ResultSet?
A ResultSet represents the result of a database query. It is used to retrieve the data returned by a SELECT statement. The ResultSet provides methods to iterate over the result set and retrieve the values of individual columns.

7. What is the difference between Statement and PreparedStatement?
Statement is used for executing static SQL queries, while PreparedStatement is used for executing parameterized queries. PreparedStatement provides better performance by precompiling the SQL statement.

8. How can you handle transactions in JDBC?
JDBC supports transactions through the Connection object. You can set the auto-commit mode to false and group multiple SQL statements within a transaction block. Use commit() to commit the transaction or rollback() to undo changes.

9. What is connection pooling in JDBC?
Connection pooling is the process of reusing database connections instead of creating a new connection for every user request. It improves performance by avoiding the overhead of establishing a new connection each time.

10. How can you retrieve auto-generated keys in JDBC?
You can retrieve auto-generated keys by passing an additional argument when creating a PreparedStatement. Use the getGeneratedKeys() method of the Statement or PreparedStatement object to retrieve the keys.

11. What are the advantages of using a PreparedStatement over a Statement?
Advantages of PreparedStatement over Statement include:
– Precompilation of SQL statement improves performance.
– Protection against SQL injection attacks.
– Better code readability and maintenance.

12. How can you handle BLOB and CLOB data in JDBC?
BLOB (Binary Large Object) and CLOB (Character Large Object) data types can be handled using the getBlob() and getClob() methods of the ResultSet class. These methods return the respective objects that can be used to read and manipulate large data.

13. What is the use of batch processing in JDBC?
Batch processing in JDBC allows executing multiple SQL statements as a single batch. It reduces the number of database round-trips, improving performance. Use the addBatch() method to add SQL statements to the batch, and executeBatch() to execute them.

14. How can you handle database metadata in JDBC?
The DatabaseMetaData interface provides methods to retrieve metadata (information about the database, tables, columns, etc.) from a connection object. Use getMetaData() method of the Connection or Statement object to obtain the DatabaseMetaData instance.

15. What is the purpose of the ResultSetMetaData interface?
ResultSetMetaData provides methods for retrieving metadata about the columns in a ResultSet. It can be used to determine the column names, types, sizes, etc. Use getMetaData() method of the ResultSet object to obtain the ResultSetMetaData instance.

16. What is a connection URL in JDBC?
A connection URL is a string that specifies the address and other parameters required to establish a connection to a database. It typically includes the database type, server address, port, database name, and authentication details.

17. How can you handle SQL exceptions in JDBC?
SQL exceptions can be caught and handled using try-catch blocks. JDBC provides a hierarchy of exception classes in the java.sql package, such as SQLException, SQLTimeoutException, etc. You can catch specific exceptions and perform appropriate error handling.

18. How can you improve JDBC performance?
Some techniques to improve JDBC performance include:
– Using PreparedStatement instead of Statement for parameterized queries.
– Employing connection pooling to avoid establishing new connections.
– Using batch processing for executing multiple SQL statements.
– Fetching only the required data, instead of retrieving the entire result set.

19. How can you retrieve a specific number of rows in JDBC?
JDBC does not provide a direct way to retrieve a specific number of rows. However, you can achieve this by using the LIMIT or OFFSET clause in your SQL query. The exact syntax may vary depending on the database you are using.

20. Can you use JDBC with non-relational databases?
JDBC is primarily designed for relational databases. However, some NoSQL databases provide JDBC drivers to enable connectivity and query execution. Examples include Apache Cassandra and MongoDB.

Java j2EE (28) 

Interview Questions and answers

Filter:AllUnanswered
What is a connection pool in JDBC and why is it important to use one?
suresh answered 3 months ago • 
57 views1 answers0 votes
What are the different types of JDBC drivers available in Java?
suresh answered 7 months ago • 
57 views1 answers0 votes
How does transaction management work in JDBC?
suresh answered 7 months ago • 
64 views1 answers0 votes
How do you handle transactions in JDBC?
suresh answered 7 months ago • 
61 views1 answers0 votes
How do you handle database transactions in JDBC?
suresh answered 7 months ago • 
54 views1 answers0 votes