oracle 直接使用java在Oracle数据库中创建函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5899726/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 23:36:14  来源:igfitidea点击:

Creating function in an Oracle database using java directly

javaoracleplsql

提问by Gallal

I understand that it is possible to create java function in an oracle database using the command CREATE JAVA - look here. I have been reading a lot about how to do this but the more I read the more there is to read, it seems.

我知道可以使用命令 CREATE JAVA 在 oracle 数据库中创建 java 函数 - 请看这里。我一直在阅读很多关于如何做到这一点的书,但我读得越多,似乎读得越多。

What I want to do is simple. Since I am already very familiar with Java, I don't want to go through learning PL/SQL especially that the project I am working on is rather small. I also don't want to mess around too much with with this feature, all that I want to do is something like the following:

我想做的很简单。因为我对Java已经很熟悉了,所以不想再去学习PL/SQL,尤其是我在做的项目比较小。我也不想过多地使用此功能,我想做的就是以下内容:

1) Declare a function while connected to the database like:

1)在连接到数据库时声明一个函数,如:

CREATE JAVA AS 
    public class Example{
        public static bool returnTrue() {
            return true;
        }
    }

2) Then call the function while connected like:

2)然后在连接时调用该函数,例如:

SELECT Example.returnTrue() FROM DUAL;

Is this possible?
How?

这可能吗?
如何?

回答by DCookie

Not directly possible, you need another step:

不是直接可能的,你需要另一个步骤:

(Note that you cannot return a boolean type in a SQL callable function. You must return a valid Oracle SQL type, in this example, a String).

(请注意,您不能在 SQL 可调用函数中返回布尔类型。您必须返回有效的 Oracle SQL 类型,在本例中为字符串)。

Create your function:

创建你的函数:

create or replace and compile java source named returntrue as
public class example
{ public static String returnTrue() { return "TRUE"; } };

You have to create a PL/SQL "wrapper" to interface between the java function and PL/SQL:

您必须创建一个 PL/SQL“包装器”来连接 java 函数和 PL/SQL:

SQL>     CREATE OR REPLACE FUNCTION returnTrue
  2      RETURN VARCHAR2
  3      AS LANGUAGE JAVA
  4      NAME 'example.returnTrue() return java.lang.String';
  5  /


Function created

SQL> select returntrue from dual;

RETURNTRUE
--------------------------------------------------------------------------------
TRUE

More info in the Oracle documentation.

Oracle 文档中的更多信息。

回答by Dan

How invested are you in Oracle? If the answer is anywhere between "a fair amount" and "my life depends on it," learn PL/SQL. It's a sort of odd language in some ways, but it does what it does quite well, and that is provide a procedural layer on top of SQL. If what your code is doing is more in depth or sophisticated than "a procedural layer on top of SQL", you shouldn't use stored procedures at all. Run your own JVM and employ JDBC when you need it.

您对 Oracle 的投资有多大?如果答案介于“相当数量”和“我的生活取决于它”之间,请学习 PL/SQL。它在某些方面是一种奇怪的语言,但它做得很好,那就是在 SQL 之上提供了一个过程层。如果您的代码所做的事情比“SQL 之上的过程层”更深入或更复杂,则根本不应该使用存储过程。运行您自己的 JVM 并在需要时使用 JDBC。