从Java调用PLSQL过程 [英] calling PLSQL procedure from Java

查看:126
本文介绍了从Java调用PLSQL过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我的Java程序。我正在调用PLSQL过程来更新Employee名称。我关闭了PLSQL代码中的提交,以便我可以从Java代码执行提交和回滚。但即使在我关闭自动提交并进行显式回滚之后,仍然会在表中更新详细信息。
怎么样?我不知道,请帮忙。

Below is my Java program. I am calling a PLSQL procedure to update the Employee name. I turned off the commit in the PLSQL code so that I can do the commit and rollback from Java code. But even after I turned off the auto commit and doing explicit rollback, still the details are updated in the table. How? I have no idea, please help.

这是我的Java代码。在PLSQL中,它只读取值并执行更新语句。没有提交。

Here's my Java code. In PLSQL, it just read the value and does an update statement . No commits.

public class TestCommit {
    public static void main(String[] args) throws SQLException, IOException {
        CallableStatement callableStatement = null;
        Connection conn = null;
        try {
            DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
            conn = DriverManager
                    .getConnection("jdbc:oracle:thin:testuser/testpwd@//testdb:1521/testbx");
            conn.setAutoCommit(false);
            String sql = "{call testpkg.saveemployee(?,?)}";
            callableStatement = conn.prepareCall(sql);
            callableStatement.setString("name", "spider");
            callableStatement.setString("id", "A101");
            callableStatement.executeQuery();
            conn.rollback();
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            // Close the statement
            callableStatement.close();
            // Close the connection
            conn.close();
        }
    }

}

编辑: PLSQL

CREATE OR REPLACE PROCEDURE saveemployee(
       name IN employee.ename%TYPE,
       id IN employee.eid%TYPE)
IS
BEGIN

  UPDATE employee SET ename = name WHERE eid = id;

END;


推荐答案

我的不好,我打电话给错误的程序,那里在两个不同的包中有两个版本的相同过程,
一个有提交,另一个没有提交。我打电话给那个有提交的人。
现在从两个程序中删除了提交,我的代码现在似乎正常工作。

My bad, I was calling a wrong procedure, there were two versions of the same procedure in two different packages, one has the commit , the other one doesn't have the commit. I was calling the one that had the commit. Now that the commit is removed from both procedures, my code seems to work now.

这篇关于从Java调用PLSQL过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆