Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Derby driver not found in Java 9 module

I'm trying to get a Java 9 module to connect to the inbuilt Derby database (embed mode). My module has a dependency on java.sql, but I'm not sure where to put the Derby driver.

Here's my module-info.java:

module mymodule {
    requires java.sql;
}

Here's the Main.java code in the module that fails to execute:

public static void main(String[] args) {

...
    Connection conn;
    try {
        conn = DriverManager.getConnection("jdbc:derby:mysampledb");
        Statement s = conn.createStatement();
        s.executeUpdate("create table testtable (" +
            "id VARCHAR(256), " +
            "value VARCHAR(256)) ");
        s.close();

    }
    catch (SQLException e) {
        System.out.println("Error connecting " + e);
    }
}

This compiles fine, but does not execute.

java -cp derbyjars --module-path out -m mymodule/foo.Main

Error connecting java.sql.SQLException: No suitable driver found for jdbc:derby:mysampledb

How do I add the DB drivers? Is that using the -cp option? I have placed all the derby jars in the folder derbyjars and I'm passing that to the -cp option. Is there another way to make the module see the drivers?

like image 973
halfinfinities Avatar asked Oct 17 '22 15:10

halfinfinities


1 Answers

The -cp parameter takes in the individual jar names. Not the name of the folder containing the jars. (Thanks @BryanPendleton).

I changed my command to the one below, and it worked:

java -cp "derbyjars/*" --module-path out -m mymodule/foo.Main
like image 125
halfinfinities Avatar answered Oct 21 '22 08:10

halfinfinities