Search code examples
javaoracle-databasejdbcprepared-statement

Exception in thread "main" java.sql.SQLException: ORA-01008: not all variables bound


I am trying to delete record from employee database (Oracle) and I used PreparedStatement interface and its method. The code throws an exception:

Exception in thread "main" java.sql.SQLException: ORA-01008: not all variables bound

Here is my code

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");

Console

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)

Solution

  • Answer :- use statement.executeUpdate(), not statement.executeUpdate(sql)