首页 文章

如何使用配置文件连接到Spring中的数据库?

提问于
浏览
0

我尝试使用Spring MVC连接到mysql数据库,但我不知道为什么数据源属性没有保存 . 它给了我一些例外,例如验证失败,但我已经在属性文件中保存了连接细节 .

这是web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            classpath:main/java/containers/dao-context.xml
            classpath:main/java/containers/service-context.xml
        </param-value>
    </context-param>


    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>


    <resource-ref>
        <description>MySQL Datasource example</description>
        <res-ref-name>jdbc/DataSource</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
    </resource-ref>

</web-app>

这是我的帐户的DAO上下文,其中包括具有配置属性的数据源bean:

<?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">

    <bean id="dataSource" class="org.apache.tomcat.dbcp.dbcp2.BasicDataSource">
        <property name="username" value="${username}" />
        <property name="password" value="${password}" />
        <property name="url" value="${url}" />
        <property name="driverClassName" value="${driver}" />
    </bean>

    <context:property-placeholder location="classpath:configuration.properties" />
    <context:component-scan base-package="business.users_management" />
    <context:annotation-config />
</beans>

这是配置文件:

username = raoul
password = cefere
driver = com.mysql.jdbc.Driver
url = jdbc:mysql://localhost:3306/gamedatabase

最后是AccountsDAO类:

@Component("accounts")
public class AccountsDAO implements UsersCRUD {
    private List<Account> accounts;
    private JdbcTemplate jdbc;


    @Autowired
    public void setJdbc(DataSource dataSource) {
        this.jdbc = new JdbcTemplate(dataSource);
    }

    @Override
    public boolean addAccount(Account account) {
        BeanPropertySqlParameterSource params = new BeanPropertySqlParameterSource(account);
        return jdbc.update("INSERT into accounts VALUES (:username, :password,, :email, :name)", params) == 1;
    }

    @Override
    public List<Account> getAllAccounts() {
        return jdbc.query("SELECT * FROM accounts", (resultSet, i) -> {
            return createAccount(resultSet);
        });
    }

}

这些是错误:似乎连接到数据库的用户名和密码被mysql设置为默认值...

INFO: Loading properties file from class path resource [configuration.properties]
Exception in thread "main" org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is java.sql.SQLException: Cannot create PoolableConnectionFactory (Access denied for user 'Gustavo'@'localhost' (using password: YES))
    at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:80)
    at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:390)
    at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:470)
    at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:480)
    at business.users_management.AccountsDAO.getAllAccounts(AccountsDAO.java:37)
    at business.Main.main(Main.java:19)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
Caused by: java.sql.SQLException: Cannot create PoolableConnectionFactory (Access denied for user 'Gustavo'@'localhost' (using password: YES))
    at org.apache.tomcat.dbcp.dbcp2.BasicDataSource.createPoolableConnectionFactory(BasicDataSource.java:2151)
    at org.apache.tomcat.dbcp.dbcp2.BasicDataSource.createDataSource(BasicDataSource.java:1902)
    at org.apache.tomcat.dbcp.dbcp2.BasicDataSource.getConnection(BasicDataSource.java:1412)
    at org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:111)
    at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:77)
    ... 10 more
Caused by: java.sql.SQLException: Access denied for user 'Gustavo'@'localhost' (using password: YES)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:996)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3887)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3823)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:870)
    at com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(MysqlIO.java:1659)
    at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1206)

1 回答

  • 0

    请将实际值放入配置中并尝试jdbc:mysql:// localhost / db?useUnicode = true&useJDBCCompliantTimezoneShift = true&useLegacyDatetimeCode = false&serverTimezone = UTC尝试添加此项

相关问题