JDBC Interview Questions

 


JDBC Interview Questions


1- What is JDBC?

Java JDBC is a java API to connect and execute query with the database. JDBC API uses jdbc drivers to connect with the database.



Why use JDBC- Before JDBC, ODBC API was the database API to connect and execute query with the database. But, ODBC API uses ODBC driver which is written in C language (i.e. platform dependent and unsecured). That is why Java has defined its own API (JDBC API) that uses JDBC drivers (written in Java language).

2- What is JDBC Driver?

JDBC Driver is a software component that enables java application to interact with the database.There are 4 types of JDBC drivers: 
JDBC-ODBC bridge driver 
Native-API driver (partially java driver) 
Network Protocol driver (fully java driver) 
Thin driver (fully java driver) 

1) JDBC-ODBC bridge driver
The JDBC-ODBC bridge driver uses ODBC driver to connect to the database. The JDBC-ODBC bridge driver converts JDBC method calls into the ODBC function calls. This is now discouraged because of thin driver. 

 
Advantages: 
easy to use. 
can be easily connected to any database. 

Disadvantages: 
Performance degraded because JDBC method call is converted into the ODBC function calls. 
The ODBC driver needs to be installed on the client machine. 

2) Native-API driver
The Native API driver uses the client-side libraries of the database. The driver converts JDBC method calls into native calls of the database API. It is not written entirely in java. 



Advantage: 
performance upgraded than JDBC-ODBC bridge driver. 

Disadvantage: 
The Native driver needs to be installed on the each client machine. 
The Vendor client library needs to be installed on client machine. 

3) Network Protocol driver
The Network Protocol driver uses middleware (application server) that converts JDBC calls directly or indirectly into the vendor-specific database protocol. It is fully written in java.



Advantage: 
No client side library is required because of application server that can perform many tasks like auditing, load balancing, logging etc. 

Disadvantages: 
Network support is required on client machine. 
Requires database-specific coding to be done in the middle tier. 
Maintenance of Network Protocol driver becomes costly because it requires database-specific coding to be done in the middle tier. 

4) Thin driver
The thin driver converts JDBC calls directly into the vendor-specific database protocol. That is why it is known as thin driver. It is fully written in Java language. 



Advantage: 
Better performance than all other drivers. 
No software is required at client side or server side. 

Disadvantage: 
Drivers depends on the Database. 

3- What are the steps to connect to the database in java?

There are 5 steps to connect any java application with the database in java using JDBC. They are as follows: 
Register the driver class 
Creating connection 
Creating statement 
Executing queries 
Closing connection 

4- What are the JDBC API components?

The java.sql package contains interfaces and classes for JDBC API.

Interfaces: 
Connection 
Statement 
PreparedStatement 
ResultSet 
ResultSetMetaData 
DatabaseMetaData 
CallableStatement etc. 

Classes: 
DriverManager 
Blob 
Clob 
Types 
SQLException etc. 

5- What are the JDBC statements?
There are 3 JDBC statements. 
Statement 
PreparedStatement 
CallableStatement 

6- What is the difference between Statement and PreparedStatement interface?
In case of Statement, query is complied each time whereas in case of PreparedStatement, query is complied only once. So performance of PreparedStatement is better than Statement.

7- How can we execute stored procedures and functions?
By using Callable statement interface, we can execute procedures and functions.

8- What is the role of JDBC DriverManager class?
The DriverManager class manages the registered drivers. It can be used to register and unregister drivers. It provides factory method that returns the instance of Connection.

9- What does the JDBC Connection interface?
The Connection interface maintains a session with the database. It can be used for transaction management. It provides factory methods that returns the instance of Statement, PreparedStatement, CallableStatement and DatabaseMetaData.

10- What does the JDBC ResultSet interface?
The ResultSet object represents a row of a table. It can be used to change the cursor pointer and get the information from the database.

11- What does the JDBC ResultSetMetaData interface?
The ResultSetMetaData interface returns the information of table such as total number of columns, column name, column type etc.

12- What does the JDBC DatabaseMetaData interface?
The DatabaseMetaData interface returns the information of the database such as username, driver name, driver version, number of tables, number of views etc.

13- Which interface is responsible for transaction management in JDBC?
The Connection interface provides methods for transaction management such as commit(), rollback() etc.

14- What is batch processing and how to perform batch processing in JDBC?
By using batch processing technique in JDBC, we can execute multiple queries. It makes the performance fast.

15- How can we store and retrieve images from the database?
By using PreparedStatement interface, we can store and retrieve images.

 

Post a Comment

0 Comments