Friday, December 7, 2012

Top 10 JDBC Interview questions answers for Java programmer

JDBC Interview Question and Answer
JDBC Questions are integral part of any Java interview,  I have not seen any Java Interview which is completed without asking single JDBC Interview question, there are always at least one or two question from JDBC API. Some of the popular questions like Why you should use PreparedStatement in Java,  Difference between PreparedStatement and CallableStatement in Java and few questions is related to improving performance of JDBC layer by applying some JDBC performance tips. In this article I have summarized few frequently asked questions in JDBC, they ranges from easy to difficult and beginner to advanced. Questions like distributed transaction management and 2 phase commit is tough to answer until you have real experience but mostly asked in various J2EE interviews. This is not an extensive list of JDBC question answers but practicing or revising this question before going to any Java interview certainly helps.

 

10 JDBC Interview question answer in Java

JDBC Interview question and answer for Java programmerHere is my list of frequently asked JDBC question in Java, I have tried to provide answer to most of question. If you have any interesting JDBC question which you have faced and not in this list then please share with us.


Question 1:  What is JDBC?
Answer : One of the first JDBC interview question in most of interviews. JDBC is java database connectivity as name implies it’s a java API for communicating to relational database, API has java classes and interfaces using that developer can easily interact with database. For this we need database specific JDBC drivers. Some time this also result in followup questions like Difference between type 2 and type 4 JDBC drivers. See the link for answer.


Question 2: What are the main steps in java to make JDBC connectivity?
Answer : Another beginner level JDBC Interview question, mostly asked on telephonic interviews. Here are main steps to connect to database.
·          Load the Driver: First step is to load the database specific driver which communicates with database.
·          Make Connection: Next step is get connection from the database using connection object, which is used to send SQL statement also and get result back from the database.
·          Get Statement object: From connection object we can get statement object which is used to query the database
·          Execute the Query:Using statement object we execute the SQL or database query and get result set from the query.
·          Close the connection:After getting resultset and all required operation performed the last step should be closing the database connection.
For complete code example you can also refere Java program to connect to Oracle database


Question 3: What is the mean of “dirty read“ in database?
Answer : This kind of JDBC interview question is asked on 2 to 4 years experience Java programmer, they are expected to familiar with database transaction and isolation level etc. As the name it self convey the meaning of dirty read “read the value which may or may not be correct”. in database when one transaction is executing and changing some field value same time some another transaction comes and read the change field value before first transaction commit or rollback the value ,which cause invalid value for that field, this scenario is known as dirty read.

Question 4: What is 2 phase commit?
Answer : This is one of the most popular JDBC Interview question and asked at advanced level, mostly to senior Java developers on J2EE interviews. Two phase commit is used in distributed environment where multiple process take part in distributed transaction process. In simple word we can understand like if any transaction is executing and it will effect multiple database then two phase commit will be used to make all database synchronized with each other.

In two phase commit, commit or rollback is done by two phases:
1.       Commit request phase: in this phase main process or coordinator process take vote of all other process that they are complete their process successfully and ready to commit if all the votes are “yes” then they go ahead for next phase. And if “No “then rollback is performed.
2.       Commit phase: according to vote if all the votes are yes then commit is done.

Similarly when any transaction changes multiple database after execution of transaction it will issue pre commit  command on each database and all database send acknowledgement and according to acknowledgement if all are positive transaction will issue the commit command otherwise rollback is done .

Question 5: What are different types of Statement?
Answer :  This is another classical JDBC interview question. Variants are Difference between Statement, PreparedStatemetn and CallableStatement in Java. Statement object is used to send SQL query to database and get result from database, and we get statement object from connection object.

There are three types of statement:
1. Statement: it’s a commonly used for getting data from database useful when we are using static SQL statement at runtime. it will not accept any parameter.
              Statement   stmt = conn.createStatement( );
      ResultSet rs = stmt.executeQuery();

2. PreparedStatement: when we are using same SQL statement multiple time its is useful and it will accept parameter at runtime.
   
              String SQL = "Update stock SET limit = ? WHERE stockType = ?";
      PreparedStatement  pstmt = conn.prepareStatement(SQL);
      ResultSet rs = pstmt.executeQuery();

To learn more about PreparedStatement, see  What is PreparedStatement in Java and Benefits

3. Callable Statement: when we want to access stored procedures then callable statement are useful and they also accept runtime parameter. It is called like this
           
      CallableStatement cs = con.prepareCall("{call SHOW_SUPPLIERS}");
      ResultSet rs = cs.executeQuery();


Question 6: How cursor works in scrollable result set?
Answer : Another  tough JDBC Interview question, not many Java programmer knows about using Cursor in Java.
in JDBC 2.0 API new feature is added to move cursor in resultset backward forward and also in a particular row .
There are three constant define in result set by which we can move cursor.
·          TYPE_FORWARD_ONLY: creates a nonscrollable result set, that is, one in which the cursor moves only forward
·          TYPE_SCROLL_INSENSITIVE : a scrollable result set does not reflects changes that are made to it while it is open
·          TYPE_SCROLL_SENSITIVE: a scrollable result set  reflects changes that are made to it while it is open


Question 7:  What is connection pooling?
Answer : This is also one of the most popular question asked during JDBC Interviews. Connection pooling is the mechanism by which we reuse the recourse like connection objects  which are  needed to make connection with database .In this mechanism client are not required every time make new connection and then interact with database instead of that connection objects are stored in connection pool and client will get it from there. so it’s a best way to share a server resources among the client and enhance the application performance. If you use Spring framework, then you can also refer How to setup JDBC Connection Pool using Spring in Java


Question 8: What do you mean by cold backup, hot backup?
Answer : This question is not directly related to JDBC but some time asked during JDBC interviews. Cold back is the backup techniques in which backup of files are taken before the database restarted. In hot backup backup of files and table is taken at the same time when database is running. A warm is a recovery technique where all the tables are locked and users cannot access at the time of backing up data.


Question 9: What are the locking system in JDBC
Answer : One more tough JDBC question to understand and prepare. There are 2 types of locking in JDBC by which we can handle multiple user issue using the record. if two user are reading the same record then there is no issue but what if users are updating the record , in this case changes done by first user is gone by second user if he also update the same record .so we need some type of locking so no lost update.

Optimistic Locking: optimistic locking lock the record only when update take place. Optimistic locking does not use exclusive locks when reading

Pessimistic locking: in this record are locked as it selects the row to update

Question 10: Does the JDBC-ODBC Bridge support multiple concurrent open statements per connection?
Answer: No, we can open only one statement object when using JDBC-ODBC Bridge.

That’s all on this list of 10 JDBC Interview question with answer. As I said JDBC API and there concepts are integral part of any Java interview and there is always atleast one question from JDBC. Since most application uses datbase in backend, JDBC becomes critical for any Java developer.

Other Java Interview articles you may find useful

4 comments :

Anonymous said...

Few more JDBC questions to add in your list :

1) Difference between Rowset and ResultSet in JDBC
2) When to use Rowset in JDBC ?
3) What is connected and disconnected Rowset in JDBC 3.0 ?
4) What are the major changes made in JDBC 3.0 version or difference between JDBC 2.0 and JDBC 3.0 API.
5) What is C3PO or which connection pool framework have you worked ? Difference between DBCP and C3PO ? which one to prefer etc.
6) Different types of CURSER in Java ?
7) Scroll-able ResultSet etc
I will appreciate if you could answers of these JDBC questions as well.

Harry said...

I was asked to explain two Major component of JDBC architecture? According to me it was 1) driver and 2) JDBC API itself

I am not sure if that is correct, anyone please advice?

Anonymous said...

JDBC is too old, use Hibernate or iBatis

GeekDude said...

Hi Javin, this will cover all the points to conncet JDBC from the java program and given all the statements clearly on JDBC Statements.
You can find some more
javabynataraj.blogspot.in/2009/08/java-interview-questions.html
164 Core Java Interview QuestionsHere.
Thank you.

Post a Comment