JavaSpartans
Java J2ee Struts Hibernate Spring Interview Questions Java J2ee Struts Hibernate Spring Interview Questions
1 2 3 4 5    Next

1. What is the difference between Resultset and Rowset?

Difference between Resultset and Rowset
RowSet
The interface that adds support to the JDBC API for the JavaBeansTM component model. A rowset, which can be used as a JavaBeans component
in a visual Bean development environment, can be created and configured at design time and executed at run time.

The RowSet interface provides a set of JavaBeans properties that allow a RowSet instance to be configured to connect to a JDBC data source and
read some data from the data source. A group of setter methods (setInt, setBytes, setString, and so on) provide a way to pass input parameters
to a rowset's command property. This command is the SQL query the rowset uses when it gets its data from a relational database, which is
generally the case.

The RowSet interface supports JavaBeans events, allowing other components in an application to be notified when an event occurs on a
rowset, such as a change in its value.

The RowSet interface is unique in that it is intended to be implemented using the rest of the JDBC API. In other words, a RowSet implementation
is a layer of software that executes "on top" of a JDBC driver. Implementations of the RowSet interface can be provided by anyone,
including JDBC driver vendors who want to provide a RowSet implementation as part of their JDBC products.
A RowSet object may make a connection with a data source and maintain that connection throughout its life cycle, in which case it is called a
connected rowset. A rowset may also make a connection with a data source, get data from it, and then close the connection. Such a rowset
is called a disconnected rowset. A disconnected rowset may make changes to its data while it is disconnected and then send the changes back to
the original source of the data, but it must reestablish a connection to do so.

ResultSet
A table of data representing a database result set, which is usually generated by executing a statement that queries the database.

A ResultSet object maintains a cursor pointing to its current row of data. Initially the cursor is positioned before the first row. The next
method moves the cursor to the next row, and because it returns false when there are no more rows in the ResultSet object, it can be used in
a while loop to iterate through the result set.

A default ResultSet object is not updatable and has a cursor that moves forward only. Thus, you can iterate through it only once and only from
the first row to the last row. It is possible to produce ResultSet objects that are scrollable and/or updatable. The following code
fragment, in which con is a valid Connection object, illustrates how to make a result set that is scrollable and insensitive to updates by
others, and that is updatable. See ResultSet fields for other options.


2. How do you find the size of the resultset in JDBC ?

To find the size of the resultset you should traverse or loop through from that resultset atleast once. There is no direct method or function to find the size of the resultset in JDBC API. They can provide that method like ResultSet.getSize() or ResultSet.size() but after using that method you will be at the end of that resultset. That is the problem.You should write a separate query like select count(*) from table_name; to find the size of the result set.
For more information follow the link
http://java.sun.com/j2se/1.4.2/docs/guide/jdbc/getstart/resultset.html

6.1.4 Determining the Number of Rows in a Result Set

With the new cursor movement methods, it is easy to see how many rows a scrollable ResultSet object contains. All that is necessary is to go to the last row of the result set and get the number of that row. In the following example, rs will have one row for each employee.

ResultSet rs = stmt.executeQuery("SELECT LAST_NAME, FIRST_NAME FROM EMPLOYEES");

rs.last();

int numberOfRows = rs.getRow();

System.out.println("XYZ, Inc. has "
+ numberOfRows + " employees"); rs.beforeFirst(); while (next()) { . . . // retrieve first and last names of each employee
}

Though not as convenient, it is also possible to find out how many rows a nonscrollable result set has. The following example shows one way to determine the number of rows.

ResultSet rs = stmt.executeQuery(
"SELECT COUNT(*) FROM EMPLOYEES"); rs.next(); int count = rs.getInt(1); System.out.println("XYZ, Inc. has " + count
+ " employees");
ResultSet rs2 = stmt.executeQuery( "SELECT LAST_NAME, FIRST_NAME FROM EMPLOYEES"); while (rs2.next()) {
. . . // retrieve first and last names of each employee
}

With the scrollable result set, the cursor was just repositioned to start iterating through the same result set to retrieve its data. In the preceding example, however, one query is needed to get the count, and another query is needed to get a result set with the data that is desired. Both queries must, of course, produce result sets of the same size for the count to be accurate.

A second way to determine the number of rows in a forward-only result set is to iterate through the result set, incrementing a variable with each iteration, which is shown in the following example. Because an application can iterate through a forward-only result set just once, the same query needs to be executed twice. In the iteration through the first rs, the number of rows is counted; in the iteration through the second rs, the data is retrieved.

ResultSet rs = stmt.executeQuery(
"SELECT LAST_NAME, FIRST_NAME FROM EMPLOYEES");
int count = 0;
while (rs.next()) {
count++;
}
System.out.println("Company XYZ has " + count " employees.");
rs = stmt.executeQuery(
"SELECT LAST_NAME, FIRST_NAME FROM EMPLOYEES");
while (rs.next()) {
. . . // retrieve first and last names of each employee
}


3. What is a rowset in JDBC ?

A RowSet is an object that encapsulates a set of rows from either Java Database Connectivity (JDBC) result sets or tabular data sources like a file or spreadsheet. RowSets support component-based development models like JavaBeans, with a standard set of properties and an event notification mechanism.


4. How can you Move the Cursor in Scrollable Result Sets ?

One of the new features in the JDBC 2.0 API is the ability to move a result sets cursor backward as well as forward. There are also methods that let you move the cursor to a particular row and check the position of the cursor.
Eg.
Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_READ_ONLY);
ResultSet srs = stmt.executeQuery("SELECT COF_NAME, PRICE FROM COFFEES");
The first argument is one of three constants added to the ResultSet API to indicate the type of a ResultSet object TYPE_FORWARD_ONLY, TYPE_SCROLL_INSENSITIVE , and TYPE_SCROLL_SENSITIVE .
The second argument is one of two ResultSet constants for specifying
whether a result set is read-only or updatable: CONCUR_READ_ONLY and CONCUR_UPDATABLE . The point to remember here is that if you specify a type, you must also specify whether it is read-only or updatable. Also, you must specify the type first, and because both parameters are of
type int , the compiler will not complain if you switch the order.
Specifying the constant TYPE_FORWARD_ONLY creates a nonscrollable result set, that is, one in which the cursor moves only forward. If you do not specify any constants for the type and updatability of a ResultSet object, you will automatically get one that is TYPE_FORWARD_ONLY and CONCUR_READ_ONLY


5. How to Make Updates to Updatable Result Sets?

Another new feature in the JDBC 2.0 API is the ability to update rows in a result set using methods in the Java programming language rather than having to send an SQL command. But before you can take advantage of this capability, you need to create a ResultSet object that is updatable. In order to do this, you supply the ResultSet constant CONCUR_UPDATABLE to the createStatement method.
Connection con= DriverManager.getConnection("jdbc:mySubprotocol:mySubName");
Statement stmt = con.createStatement ( ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
ResultSet uprs  = stmt.executeQuery("SELECT COF_NAME, PRICE FROM COFFEES");


6. Can you explain ResultSet, RowSet, CachedRowset, JdbcRowset and WebRowSet relationship ?



7. Explain the new Features of JDBC 2.0 Core API?

The JDBC 2.0 API includes the complete JDBC API, which includes both core and Optional Package API, and provides industrial-strength database computing capabilities.
New Features in JDBC 2.0 Core API:
? New data types - interfaces mapping the SQL3 data types
? Custom mapping of user-defined types (UTDs)
? Scrollable result sets- using new methods in the ResultSet interface allows programmatically move the to particular row or to a position relative to its current position
? Java applications can now use the ResultSet.updateXXX methods.
? JDBC 2.0 Core API provides the Batch Updates functionality to the java applications.
? Miscellaneous features, including performance hints, the use of character streams, full precision for java.math.BigDecimal values, additional security, and support for time zones in date, time, and timestamp values.


8. What are the different types of JDBC Drivers?

Type 1:
A JDBC-ODBC bridge provides JDBC API access via one or more ODBC drivers. Note that some ODBC native code and in many cases native database client code must be loaded on each client machine that uses this type of driver. Hence, this kind of driver is generally most appropriate when automatic installation and downloading of a Java technology application is not important.
Type 2:
A native-API partly Java technology-enabled driver converts JDBC calls into calls on the client API for Oracle, Sybase, Informix, DB2, or other DBMS. Note that, like the bridge driver, this style of driver requires that some binary code be loaded on each client machine.
Type 3:
A net-protocol fully Java technology-enabled driver translates JDBC API calls into a DBMS-independent net protocol which is then translated to a DBMS protocol by a server. This net server middleware is able to connect all of its Java technology-based clients to many different databases. The specific protocol used depends on the vendor. In general, this is the most flexible JDBC API alternative. It is likely that all vendors of this solution will provide products suitable for Intranet use. In order for these products to also support Internet access they must handle the additional requirements for security, access through firewalls, etc., that the Web imposes. Several vendors are adding JDBC technology-based drivers to their existing database middleware products.
Type 4:
A native-protocol fully Java technology-enabled driver converts JDBC technology calls into the network protocol used by DBMSs directly. This allows a direct call from the client machine to the DBMS server and is a practical solution for Intranet access. Since many of these protocols are proprietary the database vendors themselves will be the primary source for this style of driver. Several database vendors have these in progress.


9. Explain Type1, Type2, Type3 and Type4 drivers in JDBC ?

Type 1:
A JDBC-ODBC bridge provides JDBC API access via one or more ODBC drivers. Note that some ODBC native code and in many cases native database client code must be loaded on each client machine that uses this type of driver. Hence, this kind of driver is generally most appropriate when automatic installation and downloading of a Java technology application is not important.
Type 2:
A native-API partly Java technology-enabled driver converts JDBC calls into calls on the client API for Oracle, Sybase, Informix, DB2, or other DBMS. Note that, like the bridge driver, this style of driver requires that some binary code be loaded on each client machine.
Type 3:
A net-protocol fully Java technology-enabled driver translates JDBC API calls into a DBMS-independent net protocol which is then translated to a DBMS protocol by a server. This net server middleware is able to connect all of its Java technology-based clients to many different databases. The specific protocol used depends on the vendor. In general, this is the most flexible JDBC API alternative. It is likely that all vendors of this solution will provide products suitable for Intranet use. In order for these products to also support Internet access they must handle the additional requirements for security, access through firewalls, etc., that the Web imposes. Several vendors are adding JDBC technology-based drivers to their existing database middleware products.
Type 4:
A native-protocol fully Java technology-enabled driver converts JDBC technology calls into the network protocol used by DBMSs directly. This allows a direct call from the client machine to the DBMS server and is a practical solution for Intranet access. Since many of these protocols are proprietary the database vendors themselves will be the primary source for this style of driver. Several database vendors have these in progress.


10. What is the difference between JDBC and ODBC?

ODBC is for Microsoft and JDBC is for Java applications. ODBC can?t be directly used with Java because it uses a C interface. ODBC makes use of pointers which have been removed totally from Java. ODBC mixes simple and advanced features together and has complex options for simple queries. But JDBC is designed to keep things simple while allowing advanced capabilities when required. ODBC requires manual installation of the ODBC driver manager and driver on all client machines. JDBC drivers are written in Java and JDBC code is automatically installable, secure, and portable on all platforms. JDBC API is a natural Java interface and is built on ODBC. JDBC retains some of the basic features of ODBC.


Java J2ee Struts Hibernate Spring Interview Questions
1 2 3 4 5    Next
Menu - javaspartans.com - Java J2ee Struts Spring Hibernate Interview Questions
Hostens.com - A home for your website
Ads By Google