3. • It is the object of a class that implements java.sql.ResultSet interface
• ResultSet rs=st.executeQuery("select * from students");
• Here executeQuery() returns ResultSet object
4.
5. ResultSet objects
• ResultSet is a java object in our jdbc application which represents the
records selected from the database table
• Resultset object means it is the object of a class that implements
java.sql.ResultSet interface.
• There are two types of ResultSet interface
• 1. Non Scrollable ResultSet object
• 2. Scrollable ResultSet object
6.
7. Non Scrollable ResultSet object
• The Resuleset object that allows to access the records only in one
direction (top to bottom), unidirectionally, and squentially is called Non
scrollable ResultSet object.
• The ResultSet object that we have to be worked so far in our all previous
applications is called Non scrollable ResultSet object.
• To create non scrollable result set object
• Statement st=con.createStatement(0;
• ResultSet rs=st.executeQuery(“select * from emp”);
• Resultset object we must go through remaining all the records of ResultSet
object sequentially.’
• When the ResultSet object has more records, this may kill the perfomance
9. Scrollable ResultSet object
• The ResultSet object that allows to access the record randomly, non
sequentially, bidrectionally and directly is called “Scrollable ResultSet
object”.
• Records of scrollable resultSet objec can be acccessed fastly when
compared to non scrollable resultset object we can go toany record of
scrollable result set object directly without going through its previous
records.
• Based on the parameters that are used to crate statement object, the
statement object takes the decision of creating scrollable or non scrollable
resultSet object.
• Syntax: Statement st=con.createStatement(type,mode);
• ResultSet rs=st.executeQuery(“select * from emp”);
10. ResultSet types(fields)
• We can set the ResultSet type and concurrency to the Statement objct
while we are creating the Statement object.
• Ex:
1.)createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR
_READ_ONLY);
• 2)
prepareStatement(query, ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CO
NCUR_READ_ONLY);
11. ResultSet type( fields)
• static int TYPE_FORWARD_ONLY
• The constant indicating the type for
a ResultSet object whose cursor may move
only forward.
12. ResultSet type( fields)
• static int TYPE_SCROLL_INSENSITIVE
• The constant indicating the type for
a ResultSet object that is scrollable but
generally not sensitive to changes to the data
that underlies the ResultSet.
13. ResultSet type( fields)
• static int TYPE_SCROLL_SENSITIVE
• The constant indicating the type for
a ResultSet object that is scrollable and
generally sensitive to changes to the data that
underlies the ResultSet.
14. ResultSet type( fields)
• TYPE_SCROLL_INSENSITIVE
• static final int TYPE_SCROLL_INSENSITIVE
The constant indicating the type for
a ResultSet object that is scrollable but
generally not sensitive to changes to the data
that underlies the ResultSet.
•
15. • CONCUR_READ_ONLY
• static final int CONCUR_READ_ONLY
The constant indicating the concurrency mode
for a ResultSet object that may NOT be
updated.
16. • CONCUR_UPDATABLE
• static final int CONCUR_UPDATABLE
The constant indicating the concurrency mode
for a ResultSet object that may be updated.
•
17. Example prog on ResultSet Type and
ResultSet Concurrency
•
//Example to demonstrate Scrollable ResultSet
•
•
•
•
•
import java.sql.*;
import java.util.*;
import java.io.*;
public class ScrollableRSEx1 {
public static void main(String s[]) throws Exception {
•
•
Driver d= (Driver) ( Class.forName(
•
•
•
Properties p=new Properties ();
p.put("user","root");
p.put("password","admin");
•
•
Connection con=d.connect(
"com.mysql.jdbc.Driver").newInstance());
"jdbc:mysql://localhost:3306/test",p);
•
•
Statement st= con.createStatement(
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
•
•
•
String query="select * from emp where deptno=10";
18. •
•
ResultSet rs= st.executeQuery(query);
//Now the cursor of resultset will at beforeFirst & the result set produced is scrollable
•
System.out.println("EmpNotName");
•
•
•
•
•
while (rs.next()) {
System.out.print(rs.getInt(1)+"t");
System.out.println(rs.getString(2));
}//while
//Now the cursor of resultset will at afterLast
•
System.out.println("Reading Data, moving the cursor in backward directionn");
•
•
•
•
•
•
•
•
•
•
•
while (rs.previous()){
System.out.print(rs.getInt(1)+"t");
System.out.println(rs.getString(2));
}//while
con.close();
}//main
}//class
/*
EmpNo
Name
2
uday kumar
Reading Data, moving the cursor in backward direction
•
2
•
*/
uday kumar
22. getString()
• String getString(String columnLabel)
• Retrieves the value of the designated column
in the current row of this ResultSet object as
a String in the Java programming language.
23. getInt()
• int getInt(String columnLabel)
• Retrieves the value of the designated column
in the current row of this ResultSet object as
an int in the Java programming language.
24. getDouble()
• Retrieves the value of the designated column
in the current row of this ResultSet object as
a double in the Java programming language.
• double getDouble(String columnLabel)
25. Note:
• We can send only those java objects over the network that are
Serializable.
• In order to make object as Serializable object, the class of the object must
implement java.io.Serializable interface.
• In order to store data of the object in a file, the object must be serializable
object.
• ResultSet object is not Serializable object, so we can not send this object
over the network directly.
• To overcome this problem, there are two solutions
• 1. Use RowSets in the place of ResultSets.
• 2. copy data of ResultSet object to RowSet