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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With