简体   繁体   中英

Changing Java file to Stored procedure

I have a java file name E2BXmlParser where I am reading and manipulating the XML data fetched from the database.

Now I am trying to execute the java file using Oracle SQL Developer after changing the file like this

CREATE AND COMPILE JAVA SOURCE NAMED "E2BXmlParser" AS

--(Rest of Code).

And rest of code looks like this--

import oracle.jdbc.*
import oracle.xdb.XMLType;
import oracle.xml.parser.v2.XMLDocument;
import oracle.jdbc.*;
import org.w3c.dom.*;
import org.xml.sax.InputSource;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.*;
import java.sql.Connection;
import java.util.*
import javax.xml.xpath.*;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.io.StringReader;

class Reaction {

}

public class E2BXmlParser {

   //variables

    public E2BXmlParser(int regReportId, int reportId) {
       //connection
    }
    public static void parseXML(int regReportId, int reportId, int isBlinded, int reportFormid,int pi_is_r3_profile,int pi_max_length,String pi_risk_category) throws SQLException, XPathExpressionException, TransformerException {
        //fetching data
    }
    private static Document getDocumentFromString(String xmlContent) throws Exception {

    }

    private String getStringByElementName(String tagName, Element element) {

    }
    private OracleConnection getConnecton() {
       //oracle connection

    }
private Document getXmlDocumentFromDb(int regReportId, int reportId) {
    //fetching and manipulating data
}
private List<Reaction> getReactionIds() {
    //logic 
}
private void findById(Reaction reaction, String id) {

    //xpath for finding nodes

}
private boolean checkNodeExists(Element el, String nodeName) {
    NodeList list = el.getElementsByTagName(nodeName);
    return list.getLength() > 0;
}
private void updateNode(Reaction reaction, Element el) {

  //update xml

}
private void updateXmlInDB(int regReportId, int reportId) throws SQLException {
    //update xml in db
}

private void updateDrugNode() {
    Element rootElement = document.getDocumentElement();
    //logic
}

private void updateDrugEventandDrugRelatedness(int reportFormid) {
    //update xml 

}

private void updateMedicinalActiveSubstance(int regReportId, int isBlinded, int reportFormid,int pi_is_r3_profile,int pi_max_length,String pi_risk_category) {

   //update xml after fetching data and changing in DB
}

private Boolean compareStrings(String strOne, String strTwo) {

    //logic
}

private void updateDosageInformation() {
   //logic
}

private void updateActiveSubstanceName() {

   updating activesubstance using xpath 

}

private void RemoveDuplicateActiveSubstance(NodeList activesubstancenameList, List<String> names) {

// logic
     }
}

Now it is asking for multiple values(reactions,nodelist,node) that are used in code.

But this is not the case when I am executing the java file from command line like this

loadjava -user  username/password@DBalias -r E2BXmlParser.java

PS I have to change my E2BXmlParser.java file to E2BXmlParser.sql file so that I can execute it from oracle sql developer.

Please help.

The easiest solution is wrapping all logic of your class into one static method in class. Next you have to publish this method to pl sql.

And publication of static function will be look (more or less) like this.

CREATE PROCEDURE parseXML (regReportId NUMBER, reportId NUMBER, isBlinded NUMBER, reportFormid NUMBER, pi_is_r3_profile NUMBER, pi_max_length NUMBER, pi_risk_category varchar2)
AS LANGUAGE JAVA
NAME 'E2BXmlParser.parseXML(int regReportId, int reportId, int isBlinded, int reportFormid,int pi_is_r3_profile,int pi_max_length,java.lang.String pi_risk_category)';

Note. In plsql you have to use full path to object example String -> java.lang.String

Of course oracle allows to use java class in more object oriented way but this is more complicated.

For more information check this manual. https://docs.oracle.com/cd/E18283_01/java.112/e10588/toc.htm

Chapter 3 (Calling Java Methods in Oracle Database) - for basic solutions.
Chapter 6 (Publishing Java Classes With Call Specifications) - ( paragraph Writing Object Type Call Specifications) - for publishing full java class.

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