Interface result set

639 views

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
639
On SlideShare
0
From Embeds
0
Number of Embeds
23
Actions
Shares
0
Downloads
10
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Interface result set

  1. 1. Interface ResultSet
  2. 2. • • • • public interface ResultSetextends Wrapper Throws SQLException { }
  3. 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. 4. 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
  5. 5. 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
  6. 6. Scrollable ResultSet object
  7. 7. 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”);
  8. 8. 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);
  9. 9. ResultSet type( fields) • static int TYPE_FORWARD_ONLY • The constant indicating the type for a ResultSet object whose cursor may move only forward.
  10. 10. 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.
  11. 11. 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.
  12. 12. 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. •
  13. 13. • CONCUR_READ_ONLY • static final int CONCUR_READ_ONLY The constant indicating the concurrency mode for a ResultSet object that may NOT be updated.
  14. 14. • CONCUR_UPDATABLE • static final int CONCUR_UPDATABLE The constant indicating the concurrency mode for a ResultSet object that may be updated. •
  15. 15. 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";
  16. 16. • • 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
  17. 17. getMetaData() ResultSetMetaData getMetaData() throws SQLException { } • Retrieves the number, types and properties of this ResultSet object's columns. • How to call • ResultSetMetaData rsmd=rs.getMetaData();
  18. 18. rs.getString ( ) • rs.getString ( ) – • The Result set object call a get String ( ),returns you a record set into a formatted string element.
  19. 19. 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.
  20. 20. 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.
  21. 21. 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.
  22. 22. 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)
  23. 23. 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

×