JDBC Interview Questions and Answers
Freshers / Beginner level questions & answers
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.
- Register the jdbc driver with the driver manager
- Establish jdbc connection
- Execute an sql statement
- Process the results
- Close the connection
- 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?
- 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.
Ques 6. How can you make the connection?
To establish a connection you need to have the appropriate driver connect to the DBMS.
String url = "jdbc:odbc:WithoutBook";Connection con = DriverManager.getConnection(url, "Username", "Password");
Ques 7. How can you create JDBC statements and what are they?
A Statement object is what sends your SQL statement to the DBMS. You simply create a Statement object and then execute it, supplying the appropriate execute method with the SQL statement you want to send.
Statement stmt = con.createStatement();
Ques 8. How can you retrieve data from the ResultSet?
JDBC returns results in a ResultSet object, so we need to declare an instance of the class ResultSet to hold our results. The following code demonstrates declaring the ResultSet object:
ResultSet rs = stmt.executeQuery("SELECT NAME,SAL FROM EMPLOYEE");
String s = rs.getString("NAME");
The method getString is invoked on the ResultSet object (rs), so getString() will retrieve (get) the value stored in the column NAME in the current row of rs. Note: rs is ResultSet variable name here.
Ques 9. What are the different types of Statements?
- Regular statement (use createStatement method)
- prepared statement (use prepareStatement method)
- callable statement (use prepareCall)
Ques 10. How can you use PreparedStatement?
This special type of statement is derived from class Statement. If you need a Statement object to execute many times, it will normally make sense to use a PreparedStatement object instead.
Ques 11. What does setAutoCommit do?
When a connection is created, it is in auto-commit mode. This means that each individual SQL statement is treated as a transaction and will be automatically committed right after it is executed. The way to allow two or more statements to be grouped into a transaction is to disable auto-commit mode:
con.setAutoCommit(false);
Once auto-commit mode is disabled, no SQL statements will be committed until you call the method commit explicitly.
con.setAutoCommit(false);
PreparedStatement updateSales =
con.prepareStatement( "UPDATE EMPLOYEE SET SAL = ? WHERE EMP_NAME LIKE ?");
updateSales.setInt(1, 50000); updateSales.setString(2, "Arindam");
updateSales.executeUpdate();
PreparedStatement updateTotal =
con.prepareStatement("UPDATE EMPLOYEE SET TOTAL = TOTAL + ? WHERE EMP_NAME LIKE ?");
updateTotal.setInt(1, 50000);
updateTotal.setString(2, "Arindam");
updateTotal.executeUpdate();
con.commit();
con.setAutoCommit(true);
Ques 12. How do you call a stored procedure from JDBC?
The first step is to create a CallableStatement object. As with Statement an and PreparedStatement objects, this is done with an open
Connection object. A CallableStatement object contains a call to a stored procedure.
CallableStatement cs = con.prepareCall("{call SHOW_EMPLOYEES}");
ResultSet rs = cs.executeQuery();
Ques 13. What are the steps involved in establishing a JDBC connection?
involves two steps: loading the JDBC driver and making the connection.
Ques 14. What is JDBC Driver?
The JDBC Driver provides vendor-specific implementations of the abstract classes provided by the JDBC API. This driver is used to connect to the database.
Ques 15. What are the different JDB drivers available?
There are mainly four type of JDBC drivers available. They are:
Type 1 : JDBC-ODBC Bridge Driver - A JDBC-ODBC bridge provides JDBC API access via one or more ODBC drivers. Note that some ODBC native code and in many cases native database client code must be loaded on each client machine that uses this type of driver. Hence, this kind of driver is generally most appropriate when automatic installation and downloading of a Java technology application is not important. For information on the JDBC-ODBC bridge driver provided by Sun.
Type 2: Native API Partly Java Driver- A native-API partly Java technology-enabled driver converts JDBC calls into calls on the client API for Oracle, Sybase, Informix, DB2, or other DBMS. Note that, like the bridge driver, this style of driver requires that some binary code be loaded on each client machine.
Type 3: Network protocol Driver- A net-protocol fully Java technology-enabled driver translates JDBC API calls into a DBMS-independent net protocol which is then translated to a DBMS protocol by a server. This net server middleware is able to connect all of its Java technology-based clients to many different databases. The specific protocol used depends on the vendor. In general, this is the most flexible JDBC API alternative. It is likely that all vendors of this solution will provide products suitable for Intranet use. In order for these products to also support Internet access they must handle the additional requirements for security, access through firewalls, etc., that the Web imposes. Several vendors are adding JDBC technology-based drivers to their existing database middleware products.
Type 4: JDBC Net pure Java Driver - A native-protocol fully Java technology-enabled driver converts JDBC technology calls into the network protocol used by DBMSs directly. This allows a direct call from the client machine to the DBMS server and is a practical solution for Intranet access. Since many of these protocols are proprietary the database vendors themselves will be the primary source for this style of driver. Several database vendors have these in progress.
Ques 16. What are the different JDB drivers available?
There are mainly four type of JDBC drivers available. They are:
Type 1 : JDBC-ODBC Bridge Driver - A JDBC-ODBC bridge provides JDBC API access via one or more ODBC drivers. Note that some ODBC native code and in many cases native database client code must be loaded on each client machine that uses this type of driver. Hence, this kind of driver is generally most appropriate when automatic installation and downloading of a Java technology application is not important. For information on the JDBC-ODBC bridge driver provided by Sun.
Type 2: Native API Partly Java Driver- A native-API partly Java technology-enabled driver converts JDBC calls into calls on the client API for Oracle, Sybase, Informix, DB2, or other DBMS. Note that, like the bridge driver, this style of driver requires that some binary code be loaded on each client machine.
Type 3: Network protocol Driver- A net-protocol fully Java technology-enabled driver translates JDBC API calls into a DBMS-independent net protocol which is then translated to a DBMS protocol by a server. This net server middleware is able to connect all of its Java technology-based clients to many different databases. The specific protocol used depends on the vendor. In general, this is the most flexible JDBC API alternative. It is likely that all vendors of this solution will provide products suitable for Intranet use. In order for these products to also support Internet access they must handle the additional requirements for security, access through firewalls, etc., that the Web imposes. Several vendors are adding JDBC technology-based drivers to their existing database middleware products.
Type 4: JDBC Net pure Java Driver - A native-protocol fully Java technology-enabled driver converts JDBC technology calls into the network protocol used by DBMSs directly. This allows a direct call from the client machine to the DBMS server and is a practical solution for Intranet access. Since many of these protocols are proprietary the database vendors themselves will be the primary source for this style of driver. Several database vendors have these in progress.
Ques 17. What is the fastest type of JDBC driver?
Type 4 (JDBC Net pure Java Driver) is the fastest JDBC driver. Type 1 and Type 3 drivers will be slower than Type 2 drivers (the database calls are make at least three translations versus two), and Type 4 drivers are the fastest (only one translation).
Ques 18. Is the JDBC-ODBC Bridge multi-threaded?
No. The JDBC-ODBC Bridge does not support multi threading. The JDBC-ODBC Bridge uses synchronized methods to serialize all of the calls that it makes to ODBC. Multi-threaded Java programs may use the Bridge, but they won't get the advantages of multi-threading.
Ques 19. How do you handle your own transaction?
Connection Object has a method called setAutocommit ( boolean flag) . For handling our own transaction we can set the parameter to false and begin your transaction . Finally commit the transaction by calling the commit method.
Most helpful rated by users:
- What is JDBC? Describe the steps needed to execute a SQL query using JDBC.
- What are the steps involved in establishing a JDBC connection?
- Is JDBC-ODBC bridge multi-threaded?
- Does the JDBC-ODBC Bridge support multiple concurrent open statements per connection?
- What are the different driver types available in JDBC?