简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM