javajdbch2olap4j

Get a olap4j.OlapConnection from a org.h2.jdbc.JdbcConnection


Is there a way to get a olap4j.OlapConnection from a org.h2.jdbc.JdbcConnection?

Right now I am trying this (as shown there ):

Class.forName("org.h2.Driver");
Class.forName("mondrian.olap4j.MondrianOlap4jDriver");
Connection conn = DriverManager.getConnection("jdbc:h2:~/test;", "sa", "");
OlapWrapper wrapper = (OlapWrapper) conn;
OlapConnection olapConn = wrapper.unwrap(OlapConnection.class);

But I get a

Exception in thread "main" java.lang.ClassCastException: org.h2.jdbc.JdbcConnection cannot be cast to org.olap4j.OlapWrapper at myprogram.main(Mondrian2H2Test.java:67)

:67 is OlapWrapper wrapper = (OlapWrapper) conn;

EDIT :

I also tried this :

Class.forName("org.h2.Driver");
Class.forName("mondrian.olap4j.MondrianOlap4jDriver");
Connection conn = DriverManager.getConnection("jdbc:h2:~/test;", "sa", "");
OlapConnection olapConn = conn.unwrap(OlapConnection.class);

With this result :

Exception in thread "main" java.lang.AbstractMethodError: org.h2.jdbc.JdbcConnection.unwrap(Ljava/lang/Class;)Ljava/lang/Object; at myprogram.main(Mondrian2H2Test.java:46)

:46 is OlapConnection olapConn = conn.unwrap(OlapConnection.class);


Solution

  • Looking at the MondrianOlap4jDriver javadocs and at the mailing list, the code seems to be:

    import java.sql.Connection;
    import java.sql.DriverManager;
    import org.olap4j.OlapConnection;
    
    Class.forName("mondrian.olap4j.MondrianOlap4jDriver");
    Connection conn = DriverManager.getConnection(
        "jdbc:mondrian:" + 
        "Jdbc=jdbc:h2:~/test; " +
        "JdbcUser=sa; " +
        "JdbcPassword=; " +
        "Catalog=file:/mondrian/demo/FoodMart.xml; " +
        "JdbcDrivers=org.h2.Driver");
    OlapConnection olapConn = conn.unwrap(OlapConnection.class);