Saturday, September 5, 2009

JDBC interview Questions


What are types of JDBC drivers?

There are four types of drivers defined by JDBC as follows:

  • Type 1: JDBC/ODBCThese require an ODBC (Open Database Connectivity) driver for the database to be installed. This type of driver works by translating the submitted queries into equivalent ODBC queries and forwards them via native API calls directly to the ODBC driver. It provides no host redirection capability.
  • Type2: Native API (partly-Java driver)This type of driver uses a vendor-specific driver or database API to interact with the database. An example of such an API is Oracle OCI (Oracle Call Interface). It also provides no host redirection.
  • Type 3: Open Protocol-NetThis is not vendor specific and works by forwarding database requests to a remote database source using a net server component. How the net server component accesses the database is transparent to the client. The client driver communicates with the net server using a database-independent protocol and the net server translates this protocol into database calls. This type of driver can access any database.
  • Type 4: Proprietary Protocol-Net(pure Java driver)—This has a same configuration as a type 3 driver but uses a wire protocol specific to a particular vendor and hence can access only that vendor's database. Again this is all transparent to the client.

Note: Type 4 JDBC driver is most preferred kind of approach in JDBC.

Which type of JDBC driver is the fastest one?

JDBC Net pure Java driver(Type IV) is the fastest driver because it converts the JDBC calls into vendor specific protocol calls and it directly interacts with the database.


Does the JDBC-ODBC Bridge support multiple concurrent open statements per connection?

No. You can open only one Statement object per connection when you are using the JDBC-ODBC Bridge.


Which is the right type of driver to use and when?

  • Type I driver is handy for prototyping
  • Type III driver adds security, caching, and connection control
  • Type III and Type IV drivers need no pre-installation

Note: Preferred by 9 out of 10 Java developers: Type IV. Click here to learn more about JDBC drivers.


What are the standard isolation levels defined by JDBC?

The values are defined in the class java.sql.Connection and are:

  • TRANSACTION_NONE
  • TRANSACTION_READ_COMMITTED
  • TRANSACTION_READ_UNCOMMITTED
  • TRANSACTION_REPEATABLE_READ
  • TRANSACTION_SERIALIZABLE

Any given database may not support all of these levels.


What is resultset ?

The ResultSet represents set of rows retrieved due to query execution.

               ResultSet rs = stmt.executeQuery(sqlQuery);

 

What are the types of resultsets?

The values are defined in the class java.sql.Connection and are:

  • TYPE_FORWARD_ONLY specifies that a resultset is not scrollable, that is, rows within it can be advanced only in the forward direction.
  • TYPE_SCROLL_INSENSITIVE specifies that a resultset is scrollable in either direction but is insensitive to changes committed by other transactions or other statements in the same transaction.
  • TYPE_SCROLL_SENSITIVE specifies that a resultset is scrollable in either direction and is affected by changes committed by other transactions or statements within the same transaction.

Note: A TYPE_FORWARD_ONLY resultset is always insensitive.


What’s the difference between TYPE_SCROLL_INSENSITIVE and TYPE_SCROLL_SENSITIVE?

TYPE_SCROLL_INSENSITIVE

TYPE_SCROLL_SENSITIVE

An insensitive  resultset is like the snapshot of the data in the database when query was executed.

A sensitive resultset does NOT represent a snapshot of data, rather it contains points to those rows which satisfy the query condition.

After we get the resultset the changes made to data are not visible through the resultset, and hence they are known as insensitive.

After we obtain the resultset if the data is modified then such modifications are visible through resultset.

Performance not effected with insensitive.

Since a trip is made for every ‘get’ operation, the performance drastically get affected.

1

2

3

4

5

6

7

8

 

No comments:

Post a Comment