You are on page 1of 2

Experiment No 19:-Write a program to demonstrate the use of

PreparedStatment and ResultSetInterface.

1. Explain the advantage of prepared Statement Interface?

Ans:-
• PreparedStatement prevents SQL injection threats as it automatically escapes the special
characters.
• Precompiles and caches the SQL statement for faster execution and to reuse the same
SQL statement in batch.
• PreparedStatement provides clear separation between the query code and the parameter
values that improves readability.
• PreparedStatement provides convenient way to transform Java object types to SQL Data
types to pass input parameters.
2. Explain the methods of ResultSet Interface ?
Ans:-

ResultSet Interface Methods.

Methods Description
public boolean absolute(int row) Moves the cursor to the specified row in the ResultSet object.
It moves the cursor just before the first row i.e. front of the
public void beforeFirst( )
ResultSet.
Moves the cursor to the end of the ResultSet object, just after
public void afterLast()
the last row.
public boolean first() Moves the cursor to first value of ResultSet object.
public boolean last( ) Moves the cursor to the last row of the ResultSet object.
Just moves the cursor to the previous row in the ResultSet
public boolean previous ( )
object.
public boolean next( ) It moves the curser forward one row from its current position.
It retrieves the value of the column in current row as int in
public intgetInt(intcolIndex)
given ResultSet object.
public String getString( It retrieves the value of the column in current row as int in
intcolIndex) given ResultSet object.
public void relative(int rows) It moves the cursor to a relative number of rows.

3. Explain types of ResultSet?

Ans:- There are three types of ResultSets.

1] TYPE_FORWARD_ONLY:

The result set cannot be scrolled; its cursor moves forward only, from before the first row to
after the last row. The rows contained in the result set depend on how the underlying database
generates the results. That is, it contains the rows that satisfy the query at either the time the
query is executed or as the rows are retrieved.
TYPE_SCROLL_INSENSITIVE:

The result can be scrolled; its cursor can move both forward and backward relative to the
current position, and it can move to an absolute position. The result set is insensitive to
changes made to the underlying data source while it is open. It contains the rows that satisfy
the query at either the time the query is executed or as the rows are retrieved.

TYPE_SCROLL_SENSITIVE:

The result can be scrolled; its cursor can move both forward and backward relative to the
current position, and it can move to an absolute position. The result set reflects changes made
to the underlying data source while the result set remains open.

The default ResultSet type is TYPE_FORWARD_ONLY.

4. Explain disadvantages of Prepared Statement.


Ans:-
Disadvantages
We can use PreparedStatement for only one sql query (Like CDMA Phone), but we can
use simple Statement to work with any number of queries (Like GSM Phone).

E.g:
Statement st = con.createStatement();
st.executeUpdate("insert employees...");
st.executeUpdate("update employees...");
st.executeUpdate("delete employees...");

Here We Are Using One Statement Object to Execute 3 Queries But,

PreparedStatement pst = con.prepareStatement("insert into employees...");

Here PreparedStatement object is associated with only insert query.

5. State the use and syntax of following method of resultset interface that involve moving
the cursor.
1] beforefirst() 2] afterlast() 3] first() 4] last()
Answer:-
1] This ResultSet method set cursor to before first record. It sets the cursor position to 0. It
indicates whether the cursor is before the first row in this ResultSet object.
Syntax: ResultSet rs; void rs.beforeFirst();

2] This ResultSet method set cursor to after last record.


Syntax: ResultSet rs; void rs.afterLast();

3) public boolean first(): is used to move the cursor to the first row in result set object.
Syntax: ResultSet rs; void rs.first();

4) public boolean last(): is used to move the cursor to the last row in result set object.
Syntax: ResultSet rs; void rs.last();

You might also like