问题
d a t a b a s e . p r o p e r t i e s database.properties database.properties
driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/mydatabase?serverTimezone=GMT&useSSL=false&allowPublicKeyRetrieval=true
username=root
password=123123
s p r i n g − d a o . x m l spring-dao.xml spring−dao.xml
<context:property-placeholder location="classpath:database.properties"/>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${driver}"/>
<property name="jdbcUrl" value="${url}"/>
<property name="user" value="${username}"/>
<property name="password" value="${password}"/>
</bean>
在使用 Spring 配置文件用 c3p0 连接池连接数据库时,出现了 Access denied for use ‘root’@‘localhost’ (using password: YES) 的问题。
解决方法
数据库用户名不能使用 u s e r n a m e username username ,用其他名字即可。
如下所示:
d
a
t
a
b
a
s
e
.
p
r
o
p
e
r
t
i
e
s
database.properties
database.properties
driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/mydatabase?serverTimezone=GMT&useSSL=false&allowPublicKeyRetrieval=true
user=root
password=123123
s p r i n g − d a o . x m l spring-dao.xml spring−dao.xml
<context:property-placeholder location="classpath:database.properties"/>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${driver}"/>
<property name="jdbcUrl" value="${url}"/>
<property name="user" value="${user}"/>
<property name="password" value="${password}"/>
</bean>