These material are compiled for helping junior / senior software engineers and others.

1. What is new in JDBC 2.0?

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.

2. How to move the cursor in scrollable resultsets?(new feature in JDBC 2.0)

a. 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");

b. 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");
        //...

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

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

3. How to update a resultset programmatically? (new feature in JDBC 2.0)

a. 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");

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

4. How to insert and delete a row programmatically? (new feature in JDBC 2.0)

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

5. What is JDBC?

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.

6. What are the two major components of JDBC?

One implementation interface for database manufacturers, the other implementation interface for application and applet writers.

7. What is JDBC Driver interface?

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.

8. What are the common tasks of JDBC?

Create an instance of a JDBC driver or load JDBC drivers through jdbc.drivers
Register a driver
Specify a database
Open a database connection
Submit a query
Receive results
Process results

9. How to use JDBC to connect Microsoft Access?

There is a specific tutorial at javacamp.org. Check it out.

10. What are four types of JDBC driver?

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

11. Which type of JDBC driver is the fastest one?(donated in May, 2005)

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.


Sources :
DEVFYI - Developer Resource - FYI
TechGuider


Click here to get Interview's Topic Index