线程“main”中出现异常java.sql.SQLException:ORA-01008:未绑定所有变量

nnsrf1az  于 2022-12-17  发布在  Java
关注(0)|答案(1)|浏览(133)

我试图从员工数据库(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)
hrysbysz

hrysbysz1#

请参考下面的代码。使用finally块关闭连接,以确保即使在发生异常的情况下也始终执行连接。

import java.sql.*;

public class Demo {
        static final String DB_URL = "jbdc:oracle:thin:@localhost:1521:orcl";
        static final String USER = "system";
        static final String PASS ="tiger";
        Integer id = 101;
        public static void main(String[] args) {
            // Open a connection
            Connection con = null ;
            PreparedStatement st = null;
            try {
                Class.forName("oracle.jdbc.driver.OracleDriver");
                con = DriverManager.getConnection(DB_URL, USER, PASS);
                st = con.prepareStatement("delete from employee where emp_id=? ");
                st.setInt(1, id);
                int count = st.executeUpdate();
                System.out.println(count + " row deleted");
            } catch (Exception e) {
                e.printStackTrace();
            }
            finally {
                try {
                    if (con != null)
                        con.close();
                    if (stat != null)
                        stat.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

相关问题