0

我正在尝试将 java 类作为函数加载到 oracle。在服务器中,我设法使用 loadjava,如下所示:

C:\Users\n12017>loadjava -user USER1/passw E:\JAVA_repository\SOOSProjects\Mehmet_java_2db_trial\classes\mehmet_java_2db_trial\kondrakk.class

在 oracle 数据库端:

create or replace function ngram_kondrakk(src in varchar2, trg in varchar2)
  return float
as language java
  name 'mehmet_java_2db_trial/kondrakk.getDistance(java.lang.string, java.lang.string) return java.lang.float';
/

但是,当我应用如下查询时,出现错误。(作为查询的结果,我期望相似度得分为 1,因为比较了两个相同的字符串)

select ngram_kondrakk('mehmet','mehmet') from dual;

这是错误:

ORA-29532: Java call terminated by uncaught Java exception: System error : java/lang/UnsupportedClassVersionError
29532. 00000 -  "Java call terminated by uncaught Java exception: %s"
*Cause:    A Java exception or error was signaled and could not be
           resolved by the Java code.
*Action:   Modify Java code, if this behavior is not intended.

最后,这是我尝试使用的代码:

package mehmet_java_2db_trial;

公共课 kondrakk {

public static float getDistance(String source, String target) {
  final int sl = source.length();
  final int tl = target.length();

  if (sl == 0 || tl == 0) {
    if (sl == tl) {
      return 1;
    }
    else {
      return 0;
    }
  }

  int n=3;

  int cost = 0;
  if (sl < n || tl < n) {
    for (int i=0,ni=Math.min(sl,tl);i<ni;i++) {
      if (source.charAt(i) == target.charAt(i)) {
       cost++;
      }
    }
    return (float) cost/Math.max(sl, tl);
  }

  char[] sa = new char[sl+n-1];
  float p[]; //'previous' cost array, horizontally
  float d[]; // cost array, horizontally
 float _d[]; //placeholder to assist in swapping p and d

  //construct sa with prefix
  for (int i=0;i<sa.length;i++) {
    if (i < n-1) {
      sa[i]=0; //add prefix
    }
    else {
      sa[i] = source.charAt(i-n+1);
    }
  }
  p = new float[sl+1]; 
  d = new float[sl+1]; 

  // indexes into strings s and t
  int i; // iterates through source
  int j; // iterates through target

  char[] t_j = new char[n]; // jth n-gram of t

 for (i = 0; i<=sl; i++) {
     p[i] = i;
 }

 for (j = 1; j<=tl; j++) {
     //construct t_j n-gram 
     if (j < n) {
     for (int ti=0;ti<n-j;ti++) {
         t_j[ti]=0; //add prefix
       }
       for (int ti=n-j;ti<n;ti++) {
         t_j[ti]=target.charAt(ti-(n-j));
       }
     }
     else {
       t_j = target.substring(j-n, j).toCharArray();
     }
     d[0] = j;
     for (i=1; i<=sl; i++) {
         cost = 0;
         int tn=n;
         //compare sa to t_j
         for (int ni=0;ni<n;ni++) {
           if (sa[i-1+ni] != t_j[ni]) {
             cost++;
           }
           else if (sa[i-1+ni] == 0) { //discount matches on prefix
             tn--;
           }
       }
         float ec = (float) cost/tn;
         // minimum of cell to the left+1, to the top+1, diagonally left and up +cost
         d[i] = Math.min(Math.min(d[i-1]+1, p[i]+1),  p[i-1]+ec);
     }
     // copy current distance counts to 'previous row' distance counts
     _d = p;
     p = d;
     d = _d;
 }

 // our last action in the above loop was to switch d and p, so p now
 // actually has the most recent cost counts
 System.out.println(1.0f - (p[sl] / Math.max(tl, sl)));
 return 1.0f - (p[sl] / Math.max(tl, sl));

} 

}

请帮忙!

提前致谢...

4

2 回答 2

0

编译 Java 类以加载到 Oracle 数据库中时,请确保编译 Java 类以在 Oracle 数据库中与 JVM 一起运行。

与当前的 Java 相比,Oracle 数据库附带的 Java 版本通常已经过时:预计将使用 Java 1.4 和 Oracle 10g 和 1.5 和 11g。您最好的选择是使用数据库附带的 Java 编译器,但如果您不能这样做,请使用-target 1.5或类似方法强制编译器编译类以在 Java 1.5 上运行。

于 2014-06-25T20:13:42.657 回答
0

您的 plsql 包装函数有“/”

...mehmet_java_2db_trial/kondrakk.getDistance...

/替换为. [点]

检查文档 ,正如已经提到的 - 将编译的 JVM 与运行时的 JVM 同步(这将是“附加”到 DB 的 Oracle JVM)

于 2014-06-24T03:18:12.853 回答