Java J2ee Struts Hibernate Spring Interview Questions Java J2ee Struts Hibernate Spring Interview Questions

1. 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.
Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
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

2. What is the difference between Resultset and Rowset?

Difference between Resultset and 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.

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.

3. Can you explain about Cursor ?

A cursor is a pointer used to fetch rows from a result set. One can think of a cursor as a data structure that describes the results returned from a SQL SELECT statement. One of the variables in this structure is a pointer to the next record to be fetched from the query results.

Note that if you do repetitive stuff inside a loop and you fail to close your cursors, you would soon run into the ORA-01000: maximum number of open cursors exceeded error.

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

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


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");; 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 ( {
. . . // 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(
int count = 0;
while ( {
System.out.println("Company XYZ has " + count " employees.");
rs = stmt.executeQuery(
while ( {
. . . // retrieve first and last names of each employee

5. What is the difference between response.sendRedirect( ) and RequestDispatcher.forward( ) methods in Servlets ?

A sendRedirect method creates a new or fresh request and it is also reflected in the browser URL. where as forward method forwards the same request to the new target(hence the change is NOT reflected in browser's URL).
The previous request scope objects are no longer available after a redirect because it results in a new request, but it is available in forward.
response.sendRedirect() is slower compared to RequestDispatcher.forward() method.
The main difference is forward() method only work for the resource which is in the same web container. This forward() will not work for a resource which is there in other web container, i.e if you want to forward the request to another resource in different web container you must use response.sendRedirect().
forward executes on the server while sendRedirect() executes on the client.
forward() method will create Refresh button problem as the URL will not change, where as in response.sendRedirect() you will not get any refresh button problem as the URL changes to a default page.

6. How do you prevent the Creation of a Session in a JSP Page and why?

By default, a JSP page will automatically create a session for the request if one does not exist. However, sessions consume resources and if it is not
necessary to maintain a session, one should not be created. For example, a marketing campaign may suggest the reader visit a web page for more
information. If it is anticipated that a lot of traffic will hit that page, you may want to optimize the load on the machine by not creating useless
The page directive is used to prevent a JSP page from automatically creating a session: <%@ page session="false">

7. Is JSP technology extensible ?

YES. JSP technology is extensible through the development of custom actions, or tags, which are encapsulated in tag libraries

8. What advantage is there to using prepared statements if I am using connection pooling or closing the connection frequently to avoid resource or connection or cursor limitations?

Java J2ee Struts Hibernate Spring Interview Questions
Menu - - Java J2ee Struts Spring Hibernate Interview Questions - A home for your website
Ads By Google