Ich habe versucht, eine Verbindung zu einer MySQL-Datenbank mithilfe von Spring MVC herzustellen, aber ich weiß nicht, warum die Datenquelleneigenschaften nicht gespeichert werden. Es gibt mir eine Ausnahme wie Authentifizierung fehlgeschlagen, aber ich habe die Verbindungsdetails in einer Eigenschaftendatei gespeichert.Wie kann ich eine Verbindung zu einer Datenbank in Spring mithilfe von Konfigurationsdateien herstellen?
Dies ist der 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>
Dies ist für meine Konten DAO Kontext, die mit den Konfigurationseigenschaften die Datenquelle Bohne enthält:
<?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>
dies ist die Konfigurationsdatei:
username = raoul
password = cefere
driver = com.mysql.jdbc.Driver
url = jdbc:mysql://localhost:3306/gamedatabase
Und schließlich die AccountsDAO-Klasse:
@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);
});
}
}
Dies sind die Fehler: Es ist wie der Benutzername und das Passwort scheint in die Datenbank für den Anschluss von mysql auf Standard gesetzt ...
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)
können Sie mit harten versuchen, die Werte Codierung und sehen Sie hinzufügen, wenn Sie immer noch den Fehler ? – LearningPhase
Anstatt die Eigenschaftsdatei zu lesen, setzen Sie die tatsächlichen Werte – LearningPhase
Nun bekomme ich diesen Fehler .... Ich benutze mysql: mysql-connector-java: 5.1.38 Der Server Zeitzone Wert 'GTB Daylight Time' ist nicht erkannt oder repräsentiert mehr als eine Zeitzone. Sie müssen entweder den Server oder den JDBC-Treiber konfigurieren (über die Konfigurationseigenschaft serverTimezone), um einen spezifischeren Zeitzonenwert zu verwenden, wenn Sie die Zeitzonenunterstützung verwenden möchten.) –