使用JDBC运行PLSQL的问题

时间:2017-03-30 11:41:22

标签: java oracle jdbc

我正在尝试运行一个PLSQL脚本,我使用以下方法作为字符串值。

  • 我首先创建程序

  • 然后我创建一个callStatement来调用它

  • 最后我添加了一些参数

我收到错误消息:     ORA-06575:包或函数PROCEDURE_NAME处于无效状态

有什么想法我可以做些什么?

这是代码:

public IResult createAndExecuteCallable(String queryText, String procedureName, Object[] parameter) {
    IResult result = new Result();
    String procedure = "create procedure "+procedureName+"("+queryText+")";
    Connection connection = this.getDatabaseConnection().getConnection();
    try {
        connection.setAutoCommit(true);
        Statement stmt = connection.createStatement();
        stmt.executeUpdate(procedure);
        CallableStatement statement = connection.prepareCall("call "+procedureName+"()");
        commonPreparedStatment(statement,parameter);
        try {
            statement.executeUpdate();
        } catch(SQLException se){
            result = new Result(se);
        } finally {statement.close();}
    } catch(Exception e){
        result = new Result(e);
    } finally {
        closeConnection(connection);
    }
    return result;
}

1 个答案:

答案 0 :(得分:0)

感谢评论和其他一些帮助,我找到了解决方案。这是代码的新中间部分,现在包括编译:

        connection.setAutoCommit(true);
        PreparedStatement createStatement = connection.prepareStatement(queryCreateText);
        PreparedStatement compileStatement = connection.prepareStatement(queryCompileText);
        CallableStatement statement = connection.prepareCall(queryCallText);
        commonPreparedStatment(statement, parameter);
        try {
            createStatement.executeUpdate();
            log.info("create ok");
        } catch (SQLException ignored) {
            log.info(ignored.getMessage());
        } finally {
            log.info(queryCompileText);
            createStatement.close();
        }
        try {
            compileStatement.executeUpdate();
            log.info("compile ok");
        } catch (SQLException ignoredToo) {
            log.info(ignoredToo.getMessage());
        } finally {
            compileStatement.close();
        }
        try {
            statement.executeUpdate();
            log.info("execute ok");
        } catch (SQLException se) {
            result = new Result(se);
        } finally {
            statement.close();
        }

另一个障碍是plsql代码的文件内容。它包含换行符,Oracle不接受它们。所以我必须在Oracle中创建过程之前删除它们:

 queryCreateText = queryCreateText.replace("\r\n", " ");