加载Postgresql JDBC 4.1驱动程序 [英] Loading the Postgresql JDBC 4.1 driver

查看:127
本文介绍了加载Postgresql JDBC 4.1驱动程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用JDBC 4.1驱动程序连接到PostgreSQL数据库.我在pom.xml中声明了以下依赖项:

I want to connect to a PostgreSQL database using the JDBC 4.1 driver. I declared the following dependency in the pom.xml:

<dependency>
    <groupId>org.postgresql</groupId>
    <artifactId>postgresql</artifactId>
    <version>9.3-1101-jdbc41</version>
</dependency>

这会将postgresql-9.3-1101-jdbc41.jar放入我的类路径中.我已经读到,如果在META-INF/services/java.sql.Driver中指定了驱动程序,则不再需要使用Class.forName()加载驱动程序. driver-jar有此文件,但仍然出现以下错误:

This puts postgresql-9.3-1101-jdbc41.jar into my classpath. I have read that it not necessary anymore to load the driver using Class.forName() if the driver is specified at META-INF/services/java.sql.Driver. The driver-jar has this file but I still get the following error:

No suitable driver found for jdbc:postgresql://localhost:5432

我只是在测试中打电话给,然后使用mvn test运行测试:

I just make a call to in a test and then run the tests using mvn test:

DriverManager.getConnection("jdbc:postgresql://localhost:5432");

即使我在收到错误之前致电了Class.forName().如何正确加载驱动程序?

Even if I call Class.forName() before I get the error. How do I get the driver loaded properly?

推荐答案

您的连接URL无效.您必须指定数据库,否则驱动程序将不接受URL.

Your connection URL is invalid. You must specify a database, otherwise the driver will not accept the URL.

正确的URL将是:

jdbc:postgresql://localhost:5432/database_name

或者使用默认端口:

jdbc:postgresql://localhost/database_name

消息"找不到合适的驱动程序"是由DriverManager生成的消息,询问所有已注册的驱动程序是否接受给定的URL.然后,第一个说是"的驱动程序将用于打开连接. Postgres驱动程序不接受该URL,因为缺少数据库,并且由于没有其他驱动程序在附近询问,因此DriverManager放弃"并向您显示该错误消息.

The message "No suitable driver found" is a message generated by the DriverManager which asks all registered drivers if they accept the given URL. The first driver to say "yes" will then be used to open the connection. The Postgres driver doesn't accept the URL because the database is missing and as no other drivers are around to ask, the DriverManager "gives up" and presents you with that error message.

如果该课程不可用,您会从DriverManager那里得到一个ClassNotFoundException而不是消息".

If the class wasn't available you'd get a ClassNotFoundException not a "message" from the DriverManager.

这篇关于加载Postgresql JDBC 4.1驱动程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆