【问题标题】:java user defined function in oracle using byte[]使用 byte[] 在 oracle 中的 java 用户定义函数
【发布时间】:2011-11-13 08:34:21
【问题描述】:

在 Oracle 中给定这张表

create table test (bytes raw(100), chset varchar2(50))
insert into test (bytes, chset) values (hextoraw('454647'), 'iso-8859-1')

或在 MSSQL 中

create table test (bytes varbinary(100), chset nvarchar(50))
insert into test (bytes, chset) values (0x454647, 'iso-8859-1')

我正在寻找一个综合示例,说明如何使用 Java 的文本编码支持在 Java 中为 Oracle 创建 UDF。

在 MSSQL 中,我将创建这个 .Net 程序集:

using System.Text;
using Microsoft.SqlServer.Server;

namespace Whatever
{
    public class Common
    {
        [SqlFunction]
        public static string Decode(byte[] Bytes, string EncodingName)
        {
            return Encoding.GetEncoding(EncodingName).GetString(Bytes);
        }
    }
}

并使用这些命令注册程序集并定义 udf:

create assembly MyAssembly from '...\MyAssembly.dll'

create function decode(@bytes varbinary(max), @chset nvarchar(100))
returns nvarchar(max) as external name MyAssembly.[Whatever.Common].Decode

并在这样的查询中使用它:

> select *, dbo.decode(bytes, chset) decoded from test

bytes      chset       decoded
0x454647   iso-8859-1  EFG

更新

到目前为止,我已经创建了这个 Java 类:

import java.nio.*;
import java.nio.charset.*;

public class Common
{
    public static String Decode(byte[] Bytes, String CharsetName)
    {
        return Charset.forName(CharsetName).decode(ByteBuffer.wrap(Bytes)).toString();
    }
}

并使用这些命令创建 UDF:

create directory jdir as 'C:\...';
create java class using bfile (jdir, 'Common.class');

create function jdecode(bytes raw, chset varchar2) return nvarchar2 as language java
name 'Common.Decode(java.lang.byte[], java.lang.String) return java.lang.String';

但是当我尝试使用它时,我得到了这个错误:

> select jdecode(hextoraw('454647'), 'iso-8859-1') from dual

ORA-29531: no method Decode in class Common

更新 2

原来 java.lang.byte[] 不是一个东西,把它改成 byte[] 就可以了。谢谢蒂姆!

create function jdecode(bytes raw, chset varchar2) return nvarchar2 as language java
name 'Common.Decode(byte[], java.lang.String) return java.lang.String';

这里有一张方便的桌子:http://download.oracle.com/docs/cd/B19306_01/java.102/b14187/chsix.htm#BABJIJEB

【问题讨论】:

    标签: java sql oracle user-defined-functions java-stored-procedures


    【解决方案1】:

    当 oracle 已经为此类转换提供了足够的实用程序时,您是否真的需要用户定义函数 (UDF) java 或其他方式?

    UTL_I18N 包提供了所有需要的功能。 MAP_CHARSET 函数将 ISO 字符集名称映射到 Oracle 字符集名称,然后 RAW_TO_CHAR 函数将您指定字符集的原始数据转换为数据库字符集中的 VARCHAR2

    SQL Fiddle

    Oracle 11g R2 架构设置

    create table test (bytes raw(100), chset varchar2(50));
    insert into test (bytes, chset) values (hextoraw('454647'), 'iso-8859-1');
    

    查询 1

    select t.*
         , UTL_I18N.RAW_TO_CHAR( bytes
                               , UTL_I18N.MAP_CHARSET(chset,0,1)) decoded
     from test t
    

    Results

    | BYTES |      CHSET | DECODED |
    |-------|------------|---------|
    |  RUZH | iso-8859-1 |     EFG |
    

    【讨论】:

    • 这实际上只是一个玩具问题,试图了解如何让 Java 编写的 UDF 在 Oracle 中运行 - 它最终成为 Oracle 的愚蠢错误“字节与 java.lang.byte”没有提供非常丰富的错误消息。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-17
    • 1970-01-01
    • 1970-01-01
    • 2020-10-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多