1. 程式人生 > >Caused by: java.sql.SQLException: Access denied for user 'dell-pc'@'xxxxx' (using password: YES)

Caused by: java.sql.SQLException: Access denied for user 'dell-pc'@'xxxxx' (using password: YES)

今天遇到一個非常坑的異常。

在做SSM專案整合的時候,遇到了上面這個異常,我一看連線資料庫的username 怎麼變成了 dell-pc,然而jdbc.properties

username=root
password=123456

spring配置為:

<?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:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:property-placeholder location="classpath:jdbc.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}"/> <property name="maxPoolSize" value="30"/> <property name="minPoolSize" value="10"/> <property name="autoCommitOnClose" value="false"/> <property name="checkoutTimeout" value="0"/> <property name="acquireRetryAttempts" value="2"/> </bean>

其中<property name="user" value="${username}"/> 在設定的時候也能自動顯示username的值為root,一直報異常真頭疼。

後來我發現了兩種解決方案

第一種:直接把username通通改成name

第二種:<context:property-placeholder location="classpath:jdbc.properties"/> 後新增屬性system-properties-mode=”FALLBACK”:<context:property-placeholder location="classpath:jdbc.properties" system-properties-mode="FALLBACK"/>
系統預設為system-properties-mode=”ENVIRONMENT” 意思就是從系統環境中去讀取,把電腦名當做mysql的使用者名稱,修改之後,執行成功。