Updated July 6, 2023
Introduction to JDBC Interview Questions and Answers
Java Database Connectivity (JDBC) is an API (Application Programming Interface) for the Java programming language. It is a data access technology to access and perform the database operations using Java programming language. It was developed by Oracle Corporation. It is a part of the Java Standard Edition (SE) platform. It is oriented with the relational database management system. It supports the cross-platform operating system. It is a kind of Data Access type of API. A JDBC API Guide will be provided by the Oracle Corporation to implement the JDBC features. JDBC connections will provide an interface to execute the queries for retrieving and updating the data from and to the database. There are different types of JDBC drivers such as Type 1, Type 2, Type 3, Type 4, etc. There will be also commercial and free drivers.
Below are the most important question asked in the interview:
Now, if you are looking for a job which is related to JDBC then you need to prepare for the JDBC Interview Questions for 2023. It is true that every interview is different as per the different job profiles. Here, we have prepared the important JDBC Interview Questions and Answers for 2023 which will help you get success in your interview. These questions are divided into two parts are as follows:
Part 1 – JDBC Interview Questions (Basic)
This first part covers basic Interview Questions and Answers.
Q1. What is a JDBC?
Answer:
JDBC is a Java API library for establishing a connection between a database and a java application. The database will be normally a relational database management system. JDBC uses drivers to establish connection and JDBC library will be used to call the database connections. JDBC driver will be the interface between Java application and database. It provides connection to any kind of tabular data, especially relational or structural data.
Q2. What are the steps involved in making JDBC connectivity?
Answer:
This is the basic JDBC Interview Questions asked in an interview. The different steps in establishing a JDBC connection are –
- Loading Driver: This is the first step where a specific and suitable JDBC driver will be loaded to establish a communication with the database.
- Connection: This step will get the connection from the database using a connection object which will send the statements to execute the queries and will return the result objects where the result of the query will be stored.
- Create Statement: The statement object can be collected from Collection object to obtain the result from the database.
- Execute Query: The query can be executed using the statement object to query the database and retrieve the result.
- Close Connection: The database connection will be closed once after the result set is received from the database.
Q3. What are the different types of Statements in JDBC?
Answer:
There are different statements in the JDBC API to retrieve the result set based on different requirements. They are Statement, Prepared Statement, and Callable Statement :
- Statement: In this type of statement, the result set can be retrieved during the time of runtime and it does not require any parameters to be passed.
Example:-
Statement statementObject = conn.createStatement( );
ResultSet resultSetObject = stmt.executeQuery();
- Prepared Statement: In this type of statement, the query can be executed as many times as needed or frequently whenever required in the application and it also allows in taking the parameters.
Example:-
String query = "Update Employee SET type= ? WHERE empId = ?"; PreparedStatement psObject = connObj.prepareStatement(query);
ResultSet rsObject = psObject.executeQuery();
- Callable Statement: In this type of statement, the query can be executed using stored procedures and also takes parameters during runtime.
Example:-
CallableStatement csObject = connObject.prepareCall("{call EMPLOYEE_DETAILS}");
ResultSet rsObject = csObject.executeQuery();
Let us move to the next JDBC Interview Questions.
Q4. What are the different types of JDBC drivers?
Answer:
The below is the list of different JDBC drivers used to connect to the database. They are Type 1, Type 2, Type 3 and Type 4 drivers:
- Type 1: The Type 1 database is also called as JDBC – ODBC bridge driver. In this type, the ODBC driver will be used to establish a connection to the database.
- Type 2: The Type 2 database is also called as Native API or Partial Java JDBC driver. In this type, client-side libraries will be used by the driver to connect to the database.
- Type 3: The Type 3 database is also called as Network protocol Pure Java JDBC driver. In this type, application server acts as a middleware or interface which converts JDBC calls directly to the database calls.
- Type 4: The Type 4 database is also called Native Protocol Pure Java JDBC driver. In this type, thin driver exists which converts the database calls to vendor-specific database protocols to connect to the database.
Q5. What are the different JDBC components used?
Answer:
The different components used in the JDBC API to connect to the database are as below:
- PreparedStatement
- CallableStatement
- Query
- ResultSet
- ResultSetMetadata
- DatabaseMetadata
- Connection
- DriverManager
Note: – Metadata is data about the data.
Part 2 – JDBC Interview Questions (Advanced)
Let us now have a look at the advanced Interview Questions.
Q6. What are the different methods used to query the database?
Answer:
The different methods used to query the database are execute(), executeQuery() and executeUpdate().
- execute(): This method is used to execute a query for any type of SQL statements. This return type is of Boolean i.e. true or false.
- executeQuery(): This method is used to execute the queries such as an only SELECT type of statements. Its return type is of an object such as ResultSet object.
- executeUpdate(): This method is used to execute the queries which perform operations such as INSERT, UPDATE, DELETE etc., Its return type is of integer i.e. 0 or 1.
Q7. What are the different types of locks in JDBC?
Answer:
This is the advanced Interview Questions asked in an interview. The different types of locks in JDBC are as below:
- Key and Row Locks
- Table Locks
- Page Locks
- Database Locks
Q8. What is a ResultSet?
Answer:
A ResultSet is an object which contains the data that is retrieved from the database using an SQL query. ResultSet always maintains a cursor that points to a row in the database table.
Q9. What is Connection Pooling in Database?
Answer:
The Connection Pooling in the database is the process of storing the connection in Cache which can be reused later. It provides faster connections and easier to troubleshoot the issues.
Q10. What are the different types of exceptions and errors in JDBC?
Answer:
The different types of exceptions while making a JDBC connection are as below:
- SQLException
- BatchUpdateException
- Data Truncation Error
- SQLWarning
Recommended Articles
We hope that this EDUCBA information on “JDBC Interview Questions” was beneficial to you. You can view EDUCBA’s recommended articles for more information.