interface result set

25
Interface ResultSet

Upload: myrajendra

Post on 15-May-2015

212 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Interface result set

Interface ResultSet

Page 2: Interface result set

• public interface ResultSetextends Wrapper• Throws SQLException• {• }

Page 3: Interface result set

• 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

Page 4: Interface result set
Page 5: Interface result set

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

Page 6: Interface result set
Page 7: Interface result set

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

Page 8: Interface result set

Scrollable ResultSet object

Page 9: Interface result set

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

Page 10: Interface result set

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

Page 11: Interface result set

ResultSet type( fields)

• static int TYPE_FORWARD_ONLY

• The constant indicating the type for a ResultSet object whose cursor may move only forward.

Page 12: Interface result set

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.

Page 13: Interface result set

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.

Page 14: Interface result set

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.

Page 15: Interface result set

• CONCUR_READ_ONLY• static final int CONCUR_READ_ONLY

The constant indicating the concurrency mode for a ResultSet object that may NOT be updated.

Page 16: Interface result set

• CONCUR_UPDATABLE• static final int CONCUR_UPDATABLE

The constant indicating the concurrency mode for a ResultSet object that may be updated.

Page 17: Interface result set

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

Page 18: Interface result set

• 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

• */

Page 19: Interface result set

getMetaData()

ResultSetMetaData getMetaData() throws SQLException{}• Retrieves the number, types and properties of

this ResultSet object's columns.• How to call• ResultSetMetaData rsmd=rs.getMetaData();

Page 20: Interface result set

rs.getString ( ) -

• rs.getString ( ) –• The Result set object call a get String

( ),returns you a record set into a formatted string element.

Page 21: Interface result set

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.

Page 22: Interface result set

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.

Page 23: Interface result set

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.

Page 24: Interface result set

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)

Page 25: Interface result set

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