JDBC Interview Questions

What is a data source ?

A DataSource class brings another level of abstraction than directly using a

connection object. Data source can be referenced by JNDI. Data Source may point to RDBMS, file

System , any DBMS etc

Explain the 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.

How do we retrieve warning ?

SQLWarning objects are a subclass of SQLException that deal with database

access warnings. Warnings do not stop the execution of an application, as exceptions do.
They simply alert the user that something did not happen as planned. A

warning can be reported on a Connection object, a Statement object (including PreparedStatement

and CallableStatement objects), or a ResultSet object. Each of these classes has a getWarnings

method, which you must invoke in order to see the first warning reported on the calling

object.

E.g.

SQLWarning warning = stmt.getWarnings();

if (warning != null) {

while (warning != null) {

System.out.println(“Message: ” + warning.getMessage());
System.out.println(“SQLState: ” + warning.getSQLState());
System.out.print(“Vendor error code: “);
System.out.println(warning.getErrorCode());
warning = warning.getNextWarning();

}

}

How many statements can we create with one connection ?

There is no such limit on number of statements to be created

How to make updated to the  Updatable Result Sets ?

Usinf JDBC 2.0 API we have the ability to update rows in a result set. For

this we need to create a ResultSet object that is updatable.
For this, we pass the ResultSet constant CONCUR_UPDATABLE to the

createStatement method.
Connection con =DriverManager.getConnection(url, “myLogin”,

“myPassword”);
Statement stmt =con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,

ResultSet.CONCUR_UPDATABLE);
ResultSet uprs =stmt.executeQuery(“SELECT NAME, SALARY FROM

EMPLOYEES”);

How can you move the cursor in scrollable result sets

In JDBC 2.0 API we have the ability to move a result set�s cursor backward

as well as forward.
We can also move the cursor to a particular row and check the position of

the cursor.
Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,

ResultSet.CONCUR_READ_ONLY);
ResultSet srs = stmt.executeQuery(‘SELECT NAME, SALARY FROM

EMPLOYEES’);
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.

Make sure that when you specify a type, you must also specify whether it is

read-only or updatable. 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.

What is the query used to display all tables names in SQL Server (Query analyzer)?

select * from information_schema.tables

How many types of JDBC Drivers are present and what are they?

– There are 4 types of JDBC Drivers

JDBC-ODBC Bridge Driver
Native API Partly Java Driver
Network protocol Driver
JDBC Net pure Java Driver

I made my class Cloneable buts still I am not able to access protected method clone. Why?

Some of the Java books imply that all you have to do in order to have your class support

clone() is implement the Cloneable interface. Not so. Perhaps that was the intent at some point,

but that�s not the way it works currently. As it stands, you have to implement your own public

clone() method, even if it doesn�t do anything special and just calls super.clone().Why is XML

such an important development?- It removes two constraints which were holding back Web

developments: dependence on a single, inflexible document type (HTML) which was being much

abused for tasks it was never designed for; the complexity of full SGML, whose syntax allows

many powerful but hard-to-program options. XML allows the flexible development of user-defined

document types. It provides a robust, non-proprietary, persistent, and verifiable file format

for the storage and transmission of text and data both on and off the Web; and it removes the

more complex options of SGML, making it easier to program for.

What is the fastest type of JDBC driver?

JDBC driver performance will depend on a number of issues:

the quality of the driver code,
the size of the driver code,
the database server and its load,
network topology,
the number of times your request is translated to a different API.
In general, all things being equal, you can assume that the more your

request and response change hands, the slower it will be. This means that Type 1 and Type 3

drivers will be slower than Type 2 drivers (the database calls are make at least three

translations versus two), and Type 4 drivers are the fastest (only one translation).

How do I find whether a parameter exists in the request object?

boolean hasFoo = !(request.getParameter(“foo”) == null
|| request.getParameter

(“foo”).equals(“”));

or

boolean hasParameter =
request.getParameterMap().contains

(theParameter); //(which works in Servlet 2.3+)
How can I send user authentication information while makingURLConnection?-

You will  want to use HttpURLConnection.setRequestProperty and set all the appropriate headers

to HTTP authorization.

What is the difference between TYPE_SCROLL_INSENSITIVE , and TYPE_SCROLL_SENSITIVE ?

You will get a scrollable ResultSet object if you specify one of these

ResultSet constants.The difference between the two has to do with whether a result set reflects

changes that are made to it while it is open and whether certain methods can be called to detect

these changes. Generally speaking, a result set that is TYPE_SCROLL_INSENSITIVE does not reflect

changes made while it is still open and one that is TYPE_SCROLL_SENSITIVE does. All three types

of result sets will make changes visible if they are closed and then reopened:
Statement stmt =
con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,

ResultSet.CONCUR_READ_ONLY);
ResultSet srs =
stmt.executeQuery(“SELECT NAME, SALARY FROM PERSON”);
srs.afterLast();
while (srs.previous())
{
String name = srs.getString(“NAME”);
float salary = srs.getFloat(“SALARY”);
System.out.println(name + ” ” + salary);
}

How do you insert images in Database using JDBC ?

We can store images in the databse using the BLOB datatype where in the

image is stored as a byte stream

What is Metadata ?

It is information about one of two things: Database information

(java.sql.DatabaseMetaData), or Information about a specific ResultSet

(java.sql.ResultSetMetaData). Use DatabaseMetaData to find information about your database, such

as its capabilities and structure. Use ResultSetMetaData to find information about the results

of an SQL query, such as size and types of columns