A list of frequently asked jdbc interview questions with answers are given below.
1) What is JDBC?
JDBC is a Java API that is used to connect and execute query to the database. JDBC API uses jdbc drivers to connects to the database.
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)
3) What are the steps to connect to the database in java?
- Registering 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.
- CallableStatement etc.
- SQLException etc.
5) What are the JDBC statements?
There are 3 JDBC statements.
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.