interface result set
TRANSCRIPT
Interface ResultSet
• public interface ResultSetextends Wrapper• Throws SQLException• {• }
• 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
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
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
Scrollable ResultSet object
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”);
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.CONCUR_READ_ONLY);
ResultSet type( fields)
• static int TYPE_FORWARD_ONLY
• The constant indicating the type for a ResultSet object whose cursor may move only forward.
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.
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.
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.
• CONCUR_READ_ONLY• static final int CONCUR_READ_ONLY
The constant indicating the concurrency mode for a ResultSet object that may NOT be updated.
• CONCUR_UPDATABLE• static final int CONCUR_UPDATABLE
The constant indicating the concurrency mode for a ResultSet object that may be updated.
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( • "com.mysql.jdbc.Driver").newInstance());
• Properties p=new Properties ();• p.put("user","root");• p.put("password","admin");
• Connection con=d.connect(• "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";
•
• ResultSet rs= st.executeQuery(query);• //Now the cursor of resultset will at beforeFirst & the result set produced is scrollable
• System.out.println("EmpNo\tName");
• 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 direction\n");
• 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
• */
getMetaData()
ResultSetMetaData getMetaData() throws SQLException{}• Retrieves the number, types and properties of
this ResultSet object's columns.• How to call• ResultSetMetaData rsmd=rs.getMetaData();
rs.getString ( ) -
• rs.getString ( ) –• The Result set object call a get String
( ),returns you a record set into a formatted string element.
getDouble()
double getDouble(int columnIndex) {}• Retrieves the value of the designated column
in the current row of this ResultSet object as a double in the Java programming language.
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.
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.
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)
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