简体   繁体   English

Oracle DBMS可以从Java存储过程调用返回Java对象吗?

[英]Can Oracle DBMS return a Java object from a Java stored procedure call?

Can the Oracle database return a Java object from the return values of a Java stored procedure call? Oracle数据库是否可以从Java存储过程调用的返回值返回Java对象?

I would like to query the Oracle database with a call to a java stored procedure and receive back java objects as the results. 我想通过调用java存储过程来查询Oracle数据库,并接收java对象作为结果。 Is this possible? 这可能吗? If so, could someone present a very simple example? 如果是这样,有人会提出一个非常简单的例子吗?

Note: I don't want to store serialized objects in the database. 注意:我不想将序列化对象存储在数据库中。 I want to run the Java stored procedure, and have this procedure return a Java object. 我想运行Java存储过程,并让此过程返回一个Java对象。 So if the database is queried, each returned record will be a Java object. 因此,如果查询数据库,则每个返回的记录将是Java对象。

For instance: I want the Java stored procedure to parse a binary file that is stored in a network shared drive, build a Java object with the information extracted from the binary file, and return this Java object as the query result. 例如:我希望Java存储过程解析存储在网络共享驱动器中的二进制文件,使用从二进制文件中提取的信息构建Java对象,并将此Java对象作为查询结果返回。

I want to achieve something like this: 我希望实现这样的目标:

 #Using Java or Python programming language
 results = execute( Select java_procedure_call(parameter) From dual);
 For java_obj in results:
     print java_obj.name
     print java_obj.city

Other information: I am not using Java EE. 其他信息:我没有使用Java EE。

Thanks in advance. 提前致谢。

What you need to do is, serialize and de-serialize java objects to and from Oracle database table. 您需要做的是, Java对象与Oracle数据库表进行序列和反序列化。 You can store the serialized object bytes in Oracle table using BLOB column type. 您可以使用BLOB列类型将序列化对象字节存储在Oracle表中。 Here is a link describing how you can store and retrieve java objects from a table. 这是一个描述如何从表中存储和检索Java对象的链接。

http://asktom.oracle.com/pls/apex/f?p=100:11:0::::p11_question_id:1285601748584 http://asktom.oracle.com/pls/apex/f?p=100:11:0::::p11_question_id:1285601748584

You can store any serializable data in Databases from text files, ,images to Java Objects. 您可以将数据库中的任何可序列化数据从文本文件,图像存储到Java对象。 Basic logic here,any data is serialized to a byte stream and stored in DB. 这里的基本逻辑,任何数据都被序列化为字节流并存储在DB中。 They are deserialized when fetching from DB. 从DB获取时,它们被反序列化。 If you really want to this you can skip my answer. 如果你真的想要这个,你可以跳过我的答案。 However, mapping database fields to Java classes is a better solution and my explanation will be in this direction. 但是,将数据库字段映射到Java类是一个更好的解决方案,我的解释将是这个方向。

You shoud use a Object Relational Mapping(ORM) framework for this purpose. 为此,您应该使用对象关系映射(ORM)框架。 It can be Hibernate , JPA or Spring JDBC Template . 它可以是HibernateJPASpring JDBC Template One of them can be used according to your requirements. 其中一个可以根据您的要求使用。

If you do not want to use any framework you can basically implement it. 如果您不想使用任何框架,您基本上可以实现它。

Implement the structure for 2 fields such as username and password. 实现2个字段的结构,如用户名和密码。 I assume that both fields are String 我假设两个字段都是String

First, you should have a model class which will be mapped o query results. 首先,您应该有一个模型类,它将映射到查询结果。

class Model
{
     private String username;
     private String password;

    //setter and getters.
} 

Secondly, you should implement a Factory pattern to create Java objects from query results. 其次,您应该实现Factory模式以从查询结果创建Java对象。

class BasicModelFactory
{

   public List<Model> getModels(ResultSet rs)
   {
        List<Model> models = new ArrayList<Model>();
        while(rs.next())
        {
             Model m = new Model();
             m.setUsername(rs.getString("username");
             m.setPassword(rs.getString("password");
             models.add(m); 
        }
        return models;
   }

}

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

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