Friday, August 13, 2021

Top 10 JDBC Interview questions answers for Java programmer

JDBC Interview Question and Answer
JDBC Questions are an integral part of any Java interview,  I have not seen any Java Interview which is completed without asking the single JDBC Interview question, there is always at least one or two questions 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 are related to improving the performance of the JDBC layer by applying some JDBC performance tips.

In this article, I have summarized a few frequently asked questions in JDBC, they range 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 questions in Java, I have tried to provide the answer to most of the questions. If you have any interesting JDBC questions which you have faced and are not on this list then please share with us.



>

Question 1:  What is JDBC?

Answer: One of the first JDBC interview questions in most of the interviews. JDBC is java database connectivity as name implies it’s a Java API for communicating to relational databases, API has java classes and interfaces using that developer can easily interact with database. For this, we need database specific JDBC drivers. Sometimes this also result in follow-up questions like the Difference between type 2 and type 4 JDBC drivers. See the link for the 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 the main steps to connect to the 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 the 2 phase commit?

Answer: This is one of the most popular JDBC Interview questions and asked at an advanced level, mostly to senior Java developers on J2EE interviews. Two-phase commit is used in a distributed environment where multiple processes take part in the distributed transaction process. In simple word we can understand like if any transaction is executing and it will affect multiple databases then a 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 the different types of Statements in JDBC?

Answer :  This is another classical JDBC interview question. Variants are Difference between Statements, PreparedStatemetn and CallableStatement in Java. Statement object is used to send SQL query to the database and get result from the database, and we get statement object from the connection object.

There are three types of statements:

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

2. PreparedStatement: when we are using the 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 the 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 non-scrollable 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. The cold back is the backup technique in which backup of files are taken before the database is restarted. In hot backup of files and table is taken at the same time when the 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 is 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 issues using the record. if two users are reading the same record then there is no issue but what if users are updating the record, in this case, changes done by the first user is gone by the second user if he also updates the same record .so we need some type of locking so no lost update.

Optimistic Locking: optimistic locking lock the record only when an update takes 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 the JDBC-ODBC Bridge.

That’s all on this list of 10 JDBC Interview questions with answers. As I said JDBC API and its concepts are integral parts of any Java interview and there is always at least one question from JDBC. Since the most application uses a database in the backend, JDBC becomes critical for any Java developer.


Other Java Interview articles you may find useful

4 comments:

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

    ReplyDelete
  2. 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?

    ReplyDelete
  3. JDBC is too old, use Hibernate or iBatis

    ReplyDelete
  4. 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.

    ReplyDelete