JDBC Interview Questions and Answers
JDBC vs Hibernate | JDBC vs JPA | JDBC 3.0 vs JDBC 4.0 |
Ques 1. What is JDBC? Describe the steps needed to execute a SQL query using JDBC.
We can connect to databases from java using JDBC. It stands for Java DataBase Connectivity.
Here are the steps:
- Register the jdbc driver with the driver manager
- Establish jdbc connection
- Execute an sql statement
- Process the results
- Close the connection
Before doing these do import java.sql.* JDBC is java based API for accessing data from the relational databases. JDBC provides a set of classes and interfaces for doing various database operations.
The steps are:- Register/load the jdbc driver with the driver manager.
- Establish the connection thru DriverManager.getConnection();
- Fire a SQL thru conn.executeStatement();
- Fetch the results in a result set.
- Process the results.
- Close statement/result set and connection object.
Ques 2. How many different types of JDBC drivers are present? Discuss them.
There are four JDBC driver types.
Type 1: JDBC-ODBC Bridge plus ODBC Driver:
The first type of JDBC driver is the JDBC-ODBC Bridge. It is a driver that provides JDBC access to databases through ODBC drivers. The ODBC driver must be configured on the client for the bridge to work. This driver type is commonly used for prototyping or when there is no JDBC driver available for a particular DBMS.
Type 2: Native-API partly-Java Driver:
The Native to API driver converts JDBC commands to DBMS-specific native calls. This is much like the restriction of Type 1 drivers. The client must have some binary code loaded on its machine. These drivers do have an advantage over Type 1 d'rivers because they interface directly with the database.
Type 3: JDBC-Net Pure Java Driver:
The JDBC-Net drivers are a three-tier solution. This type of driver translates JDBC calls into a database-independent network protocol that is sent to a middleware server. This server then translates this DBMS-independent protocol into a DBMS-specific protocol, which is sent to a particular database. The results are then routed back through the middleware server and sent back to the client. This type of solution makes it possible to implement a pure Java client. It also makes it possible to swap databases without affecting the client.
Type 4: Native-Protocol Pure Java Driver:
These are pure Java drivers that communicate directly with the vendor's database. They do this by converting JDBC commands directly into the database engine's native protocol. This driver has no additional translation or middleware layer, which improves performance tremendously.
What does the "static" keyword mean in front of a variable or a method or a class?
static variable:
- Means a class level variable
static method:
-Does not have "this". It is not allowed to access the not static members of the class.
can be invoked enev before a single instance of a class is created.
eg: main
static class:
No such thing.
static free floating block:
It is executed at the time the class is loaded. There can be multiple such blocks. This may be useful to load native libraries when using native methods.
eg:
native void doThis(){
static{
System.loadLibrary("myLibrary.lib");
}
Ques 3. What are the different driver types available in JDBC?
- A JDBC-ODBC bridge
- A native-API partly Java technology-enabled driver
- A net-protocol fully Java technology-enabled driver
- A native-protocol fully Java technology-enabled driver
Ques 4. How can you load the drivers?
Loading the driver or drivers you want to use is very simple and involves just one line of code. If, for example, you want to use the JDBC-ODBC Bridge driver, the following code will load it:
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Your driver documentation will give you the class name to use. For instance, if the class name is jdbc.DriverXYZ, you would load the driver with the following line of code:
Class.forName("jdbc.DriverXYZ");
Ques 5. What will Class.forName do while loading drivers?
It is used to create an instance of a driver and register it with the DriverManager. When you have loaded a driver, it is available for making a connection with a DBMS.