java - How can I connect to a database in Spring using configuration files? -
i tried connect mysql database using spring mvc don't know why datasource properties not saved. gives me exception authentification failed i've saved connection details in properties file.
this 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>
this dao context accounts includes datasource bean configuration properties:
<?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>
this configuration file:
username = raoul password = cefere driver = com.mysql.jdbc.driver url = jdbc:mysql://localhost:3306/gamedatabase
and lastly accountsdao class :
@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 accounts values (:username, :password,, :email, :name)", params) == 1; } @override public list<account> getallaccounts() { return jdbc.query("select * accounts", (resultset, i) -> { return createaccount(resultset); }); } }
these errors : seems username , password connecting database set default mysql ...
info: loading properties file class path resource [configuration.properties] exception in thread "main" org.springframework.jdbc.cannotgetjdbcconnectionexception: not jdbc connection; nested exception java.sql.sqlexception: cannot create poolableconnectionfactory (access denied user 'gustavo'@'localhost' (using password: yes)) @ org.springframework.jdbc.datasource.datasourceutils.getconnection(datasourceutils.java:80) @ org.springframework.jdbc.core.jdbctemplate.execute(jdbctemplate.java:390) @ org.springframework.jdbc.core.jdbctemplate.query(jdbctemplate.java:470) @ org.springframework.jdbc.core.jdbctemplate.query(jdbctemplate.java:480) @ business.users_management.accountsdao.getallaccounts(accountsdao.java:37) @ business.main.main(main.java:19) @ sun.reflect.nativemethodaccessorimpl.invoke0(native method) @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:62) @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43) @ java.lang.reflect.method.invoke(method.java:498) @ com.intellij.rt.execution.application.appmain.main(appmain.java:144) caused by: java.sql.sqlexception: cannot create poolableconnectionfactory (access denied user 'gustavo'@'localhost' (using password: yes)) @ org.apache.tomcat.dbcp.dbcp2.basicdatasource.createpoolableconnectionfactory(basicdatasource.java:2151) @ org.apache.tomcat.dbcp.dbcp2.basicdatasource.createdatasource(basicdatasource.java:1902) @ org.apache.tomcat.dbcp.dbcp2.basicdatasource.getconnection(basicdatasource.java:1412) @ org.springframework.jdbc.datasource.datasourceutils.dogetconnection(datasourceutils.java:111) @ org.springframework.jdbc.datasource.datasourceutils.getconnection(datasourceutils.java:77) ... 10 more caused by: java.sql.sqlexception: access denied user 'gustavo'@'localhost' (using password: yes) @ com.mysql.jdbc.sqlerror.createsqlexception(sqlerror.java:996) @ com.mysql.jdbc.mysqlio.checkerrorpacket(mysqlio.java:3887) @ com.mysql.jdbc.mysqlio.checkerrorpacket(mysqlio.java:3823) @ com.mysql.jdbc.mysqlio.checkerrorpacket(mysqlio.java:870) @ com.mysql.jdbc.mysqlio.proceedhandshakewithpluggableauthentication(mysqlio.java:1659) @ com.mysql.jdbc.mysqlio.dohandshake(mysqlio.java:1206)
please put actual values in config , try jdbc:mysql://localhost/db?useunicode=true&usejdbccomplianttimezoneshift=true&uselegacydatetimecode=false&servertimezone=utc try add this
Comments
Post a Comment