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

错误日志

错误代码

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

 

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值