Forum Stats

  • 3,874,172 Users
  • 2,266,677 Discussions
  • 7,911,753 Comments

Discussions

Returning a blob from Java to calling PL/SQL function

Hi Experts,

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.

The Java code that I am using is straight out of the Oracle help so I assume it's all OK. 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. 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. I think I need a couple of lines of code to replace "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 JSendMail(SMTPServerName IN STRING,
           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);" as a blob to my calling function.


Thanks in advance,

Barry

Tagged:

Answers

  • BarryB
    BarryB Member Posts: 10 Green Ribbon

    I didn't figure it out, (didn't get any help on SO either).

    So I made a workaround by saving it to a table.


    It probably looks horrible to a Java programmer but seems to work.


    //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();}