create connection SQLException errorCode 0, state 20000,Access denied for user ‘xxxxx’@‘localhost’
运行一个SSM框架搭建的项目,连接时报错
create connection SQLException, url: jdbc:mysql://localhost:3306/zzz?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8, errorCode 0, state 08001
Access denied for user 'xxxxx'@'localhost'
很奇怪,xxxxx显示的计算机的用户名,按道理应该显示root的,而db.properties下配置的是
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/zzz
username=root
password=root
而applicationContext.xml文件的dataSource的bean配置如下:
<!-- 配置datasource -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" >
<property name="driverClassName" value="${driverClassName}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</bean>
问题解决:
方案一:
applicationContext.xml文件改成:
<!-- 配置datasource -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" >
<property name="driverClassName" value="${driverClassName}"/>
<property name="url" value="${url}"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
即将${username}改成数据库用户名,${password}改成数据库密码
方案二:
将db.properties改为:
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/zzz
jdbc.username=root
jdbc.password=root
再将applicationContext.xml文件改成:
<!-- 配置datasource -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" >
<property name="driverClassName" value="${driverClassName}"/>
<property name="url" value="${url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
问题解决!分享一下!喜欢的可以关注一下!