Skip to Main Content

Java and JavaScript in the Database

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Returning a blob from Java to calling PL/SQL function

BarryBAug 11 2022

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

Comments

Answer

they are ALL locked until you either unlock or end the session.

Marked as Answer by Mubashar Iftikhar · Sep 27 2020
Mubashar Iftikhar

Hi Dear
I need some guidance.

Can you please share with me your email address ?

Dave Stokes-MySQL Community Team-Oracle

it would probably be better to get help from https://forums.mysql.com as i am travelling so much this time of year.  Our MySQL Oracle Enginners monitor the forums and will be fast to answer then I

Mubashar Iftikhar

Actually i need guideline of MYSQL 5.6 Exam , and at forms i dont think i can get better information .

I am no longer involved with the MySQL certification exams but can tell you I took the beta of the 5.6 DBA exam and found it very hard.   Another member of the MySQL Community Team at the time of the beta took the DBA and Developer exams and concurred that they were very hard.  The best advise is to go through the list of items covered on the exam and study your rear end off.

Good luck.

Mubashar Iftikhar

Dear Sir

      I know this exam is very hard because i have already attempted 2 times and not able to clear the exam. . & i have done my complete preparation from official documentation https://dev.mysql.com/doc/refman/5.6/en/preface.html plus 6 months practical work. Now i just trying to find out a way how to clear this exam because this task giving me a lot of headache.

Mubashar Iftikhar

Hi Dear

I am in search of open source data platform which can perform ETL over Big data (structured, semi-structured & un-structured) and analytical tools for data visualization to build analytical dashboards.

I will be grateful if you can share your choice of open source data platform and data visualization tools information & your use case high level information their key features and unique capabilities. Also relevant enablement resources/training options.

Looking forward for a healthy discussion over here.

Regards

Mubashar Iftikhar

Mubashar Iftikhar

Dear Dave Stokes,
I need some help.

I have two MySQL Database Servers i.e. One is Active Server --> Master and second is Passive Server --> Slave.

And as we know we have 3-4 MySQL HR,DR Solutions / Clustering options.For example

01 : MySQL NDB Cluster

02 : MySQL InnoDB Cluster

03 : MySQL Cluster Carrier Grade

My First Question is i want to deploy One Active & One Passive MySQL Server so which Cluster Option / HA,DR Option will be fit for above mentioned scenario ?

My Second Question is suppose i have deploy One Active & One Passive MySQL Server in any above mentioned Cluster Option / HA,DR Option. So what about licensing ? I have to buy license of MySQL Servers/ Cluster ?

Regards,

Mubashar Iftikhar

Please see your other question asked today.

1 - 9

Post Details

Added on Aug 11 2022
1 comment
728 views