关于数据库连接出错问题的解决方法(mysql、postgresql)

今天在进行spring与postgresql的连接时发现了一个错误,错误信息如下:

Exception in thread "main" java.sql.SQLException: Cannot create PoolableConnectionFactory (FATAL: password authentication failed for user "Rick")
 at org.apache.commons.dbcp2.BasicDataSource.createPoolableConnectionFactory(BasicDataSource.java:2152)
 at org.apache.commons.dbcp2.BasicDataSource.createDataSource(BasicDataSource.java:1903)
 at org.apache.commons.dbcp2.BasicDataSource.getConnection(BasicDataSource.java:1413)
 at cn.yourside.community.web.test.BeanTest.main(BeanTest.java:16)
Caused by: org.postgresql.util.PSQLException: FATAL: password authentication failed for user "Rick"
 at org.postgresql.core.v3.ConnectionFactoryImpl.doAuthentication(ConnectionFactoryImpl.java:408)
 at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:181)
 at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:64)
 at org.postgresql.jdbc2.AbstractJdbc2Connection.<init>(AbstractJdbc2Connection.java:144)
 at org.postgresql.jdbc3.AbstractJdbc3Connection.<init>(AbstractJdbc3Connection.java:29)
 at org.postgresql.jdbc3g.AbstractJdbc3gConnection.<init>(AbstractJdbc3gConnection.java:21)
 at org.postgresql.jdbc4.AbstractJdbc4Connection.<init>(AbstractJdbc4Connection.java:31)
 at org.postgresql.jdbc4.Jdbc4Connection.<init>(Jdbc4Connection.java:24)
 at org.postgresql.Driver.makeConnection(Driver.java:410)
 at org.postgresql.Driver.connect(Driver.java:280)
 at org.apache.commons.dbcp2.DriverConnectionFactory.createConnection(DriverConnectionFactory.java:39)
 at org.apache.commons.dbcp2.PoolableConnectionFactory.makeObject(PoolableConnectionFactory.java:205)
 at org.apache.commons.dbcp2.BasicDataSource.validateConnectionFactory(BasicDataSource.java:2162)
 at org.apache.commons.dbcp2.BasicDataSource.createPoolableConnectionFactory(BasicDataSource.java:2148)
 ... 3 more

其中Rick是我的PC的用户名,感觉很奇怪,为什么我配置的用户名明明是postgres,在这里却变成了Rick。我修改配置文件件连接mysql数据库,发现报如下错误信息:

Exception in thread "main" java.sql.SQLException: Cannot create PoolableConnectionFactory (Access denied for user 'Rick'@'localhost' (using password: YES))
 at org.apache.commons.dbcp2.BasicDataSource.createPoolableConnectionFactory(BasicDataSource.java:2152)
 at org.apache.commons.dbcp2.BasicDataSource.createDataSource(BasicDataSource.java:1903)
 at org.apache.commons.dbcp2.BasicDataSource.getConnection(BasicDataSource.java:1413)
 at cn.yourside.community.web.test.BeanTest.main(BeanTest.java:16)
Caused by: java.sql.SQLException: Access denied for user 'Rick'@'localhost' (using password: YES)
 at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1084)
 at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4232)
 at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4164)
 at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:926)
 at com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(MysqlIO.java:1748)
 at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1288)
 at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2506)
 at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2539)
 at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2321)
 at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:832)
 at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:46)
 at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
 at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
 at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
 at java.lang.reflect.Constructor.newInstance(Unknown Source)
 at com.mysql.jdbc.Util.handleNewInstance(Util.java:409)
 at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:417)
 at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:344)
 at org.apache.commons.dbcp2.DriverConnectionFactory.createConnection(DriverConnectionFactory.java:39)
 at org.apache.commons.dbcp2.PoolableConnectionFactory.makeObject(PoolableConnectionFactory.java:205)
 at org.apache.commons.dbcp2.BasicDataSource.validateConnectionFactory(BasicDataSource.java:2162)
 at org.apache.commons.dbcp2.BasicDataSource.createPoolableConnectionFactory(BasicDataSource.java:2148)
 ... 3 more

看完之后突然想起来之前好像犯过这个错误,仔细一想,之前是因为在一个类里面存在两个username,觉得是username重复被覆盖造成的,所以我将username换成databaseUserName,替换之后就成功了,这个问题貌似和spring有关系,在它的默认的applicationContext中有username的默认值,与自定义的值造成混淆。


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值