SSM报错java.sql.SQLException: Access denied for user 'Administrator'@'localhost' (using password: YES)

孤街醉人 提交于 2020-01-22 06:59:34

错误日志

错误代码

applicationContext.xml(只有数据库连接那部分代码):

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <!--引入外部配置文件(properties等文件-->
    <context:property-placeholder location="classpath:jdbc.properties"/>

    <!-- 配置数据源 -->
    <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
        <!--数据库连接驱动-->
        <property name="driverClassName" value="${driver}"/>
        <!--数据库连接URL-->
        <property name="url" value="${url}"/>
        <!--数据库登录用户名-->
        <property name="username" value="${username}"/>
        <!--数据库登录密码-->
        <property name="password" value="${password}"/>
        <!-- 初始化连接数 -->
        <property name="initialSize" value="${initialSize}"/>
        <!-- 最大连接数 -->
        <property name="maxTotal" value="${maxActive}"/>
        <!-- 最大空闲连接数 -->
        <property name="maxIdle" value="${maxIdle}"/>
    </bean>

</beans>

jdbc.properties

# 数据库驱动
driver=com.mysql.jdbc.Driver
# 连接URL
url=jdbc:mysql://localhost:3306/db_springmvc_demo?characterEncoding=utf8&useSSL=false
# 数据库的用户名
username=root
# 数据库的密码
password=admin
# 定义初始连接数
initialSize=5
# 定义最大连接数
maxActive=20
# 定义最大空闲
maxIdle=20
# 定义最小空闲
minIdle=1
# 定义最长等待时间
maxWait=60000

原因

可能是username这个变量名和某个系统定义的冲突了。

解决

将username替换成其他的如user等。

正确代码

applicationContext.xml(只有数据库连接那部分代码):

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <!--引入外部配置文件(properties等文件-->
    <context:property-placeholder location="classpath:jdbc.properties"/>

    <!-- 配置数据源 -->
    <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
        <!--数据库连接驱动-->
        <property name="driverClassName" value="${driver}"/>
        <!--数据库连接URL-->
        <property name="url" value="${url}"/>
        <!--数据库登录用户名-->
        <property name="username" value="${user}"/>
        <!--数据库登录密码-->
        <property name="password" value="${password}"/>
        <!-- 初始化连接数 -->
        <property name="initialSize" value="${initialSize}"/>
        <!-- 最大连接数 -->
        <property name="maxTotal" value="${maxActive}"/>
        <!-- 最大空闲连接数 -->
        <property name="maxIdle" value="${maxIdle}"/>
    </bean>

</beans>

jdbc.properties

# 数据库驱动
driver=com.mysql.jdbc.Driver
# 连接URL
url=jdbc:mysql://localhost:3306/db_springmvc_demo?characterEncoding=utf8&useSSL=false
# 数据库的用户名
user=root
# 数据库的密码
password=admin
# 定义初始连接数
initialSize=5
# 定义最大连接数
maxActive=20
# 定义最大空闲
maxIdle=20
# 定义最小空闲
minIdle=1
# 定义最长等待时间
maxWait=60000

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!