H2 database User defined java function Class not f

2019-07-24 03:28发布

问题:

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