调用Java Web应用程序中的Oracle存储过程

huangapple 未分类评论48阅读模式
英文:

Calling Oracle Stored Procedure from Java Web application

问题

以下是已翻译的内容:

我正在尝试从我的 Web 应用程序调用几个 Oracle 存储过程。在任何存储过程中都没有输入或输出参数。当我在本地运行我的 Web 应用程序时,一切都运行得很完美,但是当我将应用程序部署到我们的测试服务器时,存储过程调用就停止工作了。我没有收到任何错误消息,存储过程也没有运行。

我认为这可能与 JDBC 驱动程序或连接有关,但我不知道如何解决?非常感谢任何帮助!

以下是在本地运行和调试时正常工作的代码:

public boolean deleteFromTempTables(String id)
{
    String msg = "";
    CallableStatement stmt1 = null;
    
    try
    {
        conn = DBConnection.getConnection();
       
        DbmsOutput dbmsOutput = new DbmsOutput( conn );

        dbmsOutput.enable( 1000000 );
       
        stmt1 = conn.prepareCall("{call delete_from_temp_employee()}");
        stmt1.execute();
        stmt1.close();
       
        dbmsOutput.show(id);

        dbmsOutput.close();
    }
    catch(Exception e)
    {
        e.printStackTrace();
        return true;
    }
    finally
    {
        try 
        {
            stmt1.close();
            conn.close();

        } 
        catch (SQLException e) 
        {
            e.printStackTrace();
        }
    }
    return false;
}

以下是存储过程:

create or replace procedure delete_from_temp_employee as 
temp_empe_before NUMBER;
temp_empe_after  NUMBER;

begin

    select count(*) into temp_empe_before from temp_employee;
    delete from temp_employee;
    select count(*) into temp_empe_after from temp_employee;

    DBMS_OUTPUT.PUT_LINE ('Procedure DELETE_FROM_TEMP_EMPLOYEE Employee Before:  ' || temp_empe_before);
    DBMS_OUTPUT.PUT_LINE ('Procedure DELETE_FROM_TEMP_EMPLOYEE Employee After:  ' || temp_empe_after);

end delete_from_temp_employee;

在 JSP 页面中的调用:

<%
boolean isDeleteError = false; 
isDeleteError = memberupload.deleteFromTempTables(userid);
%>
英文:

I am trying to call several oracle stored procedures from my web application. There are no in or out parameters in any of the procedures. Everything works perfectly when I run my web app locally, but when I deploy the app to our test server the stored procedure calls stop working. I get no error messages and the procedure does not run.

I would think it has something to do with the JDBC driver or connection, but I'm at a loss? Any help would be greatly appreciated!

Here is the code that works perfectly when running locally and debugging:

public boolean deleteFromTempTables(String id)
{
    String msg = &quot;&quot;;
    CallableStatement stmt1 = null;
    
    try
    {
       conn = DBConnection.getConnection();
       
       DbmsOutput dbmsOutput = new DbmsOutput( conn );

       dbmsOutput.enable( 1000000 );
       
       stmt1 = conn.prepareCall(&quot;{call delete_from_temp_employee()}&quot;);
       stmt1.execute();
       stmt1.close();
       
       dbmsOutput.show(id);

       dbmsOutput.close();
    }
    catch(Exception e)
    {
        e.printStackTrace();
        return true;
    }
    finally
    {
        try 
        {
            stmt1.close();
            conn.close();

        } 
        catch (SQLException e) 
        {
            e.printStackTrace();
        }
    }
    return false;
}

Here is the stored procedure:

create or replace procedure delete_from_temp_employee as 
temp_empe_before NUMBER;
temp_empe_after  NUMBER;

begin

    select count(*) into temp_empe_before from temp_employee;
    delete from temp_employee;
    select count(*) into temp_empe_after from temp_employee;

    DBMS_OUTPUT.PUT_LINE (&#39;Procedure DELETE_FROM_TEMP_EMPLOYEE Employee Before:  &#39; || temp_empe_before);
    DBMS_OUTPUT.PUT_LINE (&#39;Procedure DELETE_FROM_TEMP_EMPLOYEE Employee After:  &#39; || temp_empe_after);

end delete_from_temp_employee;

Call in JSP page:

&lt;%

boolean isDeleteError = false; 
isDeleteError = memberupload.deleteFromTempTables(userid);

%&gt;

答案1

得分: 0

你需要执行:

    set serveroutput ON;

这个命令会使输出正常工作。

另外,在删除语句之后添加 commit; 命令以提交更改,如果你忘记了的话。

顺便提一下,你可以通过以下方式获取影响的行数,比如对于删除语句,可以使用 sql%rowcount:

    set serveroutput ON;
    create or replace procedure delete_from_temp_employee2 as 
     rows NUMBER; 
    begin
 
      delete from temp_employee;
      rows := SQL%rowcount; 
    

      DBMS_OUTPUT.PUT_LINE ('Deleted: ' || rows || ' row(s)'); 

    end delete_from_temp_employee2;
英文:

you need to execute:

set serveroutput ON;

this command makes output works.

also after delete statement add commit; command to commit changes if you forget it.

by the way, you can get the affected rows for DML statements like delete statement by using sql%rowcownt as following:

set serveroutput ON;
create or replace procedure delete_from_temp_employee2 as 
 rows NUMBER; 
begin

  delete from temp_employee;
  rows := SQL%rowcount; 


  DBMS_OUTPUT.PUT_LINE (&#39;Deleted: &#39; || rows || &#39; row(s)&#39;); 

end delete_from_temp_employee2;

huangapple
  • 本文由 发表于 2020年7月26日 04:04:27
  • 转载请务必保留本文链接:https://java.coder-hub.com/63093037.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定