简体   繁体   中英

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?

I would like to query the Oracle database with a call to a java stored procedure and receive back java objects as the results. 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. So if the database is queried, each returned record will be a Java object.

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.

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.

Thanks in advance.

What you need to do is, serialize and de-serialize java objects to and from Oracle database table. You can store the serialized object bytes in Oracle table using BLOB column type. Here is a link describing how you can store and retrieve java objects from a table.

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. Basic logic here,any data is serialized to a byte stream and stored in DB. They are deserialized when fetching from 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.

You shoud use a Object Relational Mapping(ORM) framework for this purpose. It can be Hibernate , JPA or Spring 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. I assume that both fields are 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.

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;
   }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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