Java存储过程返回ARRAY

时间:2016-11-06 19:17:42

标签: java oracle stored-procedures java-stored-procedures

我有以下用例,我有一个表类型作为我的函数的OUT参数调用java存储过程。我正在努力让这个工作。你能在这方面帮助我吗?

SQL记录类型

TYPE tmsint_xfer_html_ws_objr AS OBJECT                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          
 (file_name           VARCHAR2(200),                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
  html_text           VARCHAR2(2000),                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
  job_id              NUMBER(10)); 

SQL表格类型

TYPE tmsint_xfer_html_ws_objt                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
  AS TABLE OF tmsint_xfer_html_ws_objr;

Java POJO映射SQL记录类型:

import java.math.BigDecimal;

import java.sql.SQLData;
import java.sql.SQLException;
import java.sql.SQLInput;
import java.sql.SQLOutput;

public class ExtractXmlLine implements SQLData {

    public ExtractXmlLine(String file_name, String html_text,BigDecimal job_id) {
        super();
        this.file_name = file_name;
        this.html_text = html_text;
        this.job_id = job_id;

    }

    private String sql_type;
    private String file_name;
    private String html_text;
    private BigDecimal job_id;    

    @Override
    public String getSQLTypeName() throws SQLException {
        return sql_type;
    }

    @Override
    public void readSQL(SQLInput stream, String typeName) throws SQLException {
        sql_type = typeName;
        job_id = stream.readBigDecimal();
        file_name = stream.readString();
        html_text = stream.readString();
    }

    @Override
    public void writeSQL(SQLOutput stream) throws SQLException {
        stream.writeBigDecimal(job_id);
        stream.writeString(file_name);
        stream.writeString(html_text);
    }
}

注册为java存储过程的Java类

public class ExtractData {
    public ExtractData() {
        super();
    }

    public static void getDataFromURL(String jobId, String sourceUrl, String userName, String password,
                                                   ExtractXmlLine[] xmlResponse, String[] errorMessage) {

        try {
            conn = JDBCUtil.getConnection();
            java.util.Map map = conn.getTypeMap();
            map.put("TMSINT_XFER_HTML_WS_OBJT", Class.forName("ExtractXmlLine"));
            conn.setTypeMap(map);

            response = HttpUtil.getHttpResponse(sourceUrl, userName, password);

            if (response.getStatus().equals(ReturnStatus.SUCCESS)) {
                String xmlBody = response.getResponseBody();
                if (xmlBody != null) {
                    xmlBody = xmlBody.replaceAll("[^\\x20-\\x7e]", "");
                    xmlBody = XMLUtil.format(xmlBody);
                    textLines.addAll(Arrays.asList(xmlBody.split("\\r\\n|\\n|\\r")));
                }

                if (!textLines.isEmpty()) {
                    dataLines = new ExtractXmlLine[textLines.size()];
                    for (int i = 0; i < textLines.size(); i++) {
                        // ignore xml declaration lines
                        if (!textLines.get(i).startsWith("<?xml")) {
                              xmlResponse[i] = new ExtractXmlLine(sourceUrl, textLines.get(i), new BigDecimal(jobId));                            
                        }
                    }                   
                }
            } else
                errorMessage[0] = response.getErrorMessage() + response.getResponseBody();
        } catch (Exception e) {
            errorMessage[0] = ExceptionUtils.getStackTrace(e);
        }
    }       

}

包装java调用的SQL过程

 create or replace PROCEDURE getDataFromURL
    (pJobID         IN VARCHAR2,
     pSourceURL     IN VARCHAR2,
     pUserName      IN VARCHAR2,
     pPassword      IN VARCHAR2,
     pXMLResponse   OUT tmsint_xfer_html_ws_objt,
     pErrorMessage  OUT VARCHAR2)
  IS LANGUAGE JAVA
     NAME 'ExtractData.getDataFromURL
             (java.lang.String,
              java.lang.String,
              java.lang.String,
              java.lang.String,
              ExtractXmlLine[],
              java.lang.String[])';

这是正确的做法吗?我在这里错过了什么吗?

0 个答案:

没有答案