简体   繁体   English

Oracle Java存储过程返回数据

[英]Oracle Java stored procedures returning data

I'm trying to write a Java stored procedure that could return a result. 我正在尝试编写一个可能返回结果的Java存储过程 I've found this doc on Oracle website, but none of the examples provided return data http://docs.oracle.com/cd/B19306_01/java.102/b14187/cheight.htm#CHDJJDGH 我已经在Oracle网站上找到了该文档,但是没有提供的示例返回数据http://docs.oracle.com/cd/B19306_01/java.102/b14187/cheight.htm#CHDJJDGH

I have created the package as follow: 我创建了以下程序包:

CREATE OR REPLACE PACKAGE test_proc AS 
FUNCTION hello_world RETURN VARCHAR2;
PROCEDURE insert_test(CHAINE VARCHAR2, NOMBRE NUMBER);
END test_proc;

The package body as follow 包体如下

CREATE OR REPLACE PACKAGE BODY test_proc AS
FUNCTION hello_world RETURN VARCHAR2 AS LANGUAGE JAVA
NAME 'TestProc.helloWorld() return java.lang.String';
PROCEDURE insert_test(CHAINE VARCHAR2, NOMBRE NUMBER) AS LANGUAGE JAVA
NAME 'TestProc.insertTEST(java.lang.String, int)';
END test_proc;

And the Java Code 和Java代码

public class TestProc {

public static void insertTEST(String chaine, int nombre) 
{
    System.out.println("Insert into test...");
    String sql = "INSERT INTO TEST VALUES(?,?)";
    try
    {
        Connection conn = DriverManager.getConnection("jdbc:default:connection:");
        PreparedStatement pstmt = conn.prepareStatement(sql);
        pstmt.setString(1, chaine);
        pstmt.setInt(2, nombre);
        pstmt.executeUpdate();
    }
    catch (SQLException e)
    {
        System.err.println(e.getMessage());     
    }
}

public static String helloWorld() 
{
    return "Hello world!!";
 }
}

I use SQLDeveloper to call my procedures using the following instructions 我使用SQLDeveloper按照以下说明调用过程

CALL test_proc.insert_test('test',1); #work
CALL test_proc.hello_world(); #doesn't work

When executing the second instruction I have the following error ORA-06576: not a valid function or procedure name 执行第二条指令时,出现以下错误ORA-06576:无效的函数或过程名称

Do you know how to solve this? 你知道如何解决吗? Or do you know where to find a working example of java stored procedure returning data in an Oracle database? 还是您知道在哪里可以找到在Oracle数据库中返回数据的Java存储过程的工作示例?

Finally got a result using the following command: 最终使用以下命令获得了结果:

select test_proc.hello_world() from dual;

Result: 结果:

TEST_PROC.HELLO_WORLD()                                                              
-------------------------------------------------------------------------------------
Hello World!!
1 rows selected

Do you know how to return complex result from database like multiple row? 您知道如何从数据库中返回复杂结果吗?

For a PL/SQL function that returns something, you cannot call it in PL/SQL as if it were a procedure. 对于返回某些内容的PL / SQL函数,您不能像在过程中那样在PL / SQL中对其进行调用。 Call it as a function. 将该函数称为函数。 Try 尝试

var test VARCHAR2(30)
CALL test_proc.hello_world() INTO :test;
print test

TEST
------------------------------------------
Hello world!!

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

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