简体   繁体   English

找不到合适的驱动程序(jdbc:pgsql)

[英]No suitable driver found (jdbc:pgsql)

(using com.impossibl.postgres.api.jdbc) (使用com.impossibl.postgres.api.jdbc)

I'm having issues establishing a connection to a database when I reference something from the hosts file. 当我从hosts文件中引用某些内容时,在建立与数据库的连接时遇到问题。

It DOES seem to work with 'localhost' though. 它似乎确实可以与“ localhost”一起使用。

Class.forName("com.impossibl.postgres.jdbc.PGDriver");
String url = "jdbc:pgsql://localhost:5432/db";
Connection conn = DriverManager.getConnection(url, "name", "pass");

I have this running inside of a docker container (on the same host machine as a postgres container). 我有这个在docker容器中运行(与postgres容器在同一主机上)。 When I use the hosts IP address or the ip of the postgres container it works fine, but whenever I try to use the container name it gives me 当我使用主机IP地址或postgres容器的ip时,它可以正常工作,但是每当我尝试使用容器名称时,它都会给我

java.sql.SQLException: No suitable driver found for jdbc:pgsql://postgres_container:5432/db

The container was linked to the postgres container when it was run, and the record is in the container's hosts file just as it should be. 容器在运行时已链接到postgres容器,并且该记录正好在该容器的hosts文件中。

Is this driver unable to reference the hosts file? 该驱动程序是否无法引用主机文件? If so, why does it work with localhost? 如果是这样,为什么它可以与localhost一起使用?

If it works with localhost, but not with a different hostname, then it sounds like a bug in that driver as it seems to be incorrectly rejecting the URL by returning null (which to DriverManager means, "this driver doesn't understand this URL") instead of accepting the URL and throwing an SQLException (which to DriverManager means, "this driver understands this URL, but something is wrong" (eg syntax error, problem connecting, etc). 如果它适用于本地主机,但不能使用不同的主机名,那么这听起来像是该驱动程序中的错误,因为它似乎通过返回null来错误地拒绝了URL(这对DriverManager表示“此驱动程序无法理解该URL”) ),而不是接受URL并引发SQLException (对于DriverManager ,这意味着“此驱动程序可以理解此URL,但是出了点问题”)(例如语法错误,连接问题等)。

If a JDBC driver accepts a specific URL format, it should be accepting it under all circumstances, from JDBC 4.2 section 9.2: 如果JDBC驱动程序接受特定的URL格式,则在所有情况下都应从JDBC 4.2第9.2节接受它:

When the DriverManager is trying to establish a connection, it calls that driver's connect method and passes the driver the URL. DriverManager尝试建立连接时,它将调用该驱动程序的connect方法并将该URL传递给该驱动程序。 If the Driver implementation understands the URL, it will return a Connection object or throw a SQLException if a connection cannot be maded to the database. 如果驱动程序实现能够理解URL,则如果无法建立与数据库的Connection ,它将返回Connection对象或抛出SQLException If the Driver implementation does not understand the URL, it will return null . 如果Driver实现不理解该URL,它将返回null

Specifically looking at the code , it looks like the URL parser in pgjdbc-ng doesn't like underscores in host names and therefor returns null , which then causes null to be returned from the Driver instead of throwing an SQLException . 具体看代码 ,它看起来像在pgjdbc-NG的URL解析器不喜欢在主机名下划线并为此返回null ,然后使null从返回的Driver抛出来代替SQLException

private static final Pattern URL_PATTERN =
    Pattern.compile("jdbc:pgsql:(?://((?:[a-zA-Z0-9\\-\\.]+|\\[[0-9a-f\\:]+\\])(?:\\:(?:\\d+))?(?:,(?:[a-zA-Z0-9\\-\\.]+|\\[[0-9a-f\\:]+\\])(?:\\:(?:\\d+))?)*)/)?((?:\\w|-|_)+)(?:[\\?\\&](.*))?");

Workaround: use a hostname without underscores, or an IP address. 解决方法:使用不带下划线的主机名或IP地址。

To get the actual issues addressed, I suggest you create an issue on https://github.com/impossibl/pgjdbc-ng 为了解决实际问题,建议您在https://github.com/impossibl/pgjdbc-ng上创建一个问题

This sounds to me like an environment issue related to the docker container configuration. 在我看来,这听起来像是与Docker容器配置相关的环境问题。 The "docker" user classpath may be missing the driver directory or the docker container environment does not reference the directory or driver. “ docker”用户类路径可能缺少驱动程序目录,或者docker容器环境未引用该目录或驱动程序。

I would double check all the settings as listed in https://docs.docker.com/engine/examples/postgresql_service/ and run command line tests, etc. until you track it down. 我将仔细检查https://docs.docker.com/engine/examples/postgresql_service/中列出的所有设置,并运行命令行测试等,直到您对其进行跟踪。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM