输入的电影成功保存到moviesTbl表,但是当我尝试显示自动编号字段'MovieID'时,我检索到错误。
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.");
}
}
错误:
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结果:1
答案 0 :(得分:0)
JDBC使用数据库引擎的功能返回生成的密钥。 这可以防止需要对数据库供应商进行特定的,最重要的是: 即使两个人同时执行相同的数据库插入,您也会获得正确的ID。
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
它有点冗长,但理论上你可以有一些带有几条记录的声明,并且每条记录有多条生成密钥。
错误可能源于SELECT,其中字符串常量不在撇号('
)之间。也应该是PreparedStatement。