1

The problem

I'm currently working with Java 9 and its module system and want to connect to my PostgreSQL database via JDBC.

The problem is that I could not find any information on its setup with Java 9 and it's module system but only for Java 8 and older.

The question

How can I properly setup JDBC and its driver using the java module system?

4

1 回答 1

3

jar 必须在运行时位于类路径中。对于编译,您不需要 jar。

如果 Postgresql 驱动程序已经模块化,它将与uses/provides运行时机制一起工作:

Java JRE:

module java.sql {
   uses java.sql.Driver;
   exports java.sql;
}

然后驱动程序在模块信息中应该有这样的东西:

module org.postgresql {
   requires java.sql;
   provides java.sql.Driver with org.postgresql.Driver;
}

而普通的 ServiceLoader 会自动发现驱动程序。

Class.forName("org.postgresql.Driver");通常是不需要的,只是在一些 JavaEE 应用程序中有一些类加载器杂耍。

于 2019-11-06T12:43:27.503 回答