简体   繁体   English

从 Java 返回一个 blob 以调用 PL/SQL function

[英]Returning a blob from Java to calling PL/SQL function

Oracle 19C. Oracle 19C。

This should hopefully be a simple syntax question for somebody.对于某人来说,这应该是一个简单的语法问题。 I am in unfamiliar Java territory as I am using javax.mail to create an email.我在不熟悉的 Java 领域,因为我正在使用 javax.mail 创建 email。

The Java code that I am using is straight out of the Oracle help so I assume it's all OK.我使用的 Java 代码直接来自 Oracle 帮助,所以我认为一切正常。 But what I would like to do is, instead of returning the error number from the Java code, to return the whole message so that I can save it as a blob.但我想做的是,不是从 Java 代码返回错误号,而是返回整个消息,以便我可以将其保存为 blob。 Below is the beginning of the code.下面是代码的开头。 leaving out all of the bits that create the email message, then continuing with the code that I have (unsuccessfully) inserted to save the message.忽略所有创建 email 消息的位,然后继续使用我(未成功)插入的代码来保存消息。 I think I need a couple of lines of code to replace "return ErrorStatus;"我想我需要几行代码来替换“return ErrorStatus;”

CREATE OR REPLACE AND COMPILE JAVA SOURCE NAMED "BarrySendMail" AS
 import java.util.*;
 import java.io.*;
 import java.sql.*;
 import javax.mail.*;
 import javax.mail.internet.*;
 import javax.activation.*;
  
  
 public class BarrySendMail {
  // Sender, Recipient, CCRecipient, and BccRecipient are comma-separated
  // lists of addresses. Body can span multiple CR/LF-separated lines.
  // Attachments is a ///-separated list of file names.
  public static int Send(String SMTPServer,
       String Sender,
       String Recipient,
       String CcRecipient,
       String BccRecipient,
       String Subject,
       String Body,
       String ErrorMessage[],
       String Attachments) {
  // Error status;
  int ErrorStatus = 0;
   
  //Connection
  Connection con = null;

  // Create some properties and get the default Session;
  Properties props = System.getProperties();
  props.put("blahblah", SMTPServer);
  Session session = Session.getDefaultInstance(props, null);
  session.setDebug(true);
  try {
   // Create a message.
   MimeMessage msg = new MimeMessage(session);

etc...then continues...等等……然后继续……

//Save the message to a binary object **This is my attempy**
   ByteArrayOutputStream bos = new ByteArrayOutputStream();
   ByteArrayInputStream bis = null;
   try{
    msg.writeTo(bos);
    }catch(Exception exp){
    exp.printStackTrace();}
// Send the message;
   Transport.send(msg);
  
 
  } catch (MessagingException MsgException) {
   ErrorMessage[0] = MsgException.toString();
   Exception TheException = null;
   if ((TheException = MsgException.getNextException()) != null)
    ErrorMessage[0] = ErrorMessage[0] + "\n" + TheException.toString();
    ErrorStatus = 1;
  }
   
  //return new ByteArrayInputStream(bos.toByteArray());
   return ErrorStatus; //***Instead, I would like to return bos;
   
  //return ErrorStatus;
  } // End Send Class
 } // End of public class BarrySendMail

This is in turn called by a function:这又由 function 调用:

           Sender IN STRING,
           Recipient IN STRING,
           CcRecipient IN STRING,
           BccRecipient IN STRING,
           Subject IN STRING,
           Body IN STRING,
           ErrorMessage OUT STRING,
           Attachments IN STRING) RETURN blob IS
  LANGUAGE JAVA
  NAME 'BarrySendMail.Send(java.lang.String,
            java.lang.String,
            java.lang.String,
            java.lang.String,
            java.lang.String,
            java.lang.String,
            java.lang.String,
            java.lang.String[],
            java.lang.String) return java.sql.Blob';

Essentially I'm asking how to return "msg.writeTo(bos);"本质上我是在问如何返回“msg.writeTo(bos);” as a blob to my calling function.作为我调用 function 的一团。

Thanks in advance,提前致谢,

Barry巴里

So I made a workaround by saving it to a table.因此,我通过将其保存到表格来解决问题。

It probably looks horrible to a Java programmer but seems to work.对于 Java 程序员来说,它可能看起来很糟糕,但似乎可以工作。

//Save the message to 
           ByteArrayOutputStream bos = new ByteArrayOutputStream();
           ByteArrayInputStream bis = null;
           //memory = new MemoryStream ();
           try  {
                msg.writeTo(bos);
                int i = bos.toByteArray().length;
                byte[] array = new byte[i];
                array = bos.toByteArray();
                
                //insertIntoProducts(desBlob);
                Connection conn = DriverManager.getConnection("jdbc:default:connection:");
                Blob desBlob = conn.createBlob();
                desBlob.setBytes(1, array);
                //PreparedStatement ps2 = conn.prepareStatement("insert into mytable values (hextoraw(?))");
                PreparedStatement ps2 = conn.prepareStatement("insert into barry_testtable(blobcolumn) values (?)");
                ps2.setBytes(1,array);
                try{
                int rowsAffected = ps2.executeUpdate();
                System.out.println(rowsAffected); //1
                } catch (SQLException e) {
                    System.err.format("SQL State: %s\n%s", e.getSQLState(), e.getMessage());
                } catch (Exception e) {
                    e.printStackTrace();
                }
                
           }catch(Exception exp){
               exp.printStackTrace();}

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

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