3

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
  • 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", ""); Commented Sep 23, 2013 at 16:38

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

3 Comments

Yes. "in the classpath of the database engine" in this case means it has to be in the classpath of the server (in the process / JVM where the database server is running)
It might be simpler to declare the user defined function is defined as source code. That way it is automatically available on the server.
@javadevil, I did made sure about the 3 things u mentioned and they are also in the documentation. But, didnt worked. For the above given sample class (TimeFrame), if I make a runnable jar and include it in the classpath when starting the server (org.tools.server), should it work? as its not working! am i missing something here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.