问题
The movie which is inputted is successfully saved to moviesTbl table but then when I try to display the autonumber field 'MovieID' I retrieve errors.
public void updateMovies(String update, String title, java.sql.Date releaseDate, String genre) throws SQLException {
Connection connection = dc.DatabaseConnection();
PreparedStatement statement = connection.prepareStatement(update); //the insert statement is prepared
statement.setString(1, title); //each attribute is set individually
statement.setDate(2, releaseDate);
statement.setString(3, genre);
statement.executeUpdate();
statement.close();
ResultSet ID = dc.query("Select * from moviesTbl where title = " + title + " and genre = " + genre + " and releaseDate = " + releaseDate); //a ResultSet is prepared for the ID of the movie
while(ID.next()) { //the ResultSet is prepared for display
int movieID = ID.getInt("MovieID");
JOptionPane.showMessageDialog(null, "The movie's ID is " + movieID + ". Please record it on the DVD for future reference.");
}
}
Errors:
Exception in thread "main" net.ucanaccess.jdbc.UcanaccessSQLException: user lacks privilege or object not found: HARRY
at net.ucanaccess.jdbc.UcanaccessConnection.prepareStatement(UcanaccessConnection.java:462)
at movierentalstore.DatabaseConnection.query(DatabaseConnection.java:63)
at movierentalstore.QueryingTheDatabase.updateMovies(QueryingTheDatabase.java:181)
at movierentalstore.MovieRentalStore1.<init>(MovieRentalStore1.java:77)
at movierentalstore.MovieRentalStore1.main(MovieRentalStore1.java:25)
Caused by: java.sql.SQLSyntaxErrorException: user lacks privilege or object not found: HARRY
at org.hsqldb.jdbc.Util.sqlException(Unknown Source)
at org.hsqldb.jdbc.Util.sqlException(Unknown Source)
at org.hsqldb.jdbc.JDBCPreparedStatement.<init>(Unknown Source)
at org.hsqldb.jdbc.JDBCConnection.prepareStatement(Unknown Source)
at net.ucanaccess.jdbc.UcanaccessConnection.prepareStatement(UcanaccessConnection.java:460)
... 4 more
Caused by: org.hsqldb.HsqlException: user lacks privilege or object not found: HARRY
at org.hsqldb.error.Error.error(Unknown Source)
at org.hsqldb.error.Error.error(Unknown Source)
at org.hsqldb.ExpressionColumn.checkColumnsResolved(Unknown Source)
at org.hsqldb.QueryExpression.resolve(Unknown Source)
at org.hsqldb.ParserDQL.compileCursorSpecification(Unknown Source)
at org.hsqldb.ParserCommand.compilePart(Unknown Source)
at org.hsqldb.ParserCommand.compileStatement(Unknown Source)
at org.hsqldb.Session.compileStatement(Unknown Source)
at org.hsqldb.StatementManager.compile(Unknown Source)
at org.hsqldb.Session.execute(Unknown Source)
... 7 more
Java Result: 1
回答1:
JDBC uses the database engine's capability to return the generated key. This prevents the need to do it database vendor specific, and most important: you get the correct ID, even if two peoply do the same database insert at the same time.
try (PreparedStatement stm = conn.prepareStatement(
"INSERT INTO moviesTbl (title, ...) VALUES(?, ? ...)")) {
stm.setString(1, title);
stm.setString(2, ...);
...
int updateCount = stm.executeUpdate();
if (updateCount != 0) {
try (ResultSet genKeys = stm.getGeneratedKeys()) {
if (genKeys.next()) { // At most 1 record inserted
// Normally only one key generated per record.
int id = genKeys.getInt(0);
...
}
} // Close result set.
}
} // Closes stm
It is a bit verbose, but theoretically you could have some statement with several records, and per record more than 1 generated key.
The error could stem from the SELECT where the string constants are not between apostrophes ('). Should have been a PreparedStatement too.
来源:https://stackoverflow.com/questions/25247569/error-displaying-the-autonumber-field-from-a-resultset