Monday, September 11, 2023

11 JDBC Interview questions answers in Java - 2 to 4 years experienced programmer

Hello guys, if you are preparing for Java developer interviews then you may know that the JDBC Interview question forms one of the important sections in Java Interviews. Similar to multithreading, Collection framework, and Garbage collection interview question, JDBC questions must be prepared by any Java programmer. Most of the questions from JDBC or Java database connectivity come from API and basic architecture of JDBC which also involves JDBC drivers. A good understanding of JDBC API along with database basics like transactions also, help to do well in JDBC interviews.

I have collected some of the frequently asked JDBC Interview questions for quick reference. This will help to revise some important JDBC concepts and also give a chance to explore JDBC API to newcomers. 

If you have any other JDBC interview question, which has been asked to you or friends, and you think it’s good to share with the Java community then please share with us. Let's see my 11 questions from JDBC, not so tough but worth preparing. 

If you need more Java questions, you can also buy my book Grokking the Java Interview on Gumroad, its not expensive and covers essential Java interview topics like core java, design patterns, multithreading, collections, JDBC, and more.  You can also download a sample copy for free. 




11 JDBC Interview Questions with Answers for Java Devs

Here is my list of interview questions from JDBC. This list contains questions from JDBC drivers, PreparedStatement, CallableStatement, JDBC Connection pool, SQL Date and Timestamp API, ResultSet and RowSet concepts, and a couple of JDBC questions from practical experience.


Question 1: Difference between SQL Date and java.util.Date in Java? (answer)

Answer: This is one of the JDBC Questions that I like to ask because knowing how to correctly store and retrieve data in a database is often confusing for new developers and it's very critical for any application. 

The main difference between SQL data i.e. java.sql.Date and util date i.e. java.util.Date is that SQL Date only contains date part and not time part but util date contain both date and time part. 

If you want to learn more then you can further see the Complete JDBC Programming Part 1 and 2 courses on Udemy to learn more about JDBC API in depth. 




Question 2: What is the benefit of using PreparedStatement in Java?

Answer: Another wonderful JDBC interview question that is very popular on telephonic as well as on the early rounds of Java Interviews. There are several benefits of using PreparedStatement while querying databases from Java programs e.g. better performance and preventing SQL Injection. I suggest reading Why use PreparedStatement in Java for more benefits and details on JDBC PreparedStatement.



Question 3: What is JDBC database Connection Pool? How to set up in Java?

Answer: I have always seen at least one question related to database connection pool in JDBC Interview e.g. benefit of using JDBC Connection pool. Well JDBC connection pool maintains a pool of JDBC connections that are used by applications to query databases. 

Since JDBC connection is expensive it takes time to create them which can slow the response time of the server if created during request time. 

Creating them on application start-up and reusing them result in better performance. See How to set up JDBC Connection Pool in Java using Spring for more details on the JDBC connection pool and the benefits it offers.




Question 4: What is the difference between type 2 and type 4 JDBC drivers in Java?

Answer: This JDBC Interview question is as old as Vector vs ArrayList or Hashtable vs HashMap. I remember questions about JDBC ODBC drivers asked during almost every fresher level interview. 

The key difference between type 2 and type 4 JDBC drivers is that you just need to include the JAR file of the JDBC driver in your classpath to connect the database. See this link for more differences between type 2 and type 4 JDBC drivers.



Question 5: What is the difference between java.sql.Time and java.sql.TimeStamp in Java?

Answer: This JDBC question is similar to the earlier JDBC interview question java.sql.Date vs java.util.Date. The main difference is that java.sql.Time class doesn't contain any date information on it while java.sql.TimeStamp contains date information. See 4 differences between Time and Timestamp in Java JDBC for more differences.


Question 6: What happens if we call resultSet.getInt(0) when Select query result just have one column of integer type?

Answer: This is one of the tricky Java questions which comes from JDBC. you may think that it will return the first column as an integer from the Query result set but unfortunately, it doesn't. It throws InvalidColumnIndexException in JDBC because index for getXXX() or setXXX() in JDBC starts with 1. See How to fix InvalidColumnIndexException in JDBC for more details on this JDBC interview question.



Question 7: What is the difference between RowSet and ResultSet in JDBC?

Answer: One of the popular JDBC interview questions nowadays. RowSet extends ResultSet and adds support for JDBC API to the Java bean component model. The main difference between ResultSet and RowSet is RowSet being connected and disconnected, which is another follow-up JDBC question. RowSet makes it easy to use ResultSet but as I said you only like to use it to get the benefit of disconnected and connected RowSet.


Question 8: What is use of setAutoCommit(false) in JDBC ?

Answer: This is one of the JDBC Interview questions I touched on Top 10 JDBC best practices for Java programmers. making setAutoCommit(false) saves a lot of performance as it doesn't commit transactions automatically after each query and we do batch updates. It allows you to handle it using the commit() and rollback(). This has resulted in impressive performance gain in the DAO layer.


Question 9: How to call a stored procedure from JDBC in Java?

Answer: This JDBC Interview question is another one you can add to any frequently asked list and just can't afford to prepare. Mostly asked Java developers with 2 to 4 years experience

In its simplicity, you can just say that CallableStatement is used to call a stored procedure, which may lead to questions like how do you pass parameters to a stored procedure from Java or the difference between IN and OUT parameters in JDBC, etc. 

It's worth preparing this JDBC question in detail. By the way IN parameter is used to pass input to the stored procedure and the OUT parameter is used to store output return from a stored procedure. 

If your stored procedure returns multiple values then you can also use ResultSet to traverse all results.




Question 10: What is the difference between Connected and disconnected RowSet in JDBC?

Answer: I have seen this JDBC question asked as a follow-up question of the previous JDBC interview question RowSet vs ResultSet.  The main difference between connected and disconnected RowSet in JDBC is that disconnected RowSet doesn't require JDBC Connection while it's in a disconnected state. 

This makes disconnected RowSet light ideal to use in thin clients, while connected RowSet is just a wrapper around ResultSet. JDBCRowSet and WebRowSet are two examples of connected RowSet while a CachedRowSet is an example of disconnected RowSet which caches data in memory. Ideal for the small data sets and thin Java clients with a small memory footprint.


Question 11: What is the difference between Statement, PreparedStatement, and CallableStatement in Java?

Answer: One of the classical JDBC interview questions. The main difference between Statement and PreparedSatement is performance and avoiding SQL Injection as we have seen in the Benefits of using PreparedStatement in Java. While CallableStatement has a very specific use in JDBC and is used to call a stored procedure from the Java program



JDBC interview questions answers for 2 to 4 years Java programmersThat's all on these 11 JDBC Interview questions and answers articles. As I said many times, JDBC questions are an important part of any Java interview, let it be a core Java or Java EE Interview and you just can't ignore it. 

Always prepare JDBC well enough to answer any JDBC Interview question to perform well in Java interviews.



Related Interview question articles for Java programmer
That's all about the frequently asked Java Database interview questions and answers. If you find these JDBC interview questions useful then please share them with your friends and colleagues. If you have any questions or feedback then please drop a note. 


3 comments:

  1. One JDBC question which I remember is Connection Pool implementation you used e.g. C3PO or DBCP and then comparison between C3PO and DBCP

    ReplyDelete
  2. Do enterprises even use straight JDBC anymore? Most projects I've been on in the last 8 years have always used Spring, Hibernate, EJBs, or some other data abstraction layer.

    ReplyDelete
  3. Even though JDBC is not part of core java I increasingly see questions from JDBC in core Java interviews. thanks for sharing these, hope you could share some more questions related to performance and security aspect of JDBC.

    ReplyDelete

Feel free to comment, ask questions if you have any doubt.