前言
使用Spring配置数据源,当加载druid.properties文件进行数据库连接时,容易碰到下面的报错:
create connection SQLException, url: jdbc:mysql://127.0.0.1:3306/mydbs, errorCode 1045, state 28000
java.sql.SQLException: Access denied for user 'ä¸ å® '@'localhost' (using password: YES)
问题分析
问题出在配置文件里,原因如下:
在配置数据源时,通常会使用 druid.properties 这个外部文件,但在该文件中,用户名的键名不能是username,因为spring在加载时会把username当作你的计算机名。
解决方法
下面是debug前后两种写法的对比:
报错的写法
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/furn_ssm?useSSL=true&useUnicode=true&characterEncoding=UTF-8
username=root
pwd=12345
正确的写法
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/furn_ssm?useSSL=true&useUnicode=true&characterEncoding=UTF-8
user=root
pwd=12345