Home  Interview Questions  Certifications  Aptitude Questions  Tutorials  Placement Papers  Search  Resume  Soft Skills  Video  Forum  Blog

Android app on Google Play

Technical Interview Questions
Javascript Interview Questions
Oracle Interview Questions
J2EE Interview Questions
C++ Interview Questions
XML Interview Questions
EJB Interview Questions
JSP Interview Questions

Programming Source Codes
Java Source Codes
Html Source Codes
CSS Source Codes
C Source Codes

Soft Skills
Communication Skills
Leadership Skills




JDBC Interview Questions and Answers

How does a custom RowSetReader get called from a CachedRowSet?
The Reader must be registered with the CachedRowSet using CachedRowSet.setReader(javax.sql.RowSetReader reader). Once that is done, a call to CachedRowSet.execute() will, among other things, invoke the readData method.

How do I implement a RowSetReader? I want to populate a CachedRowSet myself and the documents specify that a RowSetReader should be used. The single method accepts a RowSetInternal caller and returns void. What can I do in the readData method?
"It can be implemented in a wide variety of ways..." and is pretty vague about what can actually be done. In general, readData() would obtain or create the data to be loaded, then use CachedRowSet methods to do the actual loading. This would usually mean inserting rows, so the code would move to the insert row, set the column data and insert rows. Then the cursor must be set to to the appropriate position.

How can I instantiate and load a new CachedRowSet object from a non-JDBC source?
The basics are:
* Create an object that implements javax.sql.RowSetReader, which loads the data.
* Instantiate a CachedRowset object.
* Set the CachedRowset's reader to the reader object previously created.
* Invoke CachedRowset.execute().

Note that a RowSetMetaData object must be created, set up with a description of the data, and attached to the CachedRowset before loading the actual data.
The following code works with the Early Access JDBC RowSet download available from the Java Developer Connection and is an expansion of one of the examples:
// Independent data source CachedRowSet Example
import java.sql.*;
import javax.sql.*;
import sun.jdbc.rowset.*;

public class RowSetEx1 implements RowSetReader
CachedRowSet crs;
int iCol2;
RowSetMetaDataImpl rsmdi;
String sCol1,

public RowSetEx1()
crs = new CachedRowSet();
crs.execute(); // load from reader

"Fetching from RowSet...");
} // end while next

if(crs.isAfterLast() == true)
"We have reached the end");
System.out.println("crs row: " +

"And now backwards...");

} // end while previous

if(crs.isBeforeFirst() == true)
{ System.out.println(
"We have reached the start");

if(crs.isFirst() == true)
{ System.out.println(
"We have moved to first");

System.out.println("crs row: " +

if(crs.isBeforeFirst() == false)
{ System.out.println(
"We aren't before the first row."); }

if(crs.isLast() == true)
{ System.out.println(
"...and now we have moved to the last");

System.out.println("crs row: " +

if(crs.isAfterLast() == false)
"we aren't after the last.");

} // end try
catch (SQLException ex)
System.err.println("SQLException: " +

} // end constructor

public void showTheData() throws SQLException
sCol1 = crs.getString(1);
if(crs.wasNull() == false)
{ System.out.println("sCol1: " + sCol1); }
else { System.out.println("sCol1 is null"); }

iCol2 = crs.getInt(2);
if (crs.wasNull() == false)
{ System.out.println("iCol2: " + iCol2); }
else { System.out.println("iCol2 is null"); }

sCol3 = crs.getString(3);
if (crs.wasNull() == false)
System.out.println("sCol3: " +
sCol3 + "\n" );
{ System.out.println("sCol3 is null\n"); }

} // end showTheData

// RowSetReader implementation
public void readData(RowSetInternal caller)
throws SQLException
rsmdi = new RowSetMetaDataImpl();
rsmdi.setColumnType(1, Types.VARCHAR);
rsmdi.setColumnType(2, Types.INTEGER);
rsmdi.setColumnType(3, Types.VARCHAR);
crs.setMetaData( rsmdi );


crs.updateString( 1, "StringCol11" );
crs.updateInt( 2, 1 );
crs.updateString( 3, "StringCol31" );

crs.updateString( 1, "StringCol12" );
crs.updateInt( 2, 2 );
crs.updateString( 3, "StringCol32" );


} // end readData

public static void main(String args[])
new RowSetEx1();

} // end class RowSetEx1

Page Numbers :   1       2       3        4       5       6       7       8       9       10      11       12      13       14       15      16       17       18      19      20

Have a Question ? post your questions here. It will be answered as soon as possible.

Check Java Interview Questions for more Java Interview Questions with answers

Check Structs Interview Questions for more Structs Interview Questions with answers

Check Servlet Interview Questions for more Servlet Interview Questions with answers