6.ResultSet and ResultSet Types
6.ResultSet and ResultSet Types
1
nd
DURGASOFT, # 202,2 Floor,HUDAMaitrivanam,Ameerpet, Hyderabad - 500038, 040 – 64 51 27 86,
80 96 96 96 96, 9246212143 | www.durgasoft.com
JAVA Means DURGASOFT
2)Updatable ResultSet
4)Scrollable ResultSets
ResultSet Types:
-->As per the ResultSet concurrency there are two types of ResultSets.
2)Updatable ResultSet:
It is a ResultSet object,it will allow the users to perform updations on its content.
To represent this resultset object ResultSet interface has provided the following
constant.
2
nd
DURGASOFT, # 202,2 Floor,HUDAMaitrivanam,Ameerpet, Hyderabad - 500038, 040 – 64 51 27 86,
80 96 96 96 96, 9246212143 | www.durgasoft.com
JAVA Means DURGASOFT
-->As per the ResultSet cursor movement there are two types of ResultSets
-->To represent this ResultSet,ResultSet interface has provided the following constant
3
nd
DURGASOFT, # 202,2 Floor,HUDAMaitrivanam,Ameerpet, Hyderabad - 500038, 040 – 64 51 27 86,
80 96 96 96 96, 9246212143 | www.durgasoft.com
JAVA Means DURGASOFT
2)Scrollable ResultSet:
These are the Resultset objects,which will allow the users to iterate the data in both
Q)What are the differences betwwn Scroll Sensitive ResultSet and Scroll Insensitive ResultSet?
Scroll sensitive ResultSet is a Scrollable resultset object,which will allow the later Database
updations.
-->To refer this ResultSet,ResultSet interface has provided the following constant
-->Scroll Insensitive ResultSet is scrollable Resultset object,which will not allow the later database
updations after creation.
-->The default ResultSet type in Jdbc applications is Forward only and Read only.
-->In Jdbc applications if we want to specify a particular type to the ResultSet object then we
have to provide the above specified ResultSet constants at the time of creating Statement object,for
this we have to use the following method
Ex: Statement
st=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
-->In jdbc applications by using scrollable ResultSet we are able to retrieve the data in both
4
nd
DURGASOFT, # 202,2 Floor,HUDAMaitrivanam,Ameerpet, Hyderabad - 500038, 040 – 64 51 27 86,
80 96 96 96 96, 9246212143 | www.durgasoft.com
JAVA Means DURGASOFT
-->To retrieve the data in forward direction for each and every record we have to check whether
the next record is available or not,if it is available we have to move resultset cursor to the next
record position.when we refer a particular record then we have to retrieve the data from the
respective columns.To achieve this we have to use the following piece of code:
while(rs.next())
{
System.out.println(rs.getInt(1));
System.out.println(rs.getString(2));
-->If we want to retrieve the data in Backward direction then for each and every record we have to
check whether the previous record is available or not from the resultset cursor position,if is
available then we have to move resultset cursor to the previous record.To achieve this we have
-->After moving the resultset cursor to particular record then we have to retrieve the data
from the corresponding columns for this we have to use the following methods
Ex: while(rs.previous())
{
System.out.println(rs.getInt(1));
System.out.println(rs.getString(2));
System.out.println(rs.getFloat(3));
5
nd
DURGASOFT, # 202,2 Floor,HUDAMaitrivanam,Ameerpet, Hyderabad - 500038, 040 – 64 51 27 86,
80 96 96 96 96, 9246212143 | www.durgasoft.com
JAVA Means DURGASOFT
JdbcApp19:
package com.durgasoft;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
System.out.println(rs.getInt(1)+"\t"+rs.getString(2)+"\t"
+rs.getFloat(3)+"\t"+rs.getString(4));
}
con.close();
}
}
JdbcApp20:
package com.durgasoft;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
6
nd
DURGASOFT, # 202,2 Floor,HUDAMaitrivanam,Ameerpet, Hyderabad - 500038, 040 – 64 51 27 86,
80 96 96 96 96, 9246212143 | www.durgasoft.com
JAVA Means DURGASOFT
import java.sql.Statement;
JdbcApp21:
package com.durgasoft;
import java.awt.Button;
import java.awt.Color;
import java.awt.Font;
7
nd
DURGASOFT, # 202,2 Floor,HUDAMaitrivanam,Ameerpet, Hyderabad - 500038, 040 – 64 51 27 86,
80 96 96 96 96, 9246212143 | www.durgasoft.com
JAVA Means DURGASOFT
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
b1=new Button("First");
b2=new Button("Next");
b3=new Button("Previous");
b4=new Button("Last");
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
8
nd
DURGASOFT, # 202,2 Floor,HUDAMaitrivanam,Ameerpet, Hyderabad - 500038, 040 – 64 51 27 86,
80 96 96 96 96, 9246212143 | www.durgasoft.com
JAVA Means DURGASOFT
this.add(b3);
this.add(b4);
es=new EmployeeService();
}
public void actionPerformed(ActionEvent ae){
label=ae.getActionCommand();
eto=es.getEmployee(label);
repaint();
}
public void paint(Graphics g){
Font f=new Font("arial",Font.BOLD,30);
g.setFont(f);
String msg=es.getMsg();
if(msg.equals("")){
g.drawString("Employee Number :"+eto.getEno(), 50,100);
g.drawString("Employee Name :"+eto.getEname(), 50,150);
g.drawString("Employee Salary :"+eto.getEsal(), 50,200);
g.drawString("Employee Address :"+eto.getEaddr(), 50,250);
}else{
g.drawString(msg, 50,300);
}
}
}
package com.durgasoft;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public EmployeeService() {
try {
Class.forName("oracle.jdbc.OracleDriver");
con=DriverManager.getConnection
9
nd
DURGASOFT, # 202,2 Floor,HUDAMaitrivanam,Ameerpet, Hyderabad - 500038, 040 – 64 51 27 86,
80 96 96 96 96, 9246212143 | www.durgasoft.com
JAVA Means DURGASOFT
("jdbc:oracle:thin:@localhost:1521:xe", "system","durga");
st=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
rs=st.executeQuery("select * from emp1");
} catch (Exception e) {
e.printStackTrace();
}
}
public EmployeeTo getEmployee(String label){
try {
if(label.equals("First")){
rs.first();
msg="";
}
if(label.equals("Next")){
b=rs.next();
if(b==false){
msg="No More Records In Forward Direction";
}else{
msg="";
}
}
if(label.equals("Previous")){
b=rs.previous();
if(b==false){
msg="No More Records In Backward Direction";
}else{
msg="";
}
}
if(label.equals("Last")){
rs.last();
msg="";
}
eto=new EmployeeTo();
eto.setEno(rs.getInt(1));
eto.setEname(rs.getString(2));
eto.setEsal(rs.getFloat(3));
eto.setEaddr(rs.getString(4));
} catch (Exception e) {
e.printStackTrace();
}
return eto;
}
public String getMsg(){
return msg;
10
nd
DURGASOFT, # 202,2 Floor,HUDAMaitrivanam,Ameerpet, Hyderabad - 500038, 040 – 64 51 27 86,
80 96 96 96 96, 9246212143 | www.durgasoft.com
JAVA Means DURGASOFT
}
}
package com.durgasoft;
11
nd
DURGASOFT, # 202,2 Floor,HUDAMaitrivanam,Ameerpet, Hyderabad - 500038, 040 – 64 51 27 86,
80 96 96 96 96, 9246212143 | www.durgasoft.com
JAVA Means DURGASOFT
package com.durgasoft;
12
nd
DURGASOFT, # 202,2 Floor,HUDAMaitrivanam,Ameerpet, Hyderabad - 500038, 040 – 64 51 27 86,
80 96 96 96 96, 9246212143 | www.durgasoft.com
JAVA Means DURGASOFT
System.out.println("---------------------------------------");
while(rs.next()){
rs.refreshRow();
System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getFloat(3)+"
"+rs.getString(4));
}
con.close();
}
-->To move ResultSet cursor to before first record we have to use the following method from
ResultSet
public void beforeFirst()
-->To move ResultSet cursor to After the last record we have to use the following method from
ResultSet
public void afterLast()
-->To move ResultSet cursor to a particular record we have to use the following method from
ResultSet
public void absolute(int rec_position)
-->In case of scroll sensitive ResultSet objects to reflect later database updations into the
ResultSet object we have to refresh each and every record for this we have to use the following
method from ResultSet
public void refreshRow()
-->If we use Type4 Driver provided by oracle in the above application then JVM will raise an
exception like java.sql.SQLException:unsupportedfeature:refreshrow
-->In jdbc applications scroll sensitive ResultSet object should be supported by Type1 Driver
provided by Sun MicroSystems,which could not be supported by Type4 Driver provided by oracle.
-->In Jdbc applications scroll Insensitive ResultSet object could not be supported by both
Type1 Driver provided by Sun MicroSystems and Type4 Driver provided by oracle.
13
nd
DURGASOFT, # 202,2 Floor,HUDAMaitrivanam,Ameerpet, Hyderabad - 500038, 040 – 64 51 27 86,
80 96 96 96 96, 9246212143 | www.durgasoft.com
JAVA Means DURGASOFT
-->In jdbc applications the main purpose of UpdatableResultSet object is to perform updations
on its content in order to perform the manipulations with the data available at Database
-->In jdbc applications Updatable ResultSet objects can be used to insert records on database table
to achieve the above requirement we have to use the following steps:
Statement
st=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
Step2: After creating ResultSet Object we have to move ResultSet cursor to end of the ResultSet
object
where we have to take a buffer to insert new Record data temporarily to achieve this we have to
use the following method from ResultSet
Ex:rs.moveToInsertRow();
Step3:Insert record data on resultset object temporarily to do this we have to use the following
method
Ex:rs.updateInt(1,555);
rs.updateString(2,'xys');
rs.updateFloat(3,9000);
ex:rs.insertRow();
NOTE:The main advantage of this updatable ResultSet object is to perform updations on database
table without using SQL Queries.
14
nd
DURGASOFT, # 202,2 Floor,HUDAMaitrivanam,Ameerpet, Hyderabad - 500038, 040 – 64 51 27 86,
80 96 96 96 96, 9246212143 | www.durgasoft.com
JAVA Means DURGASOFT
JdbcApp23:
The following example demonstrates how to insert no.of records into database table through a
Jdbc application
package com.durgasoft;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
while(true){
System.out.print("Employee Number :");
int eno=Integer.parseInt(br.readLine());
System.out.print("Employee Name :");
String ename=br.readLine();
System.out.print("Employee Salary :");
float esal=Float.parseFloat(br.readLine());
System.out.print("Employee Address :");
String eaddr=br.readLine();
rs.updateInt(1, eno);
rs.updateString(2, ename);
rs.updateFloat(3, esal);
rs.updateString(4, eaddr);
rs.insertRow();
15
nd
DURGASOFT, # 202,2 Floor,HUDAMaitrivanam,Ameerpet, Hyderabad - 500038, 040 – 64 51 27 86,
80 96 96 96 96, 9246212143 | www.durgasoft.com
JAVA Means DURGASOFT
}
}
con.close();
}
NOTE:In jdbc applications updatable ResultSets could not be supported by Type 4 Driver provided
by oracle
-->In jdbc applications by using updatable ResultSet object it is possible to update database
Statement
st=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
ex: rs.updateFloat(3,7000.0f);
Ex:rs.updateRow();
JdbcApp24:
package com.durgasoft;
import java.sql.Connection;
import java.sql.DriverManager;
16
nd
DURGASOFT, # 202,2 Floor,HUDAMaitrivanam,Ameerpet, Hyderabad - 500038, 040 – 64 51 27 86,
80 96 96 96 96, 9246212143 | www.durgasoft.com
JAVA Means DURGASOFT
import java.sql.ResultSet;
import java.sql.Statement;
-->In Jdbc applications by using updatable ResultSet object it is possible to delete records
on database table to achieve this we have to use the following method
Ex:rs.deleteRow();
JdbcApp25:
package com.durgasoft;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
17
nd
DURGASOFT, # 202,2 Floor,HUDAMaitrivanam,Ameerpet, Hyderabad - 500038, 040 – 64 51 27 86,
80 96 96 96 96, 9246212143 | www.durgasoft.com
JAVA Means DURGASOFT
);
Statement st=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
ResultSet rs=st.executeQuery("select * from emp1");
rs.last();
rs.deleteRow();
st.close();
con.close();
}
-->To move ResultSet cursor to a particular Record position we have to use the following method
from resultset
-->To move ResultSet cursor over some no.of records we have to use the following method from
Resultset
-->If we have bulk of records in database table,where if we are trying to retrieve all the
will be reduced.
-->In the above context to improve the performance of Jdbc application we have to fetch the limited
-->To specify the no.of records which we want to fetch at an attempt then we have to use
-->To get the specified fetch size value from resultset we have to use the following method
18
nd
DURGASOFT, # 202,2 Floor,HUDAMaitrivanam,Ameerpet, Hyderabad - 500038, 040 – 64 51 27 86,
80 96 96 96 96, 9246212143 | www.durgasoft.com
JAVA Means DURGASOFT
In the above context,to improve the performance of Jdbc applications,we have to use an alternative
where we have to perform Query Processing one time inorder to perform or execute the same SQL
Query in the next sequence.
If we want to use PreparedStatement in Jdbc applications we have to use the following steps.
19
nd
DURGASOFT, # 202,2 Floor,HUDAMaitrivanam,Ameerpet, Hyderabad - 500038, 040 – 64 51 27 86,
80 96 96 96 96, 9246212143 | www.durgasoft.com
JAVA Means DURGASOFT
20
nd
DURGASOFT, # 202,2 Floor,HUDAMaitrivanam,Ameerpet, Hyderabad - 500038, 040 – 64 51 27 86,
80 96 96 96 96, 9246212143 | www.durgasoft.com