Reputation: 11
I have this code:
public static ArrayList<TicketData> readDB(Connection pConnection)
{
TicketData lSingleTicketData;
ArrayList<TicketData> lTicketData = new ArrayList<TicketData>();
Statement lStatement;
ResultSet lResultSet;
try
{
lStatement = pConnection.createStatement();
lResultSet = lStatement.executeQuery("SELECT * FROM test.ticket");
lResultSet.first();
while(!lResultSet.isAfterLast())
{
lSingleTicketData = new TicketData(lResultSet.getString(1) ,lResultSet.getDate(2).toLocalDate(),lResultSet.getInt(3));
lTicketData.add(lSingleTicketData);
lResultSet.next();
}
}
catch (Exception ex)
{
System.out.println("Error at the execution + " + ex.getMessage());
}
return lTicketData;
}
But when execute this code I am getting this :
Operation not allowed for a result set of type ResultSet.TYPE_FORWARD_ONLY
What is causing this and how can I fix it? Thanks in advance.
Upvotes: 0
Views: 3955
Reputation: 11421
Read the Javadoc for ResultSet.first()
:
/**
* Moves the cursor to the first row in
* this <code>ResultSet</code> object.
*
* @return <code>true</code> if the cursor is on a valid row;
* <code>false</code> if there are no rows in the result set
* @exception SQLException if a database access error
* occurs; this method is called on a closed result set
* or the result set type is <code>TYPE_FORWARD_ONLY</code>
* @exception SQLFeatureNotSupportedException if the JDBC driver does not support
* this method
* @since 1.2
*/
boolean first() throws SQLException;
The usual way to iterate over a result set is:
while (rs.next()) {
...
}
You are making it much more complicated than you need to.
Upvotes: 1