直接在oracle中创建java source,新建函数进行调用即可。
说明:传入17位字符,返回第18位校验码。
<!--more-->
[code lang="java"]
create or replace and compile java source named operationutils as
import java.rmi.server.UID;
import java.text.DecimalFormat;
import java.text.NumberFormat;
public class OperationUtils
{
/**
* <p>Discription: 生成主键</p>
* @return 主键
* @update :
*/
public synchronized static String getUID() {
String ret = "";
try {
ret = new UID().toString();
} catch (Exception e) {
e.printStackTrace();
}
return ret;
}
/**
* <p>Discription: 格式化数字,如#,##0.00将double类型的转为千分符的字符串</p>
* @param d 数字
* @param pattern 格式化类型
* @return
* @update :
*/
public static String formatNumber(double d,String pattern) {
String s = "";
//String pattern = "#,##0.00";"##0.00"
try {
DecimalFormat nf = (DecimalFormat) NumberFormat.getInstance();
nf.applyPattern(pattern);
s = nf.format(d);
} catch (Exception e) {
e.printStackTrace();
//Debug.println(" formatNumber is error!");
}
return s;
}
/**
* <p>Discription: 生成票号校验码</p>
* @param billCode 票号
* @return 票号校验码
* @update :
*/
public static String calculateBillMac(String billCode) {
String checkCode = calculateMac(billCode);
if (checkCode == null) {
return null;
}
int length = checkCode.length();
int sum = 0;
//将各位相加
for (int i = 0; i < length; i++) {
int thisbit = Integer.parseInt(String.valueOf(checkCode.charAt(i)));
sum += thisbit;
}
//取最终和的个位数。
String lastbit = sum % 10 + "";
return lastbit;
}
/**
* <p>Discription: 生成单位项目关联校验码</p>
* @param unitItem 单位项目关联码
* @return
* @update :
*/
public static String calculateUnitItemMac(String unitItem) {
String checkCode = calculateMac(unitItem);
if (checkCode == null && !(checkCode.length() == 4)) {
return null;
}
int[] temp = new int[4];
for (int i = 0; i < 4; i++) {
temp[i] = Integer.parseInt(String.valueOf(checkCode.charAt(i)));
}
//前两位相加的个位数
String s1 = (temp[0] + temp[1]) % 10 + "";
//后两位相加的个位数
String s2 = (temp[2] + temp[3]) % 10 + "";
return s1 + s2;
}
/**
* <p>Discription: 生成全票面信息校验码</p>
* @param billInfo
* @return
* @author : 王在勇 wangzaiy@neusoft.com
* @update :
*/
public static String calculateBillInfoMac(String billInfo) {
return calculateMac(billInfo);
}
/**
* <p>Discription: 生成校验码</p>
* @param str 需要生成校验码的字符串
* @return
* @author : 王在勇 wangzaiy@neusoft.com
* @update :
*/
private static String calculateMac(String str) {
/**
* 空字符串处理
*/
if (str == null || "".equals(str)) {
System.out.println("需要生成校验码的字符串不能为空!");
return null;
}
byte[] bytes = null;
try {
bytes = str.getBytes("GBK");
} catch (Exception e) {
e.printStackTrace();
}
//校验码生成
int input_len = bytes.length; //计算输入字符串长度
String ascii_buff = ""; //存储ASCII码字符串
String str_buff_8 = ""; //存储八进制数字串的字符串
String str_buff_10 = ""; //存储十进制数字串的字符串
String str_return = ""; //返回值字符串
int i, len_8, len_10;
long sum8_1 = 0, sum8_2 = 0, sum10_1 = 0, sum10_2 = 0; //分别存储八进制和十进制不同算法取值的合计
for (i = 0; i < input_len; i++) { //对输入字符串的每一个字节进行循环
int byteint = unsignedInt(bytes[i]);
ascii_buff = Integer.toOctalString(byteint); //将ASCII码值转换成八进制字符串
str_buff_8 += ascii_buff; //所有八进制ASCII串连接
ascii_buff = Integer.toString(byteint); //将ASCII码值转换成十进制字符串
str_buff_10 += ascii_buff; //所有十进制ASCII串连接
}
len_8 = str_buff_8.length(); //八进制字符串总长度
len_10 = str_buff_10.length(); //十进制字符串总长度
//将八进制字符串的每一个字符转换成数字,并分别按照正序和反序乘以所在的位置序号
//例如:八进制串为 3452170467
//sum8_1 = 3*1 + 4*2 + 5*3 + 2*4 + 1*5 + ....
//sum8_2 = 7*1 + 6*2 + 4*3 + 0*4 + 7*5 + ....
for (i = 1; i <= len_8; i++) {
sum8_1 += (str_buff_8.charAt(i - 1) - '0') * i;
sum8_2 += (str_buff_8.charAt(len_8 - i) - '0') * i;
}
//将十进制字符串的每一个字符转换成数字,并分别按照正序和反序乘以所在的位置序号
for (i = 1; i <= len_10; i++) {
sum10_1 += (str_buff_10.charAt(i - 1) - '0') * i;
sum10_2 += (str_buff_10.charAt(len_10 - i) - '0') * i;
}
str_return = String.valueOf(sum8_1 % 10) + String.valueOf(sum8_2 % 10) + String.valueOf(sum10_1 % 10)
+ String.valueOf(sum10_2 % 10);
//System.out.println(str_return);
return str_return;
}
/**
* <p>Discription: 获得指定字节的无符号整数</p>
* @param byteValues
* @return
* @update :
*/
public static int unsignedInt(byte byteValues) {
int intValue = new Byte(byteValues).intValue();
String bstr = Integer.toBinaryString(intValue);
if (intValue >= 0) {
return intValue;
}
String subStr = bstr.substring(24, bstr.length());
intValue = Integer.parseInt(subStr, 2);
return intValue;
}
/**
* 计算票据编号校验码
* 将给出的字符串经过运算生成一位数字或X校验码
* @param source 原字符串
* @return 生成的校验码
* @update :
*/
public static String getBillCheckCode(String source) {
int len, aMod, sum, Wi;
int m_table[] = { 1, 0, 0, 9, 8, 7, 6, 5, 4, 3, 2 };
int li_round, li_b, amod;
String myx;
sum = 0;
if (source == null || source.trim().length() == 0) {
return "-1";
} else {
len = source.trim().length();
}
for (int i = 1; i < len + 1; i++) {
// double temp = Math.exp(len - 1 + i);
// double templog = Math.log(2);
li_round = (int) Math.rint(Math.exp((len - i + 1) * Math.log(2)));
Wi = (int) (li_round % 11);
li_b = source.charAt(i - 1);
sum += Wi * (li_b - '0');
}
amod = (int) (sum % 11);
if (amod == 2) {
myx = "X";
} else {
aMod = m_table[amod];
/* 把整型转化成字符 */
myx = String.valueOf(aMod);
}
return myx;
}
}
[/code]
[code lang="java"]
create or replace function getBCD2(billnumber varchar2) return varchar2
as language java name 'OperationUtils.getBillCheckCode(java.lang.String) return java.lang.String';
[/code]