---法1:数据库方法
create or replace function FUNC_GEN_PAY_ID
return CHAR
is
v_param varchar(1);--字符参数
v_payid_param varchar(4);--字符累加
v_pay_id varchar(4);--4位字符
v_x number;--循环的次数变量
v_y number;--是否生成 1:是 0:否
v_rowCnt number ;
begin
--初始值
v_x :=1;
v_y :=1;
v_payid_param :=‘‘;
WHILE v_y =1 LOOP
FOR v_x IN REVERSE 1 .. 4 LOOP
select * into v_param from(
select *
from(
select chr(ascii(‘A‘)+ROWNUM-1) S from dual connect by rownum<=26
UNION ALL
select chr(ascii(‘a‘)+ROWNUM-1) S from dual connect by rownum<=26
UNION ALL
select chr(ascii(‘0‘)+ROWNUM-1) S from dual connect by rownum<=10
)ORDER BY dbms_random.value)
where rownum<2;
v_payid_param :=v_payid_param||v_param;
END LOOP;
SELECT COUNT(*)
INTO v_rowCnt
FROM DUAL
WHERE EXISTS(
SELECT 1
FROM yst_pay_info
WHERE is_delete = ‘0‘
AND pay_id=v_payid_param);
IF v_rowCnt = 1 THEN
v_y :=1;
ELSE
v_pay_id :=v_payid_param;
v_y :=0;
END IF;
END LOOP;
return(v_pay_id);
end FUNC_GEN_PAY_ID;
--法2:Java方法(未校验是否唯一)
public class aa {
/**
* @param args
*/
public static void main(String[] args) {
System.out.println(get4Code());
}
public static String get4Code(){
StringBuffer sb = new StringBuffer();
String codes ="qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890";
Random random = new Random();
for(int i=0;i<codes.length();i++){
char c = codes.charAt(random.nextInt(codes.length()));
System.out.println(c);
if(sb.length()==4) break;
if(!sb.toString().contains(c+"")) sb.append(c);
}
return sb.toString();
}
}