![](/img/trans.png)
[英]How do you add a local .jar dependency in Netbeans 11 with Maven?
[英]How do you add PostgreSQL Driver as a dependency in Maven?
我正在嘗試使用 Maven 開發 Java 應用程序,同時將 Hibernate 與 PostgreSQL 數據庫一起使用以實現持久性。 我不明白應該如何將 PostgreSQL 驅動程序連接到我的應用程序。 我知道您在 Maven 的 pom.xml 文件中添加了依賴項,該文件從遠程存儲庫中查找 jar,但是其他 jar 呢?
PostgreSQL 驅動程序 jar 包含在 Maven 的中央存儲庫中:
對於高達 9.1 的 PostgreSQL,請使用:
<dependency>
<groupId>postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>VERSION</version>
</dependency>
或 9.2+
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>VERSION</version>
</dependency>
(感謝@Caspar 指正)
更新最新版本:
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.14</version>
</dependency>
希望能幫助到你!
根據您的 PostgreSQL 版本,您需要將 postgresql 驅動程序添加到您的pom.xml
文件中。
對於 PostgreSQL 9.1,這將是:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<name>Your project name.</name>
<dependencies>
<dependency>
<groupId>postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.1-901-1.jdbc4</version>
</dependency>
</dependencies>
</project>
您可以從 Maven 的中央存儲庫獲取依賴項(以及任何其他依賴項)的代碼
如果您使用的是 postgresql 9.2+:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<name>Your project name.</name>
<dependencies>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.1</version>
</dependency>
</dependencies>
</project>
您可以從以下位置檢查最新版本和依賴項片段:
來自 PostgreSQL 站點,日期為 02/04/2016 ( https://jdbc.postgresql.org/download.html ):
“這是驅動程序的當前版本。除非您有特殊要求(運行舊的應用程序或 JVM),否則這是您應該使用的驅動程序。它支持 Postgresql 7.2 或更新版本,需要 1.6 或更新的 JVM。它包含對SSL 和 javax.sql 包。如果你使用的是 1.6 那么你應該使用 JDBC4 版本。如果你使用的是 1.7 那么你應該使用 JDBC41 版本。如果你使用的是 1.8 那么你應該使用 JDBC42 版本如果你正在使用Java 版本早於 1.6,那么您將需要使用驅動程序的 JDBC3 版本,該版本必然不是最新的”
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.