Using Java Sources in Oracle Database

Posted by SycoSyco on Fri, 13 Dec 2019 20:33:24 +0100

Using Java Sources in Oracle Database

Because of some special requirements, the password of the user table in the database needs to be changed to "user name + specified character". Therefore, the encryption algorithm in the program is changed to java sources, function call, convenient to update data.

Environment: Oracle 11g R2

I. java code preparation

The java code is as follows

package com.xxxxsoft.xxxxxx.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class EncryptUtil {

    /**
     * Encryption algorithm, a search on the Internet, for testing
     * @param str String to be encrypted
     * @return Encrypted string
     */
    public static String md5crypt(String str) {
        if ((((str == null) || (str.length() == 0)))) {
            return "";
        }
        StringBuffer hexString = new StringBuffer();
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(str.getBytes());
            byte[] hash = md.digest();
            for (int i = 0; i < hash.length; ++i){
                if ((0xFF & hash[i]) < 16) {
                    hexString.append("0" + Integer.toHexString(0xFF & hash[i]));
                } else {
                    hexString.append(Integer.toHexString(0xFF & hash[i]));
                }
            }
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return hexString.toString();
    }

    public static void main(String[] args) {
        System.out.println(md5crypt("1"));
        // c4ca4238a0b923820dcc509a6f75849b
    }
}

2. Rewrite the encryption algorithm in the program into Java Sources

1. Add create or replace and compile java source named EncryptUtil as in the first line.

  • java source named, as shown above, I used the java class name (EncryptUtil), which can be customized.

2. The second line, package name.

  • ① use the package name in the original code: "package com. Xxxsoft. XXXXX. Util;"
  • Second, rewriting to other content should follow the naming standard of Java package;
  • ③ do not write the package name (call function below, note that the package name is not written)

The java sources sql script is as follows

create or replace and compile java source named EncryptUtil as
package com.xxxxsoft.xxxxxx.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class EncryptUtil {

    /**
     * Encryption algorithm, a search on the Internet, for testing
     * @param str The string to be encrypted has only one parameter in this case
     * @return Encrypted string
     */
    public static String md5crypt(String str) {
        if ((((str == null) || (str.length() == 0)))) {
            return "";
        }
        StringBuffer hexString = new StringBuffer();
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(str.getBytes());
            byte[] hash = md.digest();
            for (int i = 0; i < hash.length; ++i){
                if ((0xFF & hash[i]) < 16) {
                    hexString.append("0" + Integer.toHexString(0xFF & hash[i]));
                } else {
                    hexString.append(Integer.toHexString(0xFF & hash[i]));
                }
            }
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return hexString.toString();
    }

    public static void main(String[] args) {
        System.out.println(md5crypt("1"));
    }
}

3. Create a function to call java sources

In this case, there is only one parameter (str: string to be encrypted);
The return value is the encrypted string

create or replace function func_get_password(str in STRING)

 RETURN String is
  -- Package name + java sources name + Method name(parameter)
  language java name 'com.xxxxsoft.xxxxxx.util.EncryptUtil.md5crypt(java.lang.String) return java.lang.String';

Four, test

The string "1" is encrypted with SQL and java respectively.

  • SQL query
SELECT func_get_password('1') from dual ;

Query result: c4ca4238a0b923820dcc509a6f75849b

  • Call in Java
public static void main(String[] args) {
    System.out.println(md5crypt("1"));
}

Terminal output: c4ca4238a0b923820dcc509a6f75849b

  • As shown above, the two contents are the same, oracle java sources succeeded!

Topics: Database Java Oracle SQL