Interface Cursor
The Cursor interface is used to iterate over the results returned from a database query. IMPORTANT: Notice that some methods might not be supported on all platforms!
There is more thorough coverage of the Database API here.
The sample code below presents a Database Explorer tool that allows executing arbitrary SQL and viewing the tabular results:
Toolbar.setGlobalToolbar(true);
Style s = UIManager.getInstance().getComponentStyle("TitleCommand");
FontImage icon = FontImage.createMaterial(FontImage.MATERIAL_QUERY_BUILDER, s);
Form hi = new Form("SQL Explorer", new BorderLayout());
hi.getToolbar().addCommandToRightBar("", icon, (e) -> {
TextArea query = new TextArea(3, 80);
Command ok = new Command("Execute");
Command cancel = new Command("Cancel");
if(Dialog.show("Query", query, ok, cancel) == ok) {
Database db = null;
Cursor cur = null;
try {
db = Display.getInstance().openOrCreate("MyDB.db");
if(query.getText().startsWith("select")) {
cur = db.executeQuery(query.getText());
int columns = cur.getColumnCount();
hi.removeAll();
if(columns > 0) {
boolean next = cur.next();
if(next) {
ArrayList data = new ArrayList<>();
String[] columnNames = new String[columns];
for(int iter = 0 ; iter
@author Chen
-
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Close the cursor and release its resourcesbooleanfirst()Move the cursor to the first row.intReturns the column countintgetColumnIndex(String columnName) Returns the zero-based index for a given column name.getColumnName(int columnIndex) Returns the column name at a given zero-based column index.intReturns the current Cursor position.getRow()Get the Row data Object.booleanlast()Move the cursor to the last row.booleannext()Moves the cursor to the next row.booleanposition(int row) Move the cursor to an absolute row positionbooleanprev()Moves the cursor to the previous row.
-
Method Details
-
first
Move the cursor to the first row.
If cursor provides forward-only navigation and is positioned after the first row then calling first() method would throw a IOException.
Returns
true if succeeded
Throws
IOException
- Throws:
IOException
-
last
Move the cursor to the last row.
Returns
true if succeeded
Throws
IOException
- Throws:
IOException
-
next
Moves the cursor to the next row. Calling next() method the first time will position cursor on the first.
Returns
true if succeeded
Throws
IOException
- Throws:
IOException
-
prev
Moves the cursor to the previous row. If cursor is forward type then calling prev() would throw a IOException.
Returns
true if succeeded
Throws
IOException
- Throws:
IOException
-
getColumnIndex
Returns the zero-based index for a given column name. Note that columns meta information is available only after navigation to the first row
Parameters
columnName: the name of the column.
Returns
the index of the column
Throws
IOException
- Throws:
IOException
-
getColumnName
Returns the column name at a given zero-based column index. Note that columns meta information is available only after navigation to the first row
Parameters
columnIndex: the index of the column
Returns
the name of the column
Throws
IOException
- Throws:
IOException
-
getColumnCount
Returns the column count
Returns
the column count
Throws
IOException
- Throws:
IOException
-
getPosition
Returns the current Cursor position.
Returns
the cursor position
Throws
IOException
- Throws:
IOException
-
position
Move the cursor to an absolute row position
Parameters
row: position to move to
Returns
true if succeeded
Throws
IOException
- Throws:
IOException
-
close
Close the cursor and release its resources
Throws
IOException
- Throws:
IOException
-
getRow
Get the Row data Object.
Returns
a Row Object
Throws
IOException
- Throws:
IOException
-