Java Database Connectivity or JDBC is a Java API to link and execute a query with the database. In other words, it is an Application Programming Interface (API) for the Java platform, which helps with interaction and for executing the SQL query. If you are looking for a career in software programming, we recommend going through our list of JDBC Interview Questions.

JDBC drivers are used for establishing a connection with the database. The JDBC API helps to access tabular data stored in relational databases such as MySQL, Oracle, MS Access, and so on. JDBC was released as part of JDK 1.1 in 1997, making it one of the earliest libraries that has been developed for the Java language.

Post Graduate Program: Full Stack Web Development

in Collaboration with Caltech CTMEEnroll Now
Post Graduate Program: Full Stack Web Development

Why Should You Learn JDBC?

JDBC is key for writing real-world Java applications. As data is the essential part of any application and JDBC is a basic way to work with data in Java applications, good knowledge, and expertise in JDBC can offer a competitive advantage as a Java developer. Previously, ODBC API was the database API to interact with the database and run the queries. However, an ODBC API uses C language for ODBC drivers that are unsecured and platform-dependent. Thus, Java has defined its own JDBC API. This API uses JDBC drivers, which offer a Java interface for interacting with the database through SQL. JDBC is needed to offer a “pure Java” solution to develop an application using Java programming. Following are a few interesting facts about JDBC that you should know:

  • JDBC works for relational databases only.
  • JDBC connections are expensive.
  • You can connect JDBC to ODBC which is a Microsoft-based specification used by databases such as SQL Server and Access.

Top JDBC Interview Questions and Answers for 2022

JDBC being key to java development, you must be well prepared with JDBC Interview Questions if you are serious about a career in writing Java applications. Following are the top 30 questions that you should be prepared with before you attend your interview:

1. Explain what is JDBC?

This is one of the first and most frequently asked questions in most of the interviews. JDBC stands for Java Database Connectivity. As its name implies, it is a Java API used for interacting with relational databases to access, modify and process data using SQL. It utilizes JDBC drivers for interacting with the database. By using JDBC, one can access tabular data from different types of relational databases such as MySQL, Oracle, MS Access, and so on. 

2. What is ResultSet?

The java.sql.ResultSet interface is used to represent the database result set, which is a result of the execution of SQL queries using Statement objects. The object of ResultSet comprises a cursor that points to the present row of data in the result set. At first, the cursor is displayed before the first row. Then the cursor moves to the next row by executing the next() method. The next() method iterates through the result set with the support of a while loop. If there are no other rows left, the next() method returns a false. Following is an example for the creation of ResultSet:

ResultSet rs = con.executeQuery(sqlQuery);

3. Can you get a null ResultSet?

No, you can never get null Resultset(). The ResultSet.next() can return null only if the next record does not comprise a row.

4. Explain the difference between ResultSet Vs. RowSet vs in JDBC?

In a ResultSet() handle that is linked to a database, you cannot make the Result a serialized object. Thus, you cannot pass Resultset() across the network. The RowSet() spreads the ResultSet() interface, so it comprises all methods from ResultSet(). 

A RowSet() is always serialized. Thus, we can pass a Rowset() from one class to another class as it is not connected to the database.

Full Stack Java Developer Course

In Partnership with HIRIST and HackerEarthEXPLORE COURSE
Full Stack Java Developer Course

5. What is a JDBC driver?

A JDBC driver is a software component having multiple interfaces and classes that enables a Java application to communicate with a database. To establish a link with individual databases, JDBC needs specific drivers for each database. These drivers are available with the database vendor along with the database.

A JDBC driver establishes a connection to the database. It also executes the protocol for sending the query and result between the database and the client.

6. What is DriverManager in JDBC?

The JDBC DriverManager is a static class in Java, using which you operate on the set of JDBC drivers available for an application to use. Different JDBC drivers can be used simultaneously by one application if required. Every application specifies a JDBC driver by using a Uniform Resource Locator (URL). On loading the JDBC Driver class into an application, it registers itself to the DriverManager by using DriverManager.registerDriver() or Class.forName(). To confirm this, you can view the source code of JDBC Driver classes. On calling the DriverManager.getConnection() method by passing details related to the database configuration, the DriverManager utilizes the registered drivers to receive the connection and return it to the caller program.

7. Explain the different drivers of JDBC.

There are four different drivers in JDBC. They are:

  • JDBC-ODBC Bridge: JDBC-ODBC bridge acts as an interface between the DB server and the client. 
  • Native API: Half Java Driver: This driver almost behaves as a JDBC-ODBC driver. However, instead of an ODBC driver, you use native APIs and a client-side database.
  • Network Protocol: This driver works like a 3-tier approach for accessing the database. An intermediate server connects to the database. The JDBC method calls send data to a transitional server and then the server communicates with the database.
  • Thin Driver: It is completely written in Java. This driver explicitly changes JDBC method calls to the vendor-specific database protocol.

8. Explain which is the most commonly used and fastest JDBC driver.

The JDBC Net pure Java driver (Type 4 driver) is the fastest and most commonly used driver for remote and localhost connections. It interacts directly with the database by changing the JDBC calls into vendor-specific protocol calls.

9. What are the data types used for storing images and files in the database table?

  • The BLOB data type stores images in the database. You can also store audio and videos using this data type. This data type stores a binary type of data.
  • The CLOB data type stores files in the database. This data type stores the character type of data.

10. Explain what DatabaseMetaData is and why would you use it?

The DatabaseMetaData is an interface that provides methods to gain information about the database. This interface helps in obtaining database-related information, such as database name, driver name, database version, the total number of tables or views, and so on.

11. Explain the differences between JDBC and ODBC?

JDBC (Java Database Connectivity)

ODBC (Open Database Connectivity)

JDBC is used for the Java language.

ODBC can be used for things such as C, C++, Java, and so on.

JDBC can be used on any platform; thus, making it platform-independent.

ODBC can be used only for the Windows platform; thus, making it platform-dependent.

JDBC drivers are developed using the Java language.

Most of the ODBC Drivers have been developed using native languages such as C, C++

JDBC is ideal for Java applications as there are no performance issues.

ODBC is not recommended for Java applications as its performance is not great due to internal conversion.

JDBC is Object Oriented.

ODBC is procedural.

12. What is Rowset?

A RowSet is nothing but an object that captures a row set from either tabular data sources or JDBC result sets such as spreadsheets or files. It supports component-based development models such as JavaBeans, with the help of event notifications and a standard set of properties.

Full Stack Web Developer Course

To become an expert in MEAN StackView Course
Full Stack Web Developer Course

13. What is ResultSet?

The java.sql.ResultSet interface is nothing but the result set of a SQL query. It indicates that a cursor is pointing to a row of a table.

14. What are the types of ResultSet?

There are three types of ResultSet: 

  • TYPE_FORWARD_ONLY: The cursor only moves forward.
  • TYPE_SCROLL_INSENSITIVE: The cursor can move either backward or forward, but the cursor is not sensitive to changes to the data that underlies the ResultSet.
  • TYPE_SCROLL_SENSITIVE: The cursor can move backward and forward, but it is sensitive to the data that underlies the ResultSet.

Note: If we do not declare any ResultSet then this means that we are calling the TYPE_FORWARD_ONLY ResultSet.

15. Explain the advantages of a Rowset.

The advantages of using RowSet are:

  • It is flexible and easier to use.
  • It is Updatable and Scrollable by default.

16. Explain what are the different JDBC statements:

Following are the three types of JDBC Statements:

  • Statement: Executes an SQL query (static SQL query) against the database.
  • Prepared Statement: Executes an SQL statement repeatedly. The input data is dynamic and takes the input at the run time.
  • Callable Statement: Executes stored procedures.

17. Are there any advantages of using a PreparedStatement in Java?

A Prepared Statement executes certain SQL statements repetitively. This statement is compiled only once even though it is executed “n” number of times.

18. Explain the term connection pooling.

This is another one of the popular JDBC interview questions. Connection pooling is the technique by which we reuse the resource-like connection objects that are required to establish a connection with the database. It is a technique of managing and creating a pool of connections that can be used by any thread that requires them. Each time an application tries to access a backend store (such as a database), it needs resources to develop, maintain, and release a connection to that datastore. Thus, connection pooling can greatly increase the performance of a Java application while reducing the overall resource usage. 

19. Explain the types of JDBC Architecture?

There are two kinds of architecture models to access the database while using JDBC.

They are:

  • Two-tier Architecture: In this model, Java programs exclusively connect with the database and there is no requirement of any mediator like applications servers to connect with the database. This model is also known as the client-server architecture.
  • Three-tier Architecture: This model is the complete opposite of two-tier architecture. An application server is used as a mediator between the Java program or JDBC driver and the database. The Java program sends a request to the application server. The server then forwards this request to the database and waits for a response from the database.

20. What are the main components of JDBC?

Following are the four major components available in JDBC:

  • JDBC Driver Manager
  • JDBC API
  • JDBC Test Suite
  • JDBC – ODBC Bridge

21. What are the steps to connect with JDBC?

Following are the 6 basic steps to connect with the database in Java. 

  1. Import the JDBC packages.
  2. Load and register the JDBC driver.
  3. Open and establish a connection to the database.
  4. Creation a statement object to perform a query.
  5. Execute the statement object and return a query ResultSet(). Then process the ResultSet().
  6. Close the ResultSet() and statement objects.
  7. Close the connection.

22. Explain the meaning of hot backup and cold backup.

While this question is not related to JDBC directly, it may still be asked in an interview. The cold back is the backup process in which the backup of files is taken before restarting the database. In the hot back, backup process the backup of tables and files is taken at the time when the database is running. 

New Course: Full Stack Development for Beginners

Learn Git Command, Angular, NodeJS, Maven & MoreEnroll Now
New Course: Full Stack Development for Beginners

23. What are the steps to connect with JDBC?

There are 2 types of locking available in JDBC by which we can manage various user issues. If two users are trying to access the same record then there is no issue but the problem arises when multiple users are trying to update the same record simultaneously. To address this issue, there are two locking modes available in JDBC. They are:

  1. Optimistic Locking: Locks the record only when an update is taking place. This kind of locking does not use exclusive locks while reading.
  2. Pessimistic Locking: In this system, the records are locked as the row to update is selected.

24. Explain if the JDBC-ODBC Bridge supports different concurrent open statements per connection?

No, when using the JDBC-ODBC Bridge, you can open only one Statement object.

25. What are database warnings in JDBC and explain how we can manage database warnings in JDBC?

The database warning or SQL warning is nothing but the subclass of the SQLException class. You can manage or handle this warning by using the getWarnings() method on a statement, connection, and ResultSet.

26. Explain the difference between executeQuery, executing, and executeUpdate in JDBC?

  • executeQuery(): The executeQuery() can be used for select query.
  • execute(): The execute() can be used for any type of SQL Query.
  • executeUpdate(): The executeUpdate() can be used to modify/update tables.

27. Explain what you mean by Metadata and also explain why we use it.

Metadata means information about other data. You use metadata to get the database product version, driver name, the total number of views and tables.

28. Explain why you would use setAutoCommit(false) in JDBC?

The setAutoCommit(false) is used to turn off the Auto Commit.

29. Explain the role of Class.forName while loading drivers in brief?

The Class.forName generates an instance of JDBC driver and a register with DriverManager.

30. Why are prepared statements faster?

The Prepared statement execution is much faster than direct execution as the statement is compiled only once. JDBC drivers and Prepared statements are connected with each other during execution, and there are no connection overheads.

Looking to accelerate your career as a skilled Full Stack Web Developer? Leverage Caltech CTME's academic excellence in a unique bootcamp-style Post Graduate Program in Full Stack Web Development. Enroll Now!

Conclusion

This article has provided you with the most commonly asked JDBC interview questions that can help you get the job of your dreams in the field of Java applications. Make sure you go through all the questions and answers provided in this article. 

We suggest the Post Graduate Program in Full Stack Web Development from SimpliLearn for some great web developer programs, then. This program can help soar your career as a MEAN stack developer. The program will help you learn skills such as Express.js, MongoDB, Angular, and Node.js (“MEAN”), along with GIT, HTML, CSS, and JavaScript to build and deploy interactive applications and services throughout this program.

Also, if cloud solutions interest you, then we suggest the AWS Cloud Architect program from SimpliLearn. This AWS Cloud Architect Certification Course will help you with the expertise and knowledge you require in Amazon Web Services (AWS).

If you have any questions or queries, feel free to post them in the comments section below. Our team will get back to you at the earliest.

About the Author

SimplilearnSimplilearn

Simplilearn is one of the world’s leading providers of online training for Digital Marketing, Cloud Computing, Project Management, Data Science, IT, Software Development, and many other emerging technologies.

View More
  • Disclaimer
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, and OPM3 are registered marks of the Project Management Institute, Inc.