加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

使用byte []在oracle中使用java用户定义的函数

发布时间:2020-12-12 13:14:29 所属栏目:百科 来源:网络整理
导读:鉴于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 (by
鉴于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

解决方法

当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,'iso-8859-1');

查询1:

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

Results:

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读