我试图从员工数据库(Oracle)中删除记录,我使用了PreparedStatement接口及其方法。代码抛出异常:
线程“main”中出现异常java.sql.SQLException:ORA-01008:未绑定所有变量
下面是我的代码
Class.forName("oracle.jdbc.driver.OracleDriver");
dbURL = "jbdc:oracle:thin:@localhost:1521:orcl";
username = "system";
password = "tiger";
connection = DriverManager.getConnection(dbURL, username, password);
System.out.println("Connected Successfully Database ");
String sql = "delete from employee where emp_id=? ";
statement = connection.prepareStatement(sql);
statement.setInt(1, 101);
int result = statement.executeUpdate(sql);
System.out.println(result + " record deleted");
connection.close();
System.out.println("Connection Successfully Closed");
控制台
Connected Successfully Database
Exception in thread "main" java.sql.SQLException: ORA-01008: not all variables bound
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:112)
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:331)
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:288)
at oracle.jdbc.driver.T4C8Oall.receive(T4C8Oall.java:743)
at oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:216)
at oracle.jdbc.driver.T4CPreparedStatement.executeForRows(T4CPreparedStatement.java:955)
at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1169)
at oracle.jdbc.driver.OracleStatement.executeUpdateInternal(OracleStatement.java:1615)
at oracle.jdbc.driver.OracleStatement.executeUpdate(OracleStatement.java:1580)
at preparedStatementInterface.AllQueryPgm.deleteData(AllQueryPgm.java:79)
at preparedStatementInterface.AllQueryPgm.main(AllQueryPgm.java:132)
1条答案
按热度按时间hrysbysz1#
请参考下面的代码。使用finally块关闭连接,以确保即使在发生异常的情况下也始终执行连接。