加载Postgresql JDBC 4.1驱动程序

编程入门 行业动态 更新时间:2024-10-27 03:27:11
本文介绍了加载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驱动程序

本文发布于:2023-10-28 23:50:43,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1538124.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:驱动程序   加载   JDBC   Postgresql

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!