oracle中使用byte[]的java用户定义函数
鉴于 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当 oracle 已经为此类转换提供了足够的实用程序时,您真的需要用户定义函数 (UDF) java 或其他吗?
UTL_I18N
包提供了所有需要的功能。MAP_CHARSET
函数从 ISO 字符集名称映射到 Oracle 字符集名称,然后RAW_TO_CHAR
函数将指定字符集的原始数据转换为VARCHAR2
code> 数据库字符集中。SQL Fiddle
Oracle 11g R2 架构设置:
查询1:
结果:
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. TheMAP_CHARSET
funtion maps from ISO character set names to Oracle character set names, then theRAW_TO_CHAR
function converts your raw data of the specified character set to aVARCHAR2
in the database character set.SQL Fiddle
Oracle 11g R2 Schema Setup:
Query 1:
Results: