H2 database User defined java function Class not found

本小妞迷上赌 提交于 2019-12-24 01:53:26

问题


When I create an ALIAS for registering the java function in the H2 database, it gives error of class not found. I am running the h2 database on a tcp connection.

sample,

public class TimeFrame { 
    public static void main(String... args) throws Exception {
        Class.forName("org.h2.Driver");
        Connection conn = DriverManager.getConnection("jdbc:h2:tcp://MYIP:9092/~/test", "sa", "");
        Statement stat = conn.createStatement();

        //Setup Table
        stat.execute("DROP TABLE IF EXISTS timeframe");
        stat.execute("CREATE TABLE timeframe (last_updated TIMESTAMP, ip int");
        stat.execute("CREATE ALIAS IF NOT EXISTS SLIDEWINDOW FOR \"h2TimeFrame.TimeFrame.slidewindow\" ");
    }
}

This is all in pacakge name: h2TimeFrame. To test,

take the sample class "Function" from the org.h2.samples package. how would you run this class on the server with TCP connection. Changing

  Connection conn = DriverManager.getConnection("jdbc:h2:~/test", "sa", "");

to

  Connection conn = DriverManager.getConnection("jdbc:h2:tcp://IPADDRESS:9092/~/test", "sa", "");

回答1:


Make sure that:

  • The class is public
  • The method is public and static
  • The class must be available in the classpath of the database engine

    From H2 Docs:

    When referencing a method, the class must already be compiled and included in the classpath where the database is running. Only static Java methods are supported; both the class and the method must be public



来源:https://stackoverflow.com/questions/18896093/h2-database-user-defined-java-function-class-not-found

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!