配置DriverManagerDataSource时,nested exception is java.sql.SQLException: Access denied for user

Weird

Failed to obtain JDBC Connection; nested exception is java.sql.SQLException: Access denied for user ‘Lin’@‘localhost’ (using password: YES)

General situation

  1. Generally it is a permission problem (the solution below is others)

https://blog.csdn.net/chuyouyinghe/article/details/79225972

Strange situation

  • I used DriverManagerDataSource in spring-jdbc to get the data source

The following is the configuration file of jdbc:

driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/student
username=root
password=root

The following is the configuration of the data source in spring:

<!--配置dataSource-->
    <bean id="ds" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${driverClassName}"></property>
        <property name="url" value="${url}"></property>
        <property name="username" value="${username}"></property>
        <property name="password" value="${password}"></property>
    </bean>

My test code:
Insert picture description here
running results:
Insert picture description here

Find the reason

I use the new method to create objects, instead of spring Ioc.
Insert picture description here
This will run without problems.

result

Just change the ${username} in the configuration file to another name.
Insert picture description here
Once you change it back to username, an error will be reported. After checking it for a long time, I don't know why, but the problem is solved. Does anyone know who can give me some advice?

Guess you like

Origin blog.csdn.net/weixin_44736584/article/details/106455147