-1

I am using the ucanaccess tool to interact with my access database, but I do not know how to display or store the resulting ResultSets as Strings.

Edit: I have tried =System.out.println(resultSetId.getString(1)); but that only returns errors. ResultSet resultSetId; String msAccDB = "D://Computer Science//passManager//src//Manager.accdb"; String dbURL = "jdbc:ucanaccess://" + msAccDB; connection = DriverManager.getConnection(dbURL); statement = connection.createStatement(); resultSetId = statement.executeQuery("SELECT ID FROM Passwords WHERE ID = \""+identifier+"\";"); System.out.println(resultSetId.getString(1));

Gives the error Exception in thread "main" net.ucanaccess.jdbc.Ucanaccessenter code hereSQLException: invalid cursor state: identifier cursor not positioned on row in UPDATE, DELETE, SET, or GET statement: ; ResultSet is positioned before first row

Edit: Adding the .next() fixed it, thanks!

1 Answers1

0

ResultSet is positioned before first row

When executeQuery returns the ResultSet it is not pointing at a row of data. You need to call resultSetId.next() in order to retrieve each row. That is often done in the context of a while loop when the ResultSet is expected to return more than one row, e.g.,

while (resultSetId.next()) {
    System.out.println(resultSetId.getString(1));  // print value from each row
}

but you can simply call it once if you only expect a single row, e.g.,

if (resultSetId.next()) {
    System.out.println(resultSetId.getString(1));
} else {
    System.out.println("The ResultSet contained no rows.");
}
Gord Thompson
  • 116,920
  • 32
  • 215
  • 418