简体   繁体   English

JDBC + postgres 连接问题

[英]JDBC + postgres connection problems

I've a problem when i'm connecting to a postgresql database with jdbc. I've install postgresql 9.1 from a package download on the pgAdmin3 site (i needed gui).当我使用 jdbc 连接到 postgresql 数据库时出现问题。我从 pgAdmin3 站点上的 package 下载安装了 postgresql 9.1(我需要 gui)。 I'm connecting to db whith pgAdmin with no problems, but when i try to connect from java code, i've the sequent error:我连接到 db whith pgAdmin 没有问题,但是当我尝试从 java 代码连接时,我遇到了后续错误:

org.postggresql.util.PSQLException: FATAL: password authentication failed for user postgres org.postggresql.util.PSQLException:致命:用户 postgres 的密码验证失败

the code that throws the exception is抛出异常的代码是

public class ConnectionManager {
   private ConnectionManager(){};
   private static boolean driverLoad = false;
   private static final String pgDriver="org.postgresql.Driver";
   private static final String pgUrl="jdbc:postgresql:coffeeDB";
   private static final String usr="postgres";
   private static final String psw="password";

   public static Connection getConnection() throws ClassNotFoundException, SQLException {
       if(!driverLoad) {
           Class.forName(pgDriver);
           driverLoad=true;
       }
       return DriverManager.getConnection(pgUrl, usr, psw);
   }
}

I've solved.我解决了Just add the default port on the URL of postgresql postgresql的URL上添加默认端口即可

I was getting this in Play 2.2.我在 Play 2.2 中得到了这个。 A slightly different flavor略有不同的味道

[error]c.j.b.h.AbstractConnectionHook - Failed to obtain initial connection Sleeping for 0ms and trying again. Attempts left: 0. Exception: null.Message:FATAL: password authentication failed for user "root"

Issue was quotes around the url in application.conf.问题是 application.conf 中 url 周围的引号。 If there are special characters, quotes are needed如果有特殊字符,需要引号

Fails失败

db.default.url=jdbc:postgresql://myMachine:5432/mydb

Works作品

db.default.url="jdbc:postgresql://myMachine:5432/mydb"

Same goes for password密码也一样

Fails失败

Passw)rd#!

Works作品

"Passw)rd#!"

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

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