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

发布于 2024-12-04 05:04:02 字数 2423 浏览 0 评论 0原文

鉴于 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 的文本编码支持为 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

Given this table in Oracle

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

or in MSSQL

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

I'm looking for a comprehensive example of how to create a UDF for Oracle in Java that makes use of Java's text encoding support.

In MSSQL I would create this .Net assembly:

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);
        }
    }
}

and use these commands to register the assembly and define the 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

and use it in a query like this:

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

bytes      chset       decoded
0x454647   iso-8859-1  EFG

update

so far I've created this Java class:

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();
    }
}

and used these commands to create the 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';

but when I try to use it I get this error:

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

ORA-29531: no method Decode in class Common

update 2

turns out java.lang.byte[] isn't a thing, changing it to just byte[] got things working. Thanks Tim!

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

there's a handy table here: http://download.oracle.com/docs/cd/B19306_01/java.102/b14187/chsix.htm#BABJIJEB

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

尬尬 2024-12-11 05:04:02

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

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

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

结果

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

Do you really need a user defined function (UDF) java or otherwise when oracle already provides sufficient utilities for such conversions?

The UTL_I18N package supplies all the needed functionality. The MAP_CHARSET funtion maps from ISO character set names to Oracle character set names, then the RAW_TO_CHAR function converts your raw data of the specified character set to a VARCHAR2 in the database character set.

SQL Fiddle

Oracle 11g R2 Schema Setup:

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

Query 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 |
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文