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.

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 50 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.

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.

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 Prepared Statement 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. 

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.

31. What are the JDBC API components?

The interfaces and the classes of JDBC API are mentioned below:

Different Interfaces

  • Connection: Using the getConnection()method, a connection object is created.
  • Statement: Using the createStatement() method of the connection class, the statement object is created.
  • PrepareStatement: The prepareStatement() method of connection class, the prepareStatement object is created.
  • DatabaseMetaData: The Connection interface's getMetaData() method returns an object of DatabaseMetaData.
  • CallableStatement:  The stored procedures and functions are called using the CallableStatement interface.

Different Classes

  • DriverManager: The class's function is to serve as a gateway between users and drivers.
  • Blob: The binary term for a huge object. It stands for a set of binary data that is kept together in the database management system as a single entity.
  • Clob:  Character Large Object is referred to as Clob. Different database management systems utilize it as a data type to store character files.
  • SQLException: It is an exception class that offers details on problematic database access.

32. What are the JDBC statements?

Statements are used in JDBC to communicate SQL commands to databases and receive data from them. Three categories of statements exist:

  • Statements
  • Prepared Statements
  • Collable Statement

33. Mention the return type of Class.forName() method?

The object of java.lang.Class Object is the return type of the Class.forName() method.

34. Explain the differences between Statement and PreparedStatement interface?

Statement

Prepared statement

The Statement interface provides strategies for the database to run queries. The statement interface serves as a factory for ResultSet, offering the factory function to obtain a ResultSet object.

It is a subinterface of a Statement. It is used for parameterized query execution.

When using Statement, the query is compiled each time when the application is executed.

The query is only compiled once in the case of PreparedStatement.

The Statement is mostly employed when a static query has to be executed at runtime.

We use prepared statements when the requirement for input parameters occurs at the runtime query.

35. How can we set a null value in JDBC PreparedStatement?

We can set a null value to an index using the setNull()method.  Below given is the syntax for the given method:

void setNull(int parameterIndex, int sqlType) throws SQLException  

36. Explain the benefits of Prepared Statement over Statement?

  • The Prepared Statement outperforms the statement since the statement must be compiled each time the code is run, but the PreparedStatement is compiled once and then executed only at runtime.
  • Statement can only run static queries, but Prepared Statement can run parameterized queries.
  • Every time, the query used in PreparedStatement appeared to be the same. As a result, the database can reuse the previous access plan, whereas statement inlines the parameters into the string, so the query does not appear to be the same every time, preventing cache utilization.

37. Explain the execution of stored procedures using Callable Statement?

The steps to write and run stored procedures are listed below.

Create the database procedure: You must first build the stored procedure in the database before you can use it.

Make a network connection.

Make the Callable Statement Object.

Give the values, then run the query.

The values can be located in the database, so check there.

38. Describe the role of the JDBC DriverManager class.

The DriverManager class is used as an interface between the users and drivers. It maintains track of the available drivers and performs the process of connecting a database to the proper driver. 

39. Mention the functions of the JDBC Connection interface?

The database session is maintained by the Connection interface. The management of transactions is possible with it. To return the instance of statement, Prepared Statement, Callable Statement, and DatabaseMetaData, the factory method is provided by the JDBC connection interface.

40. What is the JDBC ResultSet interface?

A table row is represented by the ResultSet object. It can be used to retrieve data from the database and move the cursor pointer. The ResultSet object can only move forward by default and cannot be updated.

41. What is the JDBC ResultSetMetaData interface?

The table's information, including the total number of columns, column names, column types, etc., is returned via the ResultSetMetaData interface.

42. Mention the interface which enables transaction management in JDBC.

The Connection interface offers transaction management methods like commit(), rollback(), and others.

43. Explain batch processing and method to perform batch processing in JDBC?

We can run many queries by utilizing JDBC's batch processing method. It speeds up the performance. Batch processing is supported through java.sql.Statement and java.sql.Prepared Statement interfaces. The actions listed below are necessary for batch processing in JDBC.

Load the driver class

Create Connection

Create Statement

Add query in the batch

Execute the Batch

Close Connection

44. Explain CLOB and BLOB data types in JDBC?

BLOB: A BLOB is a big, variable-length binary object that is used to store mixed media, voice, and other binary data types. On a MySQL database, it can hold up to 2GB of data, and on an Oracle database, up to 128 GB. Many databases, including MySQL, Oracle, and DB2, enable BLOB to store binary data (images, video, audio, and mixed media).

CLOB: A CLOB is a character-large object of variable length that is used to store character-based data, such as files in various databases. On an Oracle Database, it can hold 128 GB, while a MySQL database can handle up to 2 GB. Considered to be a character string is a CLOB.

45. What are the different types of lockings in JDBC?

The four different types of locks offered by JDBC are discussed below.

  • Row and Key Locks: We utilize this kind of lock when updating the rows.
  • Page locks: A page can have one of these locks applied to it.
  • Table locks: The table is protected by table locks.
  • Database locks: When a database is open, a lock is used to stop other transactions from reading or updating data.

46. How can we store and retrieve images from the database?

We may store and retrieve photos using the Prepared Statement interface. A table with the columns NAME and PHOTO should be created.

47. How can we store the file in the Oracle database?

CLOB datatype is used in the table to store files in the Oracle database. 

48. How can we retrieve the file in the Oracle database?

To retrieve the file in the Oracle database we use the getClob() method of the prepared statement.

49. Mention the differences between stored procedures and functions?

Stored procedures

Functions

Performs business logic

Performs the calculation

Will not have the return type

Will have the return type.

Should return 0 or more values

Should return only one value

It supports both input and output parameters

It supports only one parameter.

50. Mention ways to maintain the integrity of a database by using JDBC?

We must ensure the ACID properties to maintain the integrity of a database. Atomicity, consistency, isolation, and durability are referred to as ACID properties.

51. What is the role of the JDBC setMaxRows method?

Using setMaxRows(int i), we can limit the number of rows returned by a database using the query. Since MySQL allows us to use the limit cause, this may also be done within the query.

If you're eager to gain the skills required to work in a challenging, rewarding, and dynamic IT role - we've got your back! Discover the endless opportunities through this innovative Post Graduate Program in Full Stack Web Development course designed by our partners at Caltech CTME. Enroll today!

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.

Our Software Development Courses Duration And Fees

Software Development Course typically range from a few weeks to several months, with fees varying based on program and institution.

Program NameDurationFees
Caltech Coding Bootcamp

Cohort Starts: 15 Apr, 2024

6 Months$ 8,000
Full Stack Java Developer

Cohort Starts: 2 Apr, 2024

6 Months$ 1,449
Automation Test Engineer

Cohort Starts: 3 Apr, 2024

11 Months$ 1,499
Full Stack Developer - MERN Stack

Cohort Starts: 3 Apr, 2024

6 Months$ 1,449

Learn from Industry Experts with free Masterclasses

  • Fuel Your 2024 FSD Career Success with Simplilearn's Masters program

    Software Development

    Fuel Your 2024 FSD Career Success with Simplilearn's Masters program

    21st Feb, Wednesday9:00 PM IST
  • Break into a Rewarding Full Stack Developer Career with Mern Stack

    Software Development

    Break into a Rewarding Full Stack Developer Career with Mern Stack

    2nd Apr, Tuesday9:00 PM IST
  • Java vs JavaScript: The Right Learning Path for You in 2024

    Software Development

    Java vs JavaScript: The Right Learning Path for You in 2024

    19th Mar, Tuesday9:00 PM IST
prevNext