简体   繁体   English

如何在 Java 中执行 Oracle PLSQL 块

[英]How to execute an Oracle PLSQL block in Java

I have an PL/SQL block like this:我有一个像这样的 PL/SQL 块:

BEGIN
  FOR i IN 1..100
  LOOP
    UPDATE rptbody 
       SET comments = 'abcs';
     WHERE (rptno> 100 and rptno < 200) and ROWNUM<2;
    COMMIT;
  END LOOP;
END;

This block needs to be executed using Oracle JDBC.该块需要使用 Oracle JDBC 执行。 I have tried the following methods:我尝试了以下方法:

  • Tried to execute this using Statement object.尝试使用 Statement 对象执行此操作。 Since this is a block, an exception was raised saying that this is not an sql statement由于这是一个块,因此引发了一个异常,说这不是一个 sql 语句

  • This can be split up into sql statements, but I have 100s of such blocks which would be cumbersome for the code and thought of leaving this to the sqlplus.这可以拆分为 sql 语句,但是我有 100 个这样的块,这对于代码来说会很麻烦,并考虑将其留给 sqlplus。

  • Tried with CallableStatement which did not work as well.尝试使用 CallableStatement,但效果不佳。

Any solutions would be helpful.任何解决方案都会有所帮助。

This has nothing to do with how you run it.这与您如何运行它无关。 The PL/SQL syntax is invalid. PL/SQL 语法无效。 You have a ;你有一个; after the update clause right before the WHERE clause:WHERE子句之前的更新子句之后:

BEGIN
  FOR i IN 1..100
  LOOP
    UPDATE rptbody 
       SET comments = 'abcs' --<<< no semicolon here!!
     WHERE (rptno> 100 and rptno < 200) and ROWNUM<2;
    COMMIT;
  END LOOP;
END;

The above code can be run like this:上面的代码可以这样运行:

String sql = "... the PL/SQL block ...";
Statement stmt = connection.createStatement();
stmt.execute(sql);

查看一些代码示例以在Github上使用 CallableStatement 和 PreparedStatement

Tried to execute this using Statement object.尝试使用 Statement 对象执行此操作。 Since this is a block, an exception was raised saying that this is not an sql statement由于这是一个块,因此引发了一个异常,说这不是一个 sql 语句

Since you were trying to execute a plsql block, you should not use Statement object. 

From https://docs.oracle.com/javase/7/docs/api/java/sql/Statement.html : The object used for executing a static SQL statement and returning the results it produces.来自https://docs.oracle.com/javase/7/docs/api/java/sql/Statement.html :用于执行静态 SQL 语句并返回它产生的结果的对象。

This is the way you need to execute a block:这是执行块所需的方式:

CallableStatement anonBlock = null // Note that CallableStatement is an interface

String anonBlockString = '' //Generally multi line
// Set in and out parameters as needed by your anonBlockString

anonBlock.registerOutParameter( <number> , <type> )

...

// executeUpdate() is inherited from PreparedStatement and can be used for executing DML statements (update, insert, delete)
anonBlock.executeUpdate(); 


To access out parameters:
anonBlock.getString(<number which you have assigned in registerOutParameter() calls);

For complete example: ( https://download.oracle.com/otn_hosted_doc/timesten/1121/quickstart/sample_code/jdbc/plsqlJDBC.java )完整示例:( https://download.oracle.com/otn_hosted_doc/timesten/1121/quickstart/sample_code/jdbc/plsqlJDBC.java

This can be split up into sql statements, but I have 100s of such blocks which would be cumbersome for the code and thought of leaving this to the sqlplus.这可以拆分为 sql 语句,但是我有100 个这样的块,这对于代码来说会很麻烦,并考虑将其留给 sqlplus。

Prefer to use stored procedures instead of anonymous blocks.更喜欢使用存储过程而不是匿名块。 Since stored procedures are stored in a compiled and optimized format , they have a performance boost compared to anonymous ones由于存储过程以经过编译和优化的格式存储,因此与匿名存储过程相比,它们的性能有所提升

Tried with CallableStatement which did not work as well:尝试使用 CallableStatement 但效果不佳:

What was the code, error/stack?代码是什么,错误/堆栈?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM