JDBC commonly known as the Java Database Connectivity is an API (Application Programming Interface) for java. JDBC is an abstraction that has been provided to us by SunMicrosystems and implemented by Database vendors. We use this to connect the java app with the database. JDBC provides some classes and interfaces by which we do connection the java applications with databases. JDBC API has two packages which include java.sql and javax.sql. These packages have classes and interfaces.
In this article, we’ll discuss some important questions with their answers related to JDBC. Once you go through this article, I can assure you that this will boost your knowledge to excel in your interview.
JDBC Interview Questions and answers
Some important questions asked in the interview are as below:
What is JDBC?
Answer: JDBC is a Java API (Application Programming Interface) that we use to create a link between java and other databases. It uses JDBC drivers to reach the database. We can also use this to access any tabular data stored in a database, relational or structural data. Different JDBC drivers include JDBC-ODBC bridge driver, Native, Network Protocol, and Thin. These drivers are of two types to both commercial and free versions.
Describe a general JDBC Architecture.
Answer: JDBC Architecture has layers like the JDBC Application program interface (For the application-to-JDBC Manager connection) and JDBC Driver API (For the JDBC Manager-to-Driver connection).
Define a JDBC Driver?
Answer: JDBC Driver is a boundary enabling a Java application to make a connection with a database. JDBC requires drivers for each database to make a connection with individual databases.
Name the different types of JDBC drivers available for use? Explain in detail.
Answer: Different JDBC drivers include JDBC-ODBC bridge driver, Native, Network Protocol, and Thin.
JDBC-ODBC bridge driver: This bridge driver uses the ODBC driver to reach the database. The ODBC Driver converts ODBC calls into DBMS Specific Calls and redirects the request to Database. The JDBC-ODBC bridge that is available with ‘JDK 1.2’ is a good sample of this kind of driver. One of the advantages is it can be easily connected to any database since the database’s ODBC drivers are easily accessible.
Type 2 or Native-API Driver: It uses vendor-specific APIs to connect to the database. It needs to install in the machine for every user. We shall use it to change the JDBC calls into Native Calls. Performance-wise it is preferred to JDBC-ODBC bridge driver.
Type 3 or Network Protocol Driver: Network Protocol Driver is designed purely for enterprise applications and is suitable for standalone applications. The portability of this driver is better than both the JDBC-ODBC bridge driver and Native drivers. It will provide a better environment to interact with multiple databases. Switching from one database to another database is very easy without having any modifications in client applications.
Type 4 or Thin Driver: This driver uses the database-specific native protocol to communicate with the database. Thin driver changes the Calls JCBC into Database specific calls in a direct manner. This driver is developed only in java. Hence, it is purely a java driver. It is a platform-independent driver.
What are the well-known JDBC API components?
Answer: JDBC API has the following interfaces and classes DriverManager, Driver, Connection, Statement, ResultSet, SQLException. The DriverManager JDBC is a class that handles a list of drivers for the database. It sends connection requests from the java API with the proper database driver with the help of a communication subprotocol.
Name the fastest JDBC Driver and explain in brief.
Answer: Type 4 Driver is the superfast JDBC Driver among four different types. This driver uses the database-specific native protocol to communicate with the database. It changes the JDBC_Calls into Database_specific_calls directly. This driver is developed only in java. Therefore, it is also known as a pure Java driver. This driver is independent of the platform.
Tell me about the JDBC statements in brief.
Answer: Three types of JDBC statements are Statement, Prepared Statement, Callable Statement. The statement we use to execute a static SQL statement whereas, the Prepared Statement is for a pre-compiled SQL Statement. The former one gets executed again and again while accepting different parameters at runtime.
State the reasons for the use Prepared_Statement in Java.
Answer: Prepaid Statement that we use to run SQL statements multiple times. Although Prepaid Statement is compiled for one time yet, we can use this many times.
What do you know about ResultSet and various types of ResultSet?
Answer: It is a Java object that has the output of the execution result of an SQL query. The information can be retracted utilizing get methods. Three basic types of ResultSet are SCROLL_SENSITIVE, FORWARD_ONLY, SCROLL_INSENSITIVE.
Why are we using ‘Metadata’?
Answer: Metadata means information about other data. Some examples that we can refer are the library catalog, the table of content, data items about a person like weight, walking, height.
Explain the term Database connection pooling.
Answer: Database connection pooling means storing the connections in the cache that you can use for future work. This process is faster and easy to diagnose.
Tell us the function of DriverManager class.
Answer: It is an interface between users and drivers. DriverManager keeps a record of all the activities done in between a database and the driver.
What do you mean by batch updates?
Answer: Batch updates mean the execution of a set of SQL queries all at a time. It is used only for insertion, up-gradation, and deletion except for the selection of SQL queries.
Name the packages available in JDBC API?
Answer: Only two types of packages are available for JDBC API named java.sql and javax.sql.
Mention the return type of Execute, executeQuery, executeUpdate.
Answer: The return type of ‘execute’ is boolean.
For executeQuery, it is ResultSet object.
For ExecuteUpdate, it is int.
Does the ResultSet Index start with zero or one?
Answer: ResultSet Index starts with 1.
Do you know the role of Class.forName while loading drivers?
Answer: It generates one case of JDBC Driver and lists with the DriverManager.
JDBC-ODBC Bridge is an example of multi-threaded or not?
Answer: It is not multi-threaded that uses synchronized methods for all the Calls made to ODBC.
Mention the interface that handles TransactionManagement in JDBC.
Answer: It is the Connection interface that handles TransactionManagement in JDBC. It also provides method for commit( ), rollback( ), etc.
State the reason for the well-known error NoSuitable driver?
Answer: This error occurs upon calling DriverManager getConnection Method. It may also be due to the inability to load the exact JDBC driver before you can call the getConnection Method and inappropriate JDBC URL. It is a common type of error while dealing with JDBC.
Why PreparedStatements are faster?
Answer: The execution of PreparedStatemenst is faster than DirectStatement because of the compilation. PreparedStatements and JDBC driver-both of them are connected at the time of execution.
Is it practically feasible to connect multiple databases?
Answer: Yes. It is practically possible to connect more than one database at a time but, it depends on the exact driver required. To do up-gradation and retrieve data from various databases it is better to use a single statement.
Differentiate between SetMaxRows(int) and SetFetchSize(int)?
Answer: SetMaxRows(int) defines the number of rows a resultset can contain at a particular time.
SetFetchSize(int) defines how many rows to read from the database.
Comment on the term Dirty read in the JDBC?
Answer: Dirty read means ‘read the value which may correct or may not correct’.
Elaborate on the meaning of a Two-phase commit?
Answer: Two-phase commit applies to the ‘Distributed transaction’ process. During the execution of any transaction, it will affect multiple databases. A two-phase commit is for the synchronization of all the databases to each other.
Can you mention the locking systems available in JDBC?
Answer: There are two types of locking systems available in JDBC which help to handle multiple users. When multiple users are viewing the same record, it ensures the continuation without locking. When multiple users are updating the record, we can use locking at that moment.
How many exceptions are there in JDBC?
Answer: We have four exceptions in JDBC such as Batch_Update exception, Data_Truncation, SQL_exception, SQL_warning. It becomes a necessary step to handle these exceptions while dealing with JDBC.
Name two steps to connect to the database using JDBC.
Answer: Anyone can create the connection to the database with the help of JDBC in two ways. One can perform the task either using DriverManager or using DataSource. DriverManager.get_Connection will help in creating a connection to ensure the accessibility of the database. For the DataSource, there is no need to use DriverManager using JNDI.
Explain the reason for using setAutoCommit(false) in JDBC?
Answer: It is when anyone wants to turn off the AutoCommit.
State the database warnings in JDBC and how to handle this?
Answer: It is the subclass of SQLException class. Anyone can handle it by using the GetWarnings() method.
Does it possible to get a null ResultSet?
Answer: It is impossible to get a null ResultSet.
In this article, we have discussed the various types of questions asked by an interviewer in the interview from the topic of JDBC. I hope you have got enough guidance from this article. Now you can go for the JDBC interview without any nervousness.