public interface Cursor
Known subtypesCursorExt
Iterates over the results returned from a database query.
Positions are counted from zero and a new cursor sits before the first row, so the usual loop is simply:
Cursor cur = db.executeQuery("SELECT id, body FROM notes ORDER BY id");
try {
while (cur.next()) {
Row row = cur.getRow();
System.out.println(row.getInteger(0) + ": " + row.getString(1));
}
} finally {
cur.close();
}
Every navigation method works on every platform. Only the cost varies: #next() is uniformly
cheap, while #last(), #prev() and #position(int) may have to rewind and re-step the
underlying statement, which costs time proportional to the distance from the start. For a large
result set, prefer iterating forward with #next().
Because a backward seek re-runs the statement, a cursor is a repeatable read only inside a
transaction. See the com.codename1.db package documentation for the full contract.
Methods
Method details
first
public abstract boolean first()
throws IOExceptionReturns
Throws
IOException- if the cursor is closed
last
public abstract boolean last()
throws IOExceptionMoves the cursor onto the last row.
Costs a full pass over the result set the first time it is called.
Returns
Throws
IOException- if the cursor is closed
next
public abstract boolean next()
throws IOExceptionAdvances the cursor one row.
A new cursor sits before the first row, so the first call lands on it.
Returns
Throws
IOException- if the cursor is closed
prev
public abstract boolean prev()
throws IOExceptionReturns
Throws
IOException- if the cursor is closed
getColumnIndex
public abstract int getColumnIndex(String columnName)
throws IOExceptionReturns the zero-based index of a column, or -1 if there is no such column.
The comparison is case-insensitive and matches the result set label, so a column selected
as SELECT a AS b is found under b. Available as soon as the query returns, before the
first #next().
Parameters
columnNameString- the name of the column
Returns
Throws
IOException- if the cursor is closed
getColumnName
public abstract String getColumnName(int columnIndex)
throws IOExceptionReturns the label of the column at a zero-based index.
Available as soon as the query returns, before the first #next().
Parameters
columnIndexint- the zero-based index of the column
Returns
Throws
IOException- if the cursor is closed
getColumnCount
public abstract int getColumnCount()
throws IOExceptionReturns
Throws
getPosition
public abstract int getPosition()
throws IOExceptionReturns the zero-based position of the cursor.
Reports -1 before any successful move, and the row count once the result set is exhausted.
Returns
Throws
IOException- if the cursor is closed
position
public abstract boolean position(int row)
throws IOExceptionMoves the cursor to an absolute zero-based row.
Passing -1 rewinds to before the first row and returns false.
Parameters
rowint- the zero-based row to move to
Returns
Throws
IOException- if the cursor is closed
close
public abstract void close()
throws IOExceptionCloses the cursor and releases its resources.
Calling this more than once is harmless.
Throws
IOException- if the underlying statement cannot be released
getRow
public abstract Row getRow()
throws IOExceptionReturns the current row.
Valid only while the cursor is on a row.
Returns
Throws
IOException- if the cursor is closed, or is before the first row or past the last one