Java Interview Questions - JDBC

 
Q. What is JDBC?
A. JDBC may stand for Java Database Connectivity. It is also a trade mark. JDBC is a layer of abstraction that allows users to choose between databases. It allows you to change to a different database engine and to write to a single API. JDBC allows you to write database applications in Java without having to concern yourself with the underlying details of a particular database.
Q. What are the two major components of JDBC?
A. One implementation interface for database manufacturers, the other implementation interface for application and applet writers.
Q. What is JDBC Driver interface?
A. The JDBC Driver interface provides vendor-specific implementations of the abstract classes provided by the JDBC API. Each vendor driver must provide implementations of the java.sql.Connection,Statement,PreparedStatement, CallableStatement, ResultSet and Driver.
Q. How to use JDBC to connect Microsoft Access?
A. There is a specific tutorial at javacamp.org. Check it out.

Q. What are four types of JDBC driver? (Job Interview Favourites)
A.
Type 1 Drivers
Bridge drivers such as the jdbc-odbc bridge. They rely on an intermediary such as ODBC to transfer the SQL calls to the database and also often rely on native code. It is not a serious solution for an application

Type 2 Drivers
Use the existing database API to communicate with the database on the client. Faster than Type 1, but need native code and require additional permissions to work in an applet. Client machine requires software to run.

Type 3 Drivers
JDBC-Net pure Java driver. It translates JDBC calls to a DBMS-independent network protocol, which is then translated to a DBMS protocol by a server. Flexible. Pure Java and no native code.

Type 4 Drivers
Native-protocol pure Java driver. It converts JDBC calls directly into the network protocol used by DBMSs. This allows a direct call from the client machine to the DBMS server. It doesn't need any special native code on the client machine.

Recommended by Sun's tutorial, driver type 1 and 2 are interim solutions where direct pure Java drivers are not yet available. Driver type 3 and 4 are the preferred way to access databases using the JDBC API, because they offer all the advantages of Java technology, including automatic installation. For more info, visit Sun JDBC page

Q. Which type of JDBC driver is the fastest one?(donated in May, 2005)
A. 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.

Q. What are the steps in the JDBC connection?
A. 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();

Q. How does a try statement determine which catch clause should be used to handle an exception?
A. When an exception is thrown within the body of a try statement, the catch clauses of the try statement are examined in the order in which they appear. The first catch clause that is capable of handling the exceptionis executed. The remaining catch clauses are ignored.

Q. What is new in JDBC 2.0?
A. With the JDBC 2.0 API, you will be able to do the following:

--Scroll forward and backward in a result set or move to a specific row (TYPE_SCROLL_SENSITIVE,previous(), last(), absolute(), relative(), etc.)
--Make updates to database tables using methods in the Java programming language instead of using SQL commands.(updateRow(), insertRow(), deleteRow(), etc.)
--Send multiple SQL statements to the database as a unit, or batch (addBatch(), executeBatch())
--Use the new SQL3 datatypes as column values like Blob, Clob, Array, Struct, Ref.

Q. How to move the cursor in scrollable resultsets?(new feature in JDBC 2.0)
A.
1. create a scrollable ResultSet object.

Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_READ_ONLY);
ResultSet srs = stmt.executeQuery("SELECT COLUMN_1, COLUMN_2 FROM TABLE_NAME");

2. use a built in methods like afterLast(), previous(), beforeFirst(), etc. to scroll the resultset.

srs.afterLast();
while (srs.previous()) {
String name = srs.getString("COLUMN_1");
float salary = srs.getFloat("COLUMN_2");
//...
}

3. to find a specific row, use absolute(), relative() methods.

srs.absolute(4); // cursor is on the fourth row
int rowNum = srs.getRow(); // rowNum should be 4
srs.relative(-3);
int rowNum = srs.getRow(); // rowNum should be 1
srs.relative(2);
int rowNum = srs.getRow(); // rowNum should be 3

4. use isFirst(), isLast(), isBeforeFirst(), isAfterLast() methods to check boundary status.

Q. How to update a resultset programmatically? (new feature in JDBC 2.0)
A.
1. create a scrollable and updatable ResultSet object.

Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
ResultSet uprs = stmt.executeQuery("SELECT COLUMN_1, COLUMN_2 FROM TABLE_NAME");

2. move the cursor to the specific position and use related method to update data and then, call updateRow() method.

uprs.last();
uprs.updateFloat("COLUMN_2", 25.55);//update last row's data
uprs.updateRow();//don't miss this method, otherwise, the data will be lost.

Q. How to insert and delete a row programmatically? (new feature in JDBC 2.0)
A. Make sure the resultset is updatable.

1. move the cursor to the specific position.

uprs.moveToCurrentRow();

2. set value for each column.

uprs.moveToInsertRow();//to set up for insert
uprs.updateString("col1" "strvalue");
uprs.updateInt("col2", 5);
...

3. call inserRow() method to finish the row insert process.

uprs.insertRow();

To delete a row: move to the specific position and call deleteRow() method:

uprs.absolute(5);
uprs.deleteRow();//delete row 5

To see the changes call refreshRow();

uprs.refreshRow();

 
Disclaimer                        Feedback                      Suggestions