1. Database version
2.Java Script
Importjava.security.MessageDigest;ImportCOM.ibm.db2.app.UDF; Public classMd5udfextendsUDF { Public Staticstring MD5 (String s) {string S1=NewString (""); CharHexdigits[] = {' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', ' A ', ' B ', ' C ', ' d ', ' e ', ' F ' }; Try { byte[] strtemp =s.getbytes (); MessageDigest mdtemp= Messagedigest.getinstance ("MD5"); Mdtemp.update (strtemp); byte[] MD =mdtemp.digest (); intj =md.length; CharStr[] =New Char[J * 2]; intK = 0; for(inti = 0; I < J; i++) { byteBYTE0 =Md[i]; Str[k+ +] = hexdigits[byte0 >>> 4 & 0xf]; Str[k+ +] = hexdigits[byte0 & 0xf]; } S1=NewString (str); } Catch(Exception e) {System.out.println (E.getmessage ()); } return"{" + S1 + "}"; } Public Static voidMain (string[] args) {System.out.println (MD5 ("Asdf")); }}
3. Run Javac Compilation
Javac Md5udf.java
4. Run the jar package Md5udf.class to Md5udf.jar
Jar CF Md5udf.jar Md5udf.class
5. Connect the database, run the install. jar file into the database (C:\Documents and Settings\All Users\Application Data\ibm\db2\db2copy1\function\jar\zen directory)
DB2 Connect to sample
DB2 "Call Sqlj.install_jar (' File:e:\md5udf.jar ', ' md5udf ')"
6. Login Database Create function
DROP FUNCTION bi_md5; CREATE FUNCTION BI_MD5 (varchar)RETURNSvarchar md5udf:md5udf. MD5'FENCED variantno sqlexternal ACTION LANGUAGE javaparameter STYLE JAVA
7. Call the test
Select Bi_md5 (' 12345 ') from sysibm.dual
Ok!
Conclusion:
1. Installing, deleting, and replacing JAR files
DB2 "Call Sqlj.install_jar (' File:d:\somebookes\java\md5udf2.jar ', ' BIMD5 ')"
DB2 "Call Sqlj.remove_jar (' BIMD5 ')"
DB2 "Call Sqlj.replace_jar (' File:e:\mydb2\mylog.jar ', ' BIMD5 ')"
2. Refresh the already called jar or class without restarting the instance to take effect:
DB2 "Call SQLJ. Refresh_classes () "
3. The Java mode of the call must be static.
4. The SDK version compiled into the jar package must match the version of DB2
Otherwise it will be an error
Reference article:
Http://bbs.chinaunix.net/thread-966098-1-1.html
http://www.ibm.com/developerworks/cn/data/library/techarticle/dm-1012liucs/
Http://www.ibm.com/developerworks/data/library/techarticle/dm-0510law/index.html
DB2 using Java stored procedures to implement MD5 functions