JDBC Interview Questions:-

  1. What is JDBC?

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.

JFor this we need database specific JDBC drivers.


  2. What are the main steps in java to make JDBC connectivity?

Another beginner level JDBC Interview question, mostly asked on telephonic interviews. Here are main steps to connect to database.

1. Load the Driver: First step is to load the database specific driver which communicates with database.

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

3. Get Statement object: From connection object we can get statement object which is used to query the database

4. Execute the Query:Using statement object we execute the SQL or database query and get result set from the query.

5. Close the connection:After getting resultset and all required operation performed the last step should be closing the database connection.


  3. What is the mean of "dirty read" in database?

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.


  4. What is 2 phase commit?

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 .


  5. What are different types of Statement?

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();

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();


  6. How cursor works in scrollable result set?

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.

1. TYPE_FORWARD_ONLY: creates a nonscrollable result set, that is, one in which the cursor moves only forward

2. TYPE_SCROLL_INSENSITIVE : a scrollable result set does not reflects changes that are made to it while it is open

3. TYPE_SCROLL_SENSITIVE: a scrollable result set reflects changes that are made to it while it is open


  7. What is connection pooling?

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.


  8. How to setup JDBC Connection Pool using Spring in Java

Setting up JDBC Database Connection Pool in Spring framework is easy for any Java application, just matter of changing few configuration in spring configuration file.

If you are writing core java application and not running on any web or application server like Tomcat or Weblogic, Managing Database connection pool using Apache Commons DBCP and Commons Pool along-with Spring framework is nice choice but if you have luxury of having web server and managed J2EE Container, consider using Connection pool managed by J2EE server those are better option in terms of maintenance, flexibility and also help to prevent java.lang.OutofMemroyError:PermGen Space in tomcat by avoiding loading of JDBC driver in web-app class-loader, Also keeping JDBC connection pool information in Server makes it easy to change or include settings for JDBC over SSL.

This article is in continuation of my tutorials on spring framework and database like LDAP Authentication in J2EE with Spring Security and manage session using Spring security If you haven't read those article than you may find them useful.


  9. What do you mean by cold backup, hot backup?

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.


  10. What are the locking system in JDBC?

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.


PHP

PHP is a server-side scripting language designed primarily for web development but is also used as a general-purpose programming language..

Read more