Saturday, December 22, 2012
JDBC Interview questions answers in Java - 2 to 4 years experienced programmer
JDBC Interview question forms one of the important section in Java Interviews. Similar to multithreading, Collection framework and Garbage collection interview question, JDBC question must be prepared by any Java programmer. Most of questions from JDBC or Java database connectivity comes 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 frequently asked JDBC Interview question 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 among Java community then please share with us. Let's see my 11 questions from JDBC, not so tough but worth preparing.
Here is my list of interview questions from JDBC. This list contains questions form JDBC drivers, PreparedStatement, CallableStatement, JDBC Connection pool, SQL Date and Timestamp API, ResultSet and RowSet concepts and couple of JDBC question from practical experience.
Question 1 : Difference between SQL Date and java.util.Date in Java
Answer : This is one of JDBC Questions which I like to ask, because knowing how to correctly store and retrieve date in a database is often confusing for new developers and its very critical for any application. 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 contains both date and time part. See SQL Date vs Util Date in Java for more differences between them.
Question 2: What is benefit of using PreparedStatement in Java
Answer : Another wonderful JDBC interview question which is very popular on telephonic as well as on early round of Java Interviews. There are several benefits of using PreparedStatement while querying database from Java program e.g. better performance and prevents from SQL Injection. I suggest to read Why use PreparedStatement in Java for more benefits and details on JDBC PreparedStatement.
Question 3: What is JDBC database Connection Pool? How to setup 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 pool of JDBC connection which is used by application to query database. Since JDBC connection are expensive it take time to create them which can slow response time of server if created during request time. Creating them on application start-up and reusing them result in better performance. See How to setup JDBC Connection Pool in Java using Spring for more details on JDBC connection pool and benefits it offer.
Question 4: What is 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. Key difference between type 2 and type 4 JDBC driver is that you just need to include JAR file of JDBC driver in your classpath to connect database. See this link for more difference between type 2 and type 4 JDBC drivers.
Question 5: What is difference between java.sql.Time and java.sql.TimeStamp in Java
Answer : This JDBC questions is similar to earlier JDBC interview question java.sql.Date vs java.util.Date. 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 difference 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 question which comes from JDBC. you may think that it will return first column as integer from 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 difference between RowSet and ResultSet in JDBC?
Answer : One of the popular JDBC interview question now days. RowSet extends ResultSet and add support for JDBC API to Java bean component model. Main difference of 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 benefit of disconnected and connected RowSet.
Question 8: What is use of setAutoCommit(false) in JDBC ?
Answer : This is one of the JDBC Interview question I touched on Top 10 JDBC best practices for Java programmer. making setAutoCommit(false) saves a lot of performance as it doesn't commit transaction automatically after each query and we do batch update. It allows you to handle it using commit() and rollback(). This has result in impressive performance gain in DAO layer.
Question 9: How to call stored procedure from JDBC in Java?
Answer : This JDBC Interview question is another one you can add on any frequently asked list and just can't afford not to prepare. Mostly asked to Java developers with 2 to 4 years experience. In its simplicity you can just say that CallableStatement is used to call stored procedure, which may lead questions like how do you pass parameters to stored procedure from Java or difference between IN and OUT parameters in JDBC etc. It's worth to prepare this JDBC question in detail. By the way IN parameter is used to pass input to stored procedure and OUT parameter is used to store output return from stored procedure. IF your stored procedure return multiple values than you can also use ResultSet to traverse all results.
Question 10: What is difference between Connected and disconnected RowSet in JDBC?
Answer : I have seen this JDBC question asked as a follow-up question of previous JDBC interview question RowSet vs ResultSet. Main difference between connected and disconnected RowSet in JDBC is that disconnected RowSet doesn't require JDBC Connection while it's on disconnected state. This makes disconnected RowSet light and 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 small data set and thin Java clients with small memory foot print.
Question 11: What is difference between Statement, PreparedStatement and CallableStatement in Java?
Answer : One of the classical JDBC interview question. Main difference between Statement and PreparedSatement is performance and avoiding SQL Injection as we have seen in Benefits of using PreparedStatement in Java. While CallableStatement has very specific use in JDBC and used to call stored procedure from Java program
That's all on these 11 JDBC Interview questions and answers article. As I said many times, JDBC questions are important part of any Java interview, let it core Java or J2EE 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