Create ResultSet
ResultSet is an object which is used to store the result of executed SQL statement. These ResultSet object can contain Rows if database query having number of rows. ResultSet have number of methods to access column and attribute from RDBMS, commonly get methods. Move to next row, ResultSet next() method is used, and cursor will move to next row of database table. This ResultSet object is maintained in cursor, which points to current row of table data. When ResultSet object is created default cursor position is on before first row, if ResultSet next method is called cursor set to first row of table data.
ResultSet are in java.sql.ResultSet
<%
Statement stm=null;
stm=conn.createStatement();
// conn is Connection object
ResultSet rs=null;
rs=stm.executeQuery("select * from TableName where iEmpID=10");
String sEmpName=null;
while(rs.next())
{
sEmpName=rs.getString("DataBaseAttributeName");
}
%>
It is possible scrollable ResultSet, scrollable ResultSet help us to revisit again at previous cursor point.
Result with simple Statement
<%
Statement stm=null;
stm=conn.createStatement();
// conn is Connection object
ResultSet rs=null;
rs=stm.executeQuery("select * from TableName where iEmpID=10");
String sEmpName=null;
rs.beforeFirst();
while(rs.next())
{
sEmpName=rs.getString("DataBaseAttributeName");
}
%>
ResultSet with PreparedStatement
<%
// conn is Connection object
PreparedStatement pstm=null;
pstm=conn.prepareStatement("select * from TableName where iEmpID=?");
pstm.setString(1,VariableName);
ResultSet rs=null;
rs=pstm.executeQuery();
String sEmpName=null;
while(rs.next())
{
sEmpName=rs.getString("DataBaseAttributeName");
}
%>
Other scrollable ResultSet are
Fetching data from ResultSet
Fetching data from ResultSet is easy task; it provides lot of methods to get any type of data. With iterator we can fetch all rows from table one by one next method of ResultSet.
|