JAVA Source
有的時(shí)候妻率,我們需要在Oracle的Prodedure乱顾、Function中調(diào)用JAVA代碼來(lái)實(shí)現(xiàn)功能,比方說(shuō)下面的這個(gè)例子走净,調(diào)用JAVA代碼來(lái)實(shí)現(xiàn)大小寫金額的轉(zhuǎn)換券时,如果這個(gè)代碼用PL實(shí)現(xiàn),難度還是有的,那如果我們先寫成JAVA代碼,然后通過(guò)調(diào)用弄唧,來(lái)實(shí)現(xiàn)澄干,還是可以的逛揩。
CREATE or replace and compile java source named js_translater
AS
package com.yzupope.util;
/*
* 人民幣大小寫金額轉(zhuǎn)化
*/
public class XhgUtil {
private static final String UNIT = "萬(wàn)千佰拾億千佰拾萬(wàn)千佰拾元角分";
private static final String DIGIT = "零壹貳叁肆伍陸柒捌玖";
private static final double MAX_VALUE = 9999999999999.99D;
public static String trans(double v) {
if (v < 0 || v > MAX_VALUE)
return "參數(shù)非法!";
long l = Math.round(v * 100);//round(double a):返回最接近參數(shù)的long(返回值為static long)
if (l == 0)
return "零元整";
String strValue = l + "";
// j用來(lái)控制單位
int j = UNIT.length() - strValue.length();
String rs = "";
boolean isZero = false;
// i用來(lái)控制數(shù)
for (int i = 0; i < strValue.length(); i++, j++) {
char ch = strValue.charAt(i);
if (ch == '0') {
isZero = true;
if (UNIT.charAt(j) == '億' || UNIT.charAt(j) == '萬(wàn)'
|| UNIT.charAt(j) == '元') {
rs = rs + UNIT.charAt(j);
isZero = false;
}
} else {
if (isZero) {
rs = rs + "零";
isZero = false;
}
rs = rs + DIGIT.charAt(ch - '0') + UNIT.charAt(j);
}
}
if (!rs.endsWith("角") && !rs.endsWith("分")) {
rs = rs + "整";
}
rs = rs.replaceAll("億萬(wàn)", "億");
return rs;
}
}
創(chuàng)建引用
CREATE OR REPLACE FUNCTION xchg(v NUMBER) RETURN VARCHAR2
AS
LANGUAGE JAVA NAME 'com.yzupope.util.XhgUtil.trans(double)return java.lang.String';
函數(shù)調(diào)用
select xchg(105.91) FROM dual;
Where are java classes stored in Oracle?(如何查看呢)
SELECT object_name, object_type, status, TIMESTAMP
FROM user_objects
WHERE (object_name NOT LIKE 'SYS_%' AND object_name NOT LIKE 'CREATE$%' AND
object_name NOT LIKE 'JAVA$%' AND object_name NOT LIKE 'LOADLOB%')
AND object_type LIKE 'JAVA %'
ORDER BY object_type, object_name;