Wednesday, April 2, 2008

JDBC interview questions - Part 1

Here is a compilation of some frequently asked JDBC questions in job interviews

1. What is JDBC?

Ans : JDBC stands for Java Database Connectivity. It is an API which provides easy connection to a wide range of databases. To connect to a database we need to load the appropriate driver and then request for a connection object.

The Class.forName(….) will load the driver and register it with the DriverManager

2. What is a Transaction?

Ans: A transaction is a set of operations that should be completed as a unit. If one operation fails then all the other operations fail as well.

3. What are the steps in the JDBC connection?

Ans : While making a JDBC connection we go through the following steps :

Step 1 : Register the database driver by using :
Class.forName(\" driver classs for that specific database\" );

Step 2 : Now create a database connection using :
Connection con = DriverManager.getConnection(url,username,password);

Step 3: Now Create a query using :
Statement stmt = Connection.Statement(\"select * from TABLE NAME\");

Step 4 : Exceute the query :
stmt.exceuteUpdate();


4. What is the difference between statements and prepared statements?

Ans : Prepared statements offer better performance, as they are pre-compiled. Prepared statements reuse the same execution plan for different arguments rather than creating a new execution plan every time. Prepared statements use bind arguments, which are sent to the database engine. This allows mapping different
requests with same prepared statement but different arguments to execute the same execution plan.

5. Which are te types of JDBC drivers ?

Ans: a. JDBC-ODBC Bridge Driver (Type-1 driver)
b. Native API Partly Java Driver (Type-2 driver)
c. Net protocol pure Java Driver (Type-3 driver)
d. Native protocol Pure Java Driver (Type-4 driver)

6.What are the types of resultsets in JDBC3.0? How you can retrieve information of resultset?

Ans: ScrollableResultSet and ResultSet. We can retrieve information of resultset by using java.sql.ResultSetMetaData interface. You can get the instance by calling the method getMetaData() on ResulSet object.

7. How can we store java objects in database ?

Ans: We can store java objects by using setObject(), setBlob() and setClob() methods in PreparedStatement.

8. What do you mean by isolation level?

Ans: Isolation means that the business logic can proceed without consideration for the other activities of the system.

9. List some exceptions thrown by JDBC

A. BatchUpdateException , DataTruncation , SQLException , SQLWarning

10. In which interface the methods commit() & rollback() are defined ?

Ans: java.sql.Connection interface

11. How to binary data is stored in the database ?

Ans: The binary data such as images are stored as binary streams
- getBinaryStream(), setBinaryStream()). It will not be visible in database, it is stored in form of bytes, to make it visible we have to use any one frontend tool.

12. How to check null value in JDBC?

A. By using the method wasNull() in ResultSet ,it returns boolean value. Returns whether the last column read had a value of SQL NULL. Note that you must first call one of the getXXX methods on a column to try to read its value and then call the method wasNull to see if the value read was SQL NULL.

No comments: