5
respostas

Problema ao passar a usar a conexao com banco via arquivo de propriedades

Prezados,

Minha aplicacao estava rodando certo ate eu passar as minhas configuracoes para o arquivo application.properties, ai passaram a dar as seguintes mensagens erro:

2017-10-15 15:44:21.097  INFO 12236 --- [  restartedMain] br.com.alura.listavip.Configuracao       : Starting Configuracao on Renatos-MacBook-Pro-19137.local with PID 12236 (/Users/renatoramos/Documents/workspaceWeb/listavip/target/classes started by renatoramos in /Users/renatoramos/Documents/workspaceWeb/listavip)
2017-10-15 15:44:21.098  INFO 12236 --- [  restartedMain] br.com.alura.listavip.Configuracao       : No active profile set, falling back to default profiles: default
2017-10-15 15:44:21.353  INFO 12236 --- [  restartedMain] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@6caccb9: startup date [Sun Oct 15 15:44:21 BRST 2017]; root of context hierarchy
2017-10-15 15:44:23.058  INFO 12236 --- [  restartedMain] org.eclipse.jetty.util.log               : Logging initialized @3077ms to org.eclipse.jetty.util.log.Slf4jLog
2017-10-15 15:44:23.188  INFO 12236 --- [  restartedMain] e.j.JettyEmbeddedServletContainerFactory : Server initialized with port: 9000
2017-10-15 15:44:23.190  INFO 12236 --- [  restartedMain] org.eclipse.jetty.server.Server          : jetty-9.4.5.v20170502
2017-10-15 15:44:23.298  INFO 12236 --- [  restartedMain] org.eclipse.jetty.server.session         : DefaultSessionIdManager workerName=node0
2017-10-15 15:44:23.298  INFO 12236 --- [  restartedMain] org.eclipse.jetty.server.session         : No SessionScavenger set, using defaults
2017-10-15 15:44:23.301  INFO 12236 --- [  restartedMain] org.eclipse.jetty.server.session         : Scavenging every 600000ms
2017-10-15 15:44:23.307  INFO 12236 --- [  restartedMain] o.e.j.s.h.ContextHandler.application     : Initializing Spring embedded WebApplicationContext
2017-10-15 15:44:23.308  INFO 12236 --- [  restartedMain] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1958 ms
2017-10-15 15:44:23.490  INFO 12236 --- [  restartedMain] o.s.b.w.servlet.ServletRegistrationBean  : Mapping servlet: 'dispatcherServlet' to [/]
2017-10-15 15:44:23.493  INFO 12236 --- [  restartedMain] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
2017-10-15 15:44:23.493  INFO 12236 --- [  restartedMain] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2017-10-15 15:44:23.493  INFO 12236 --- [  restartedMain] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2017-10-15 15:44:23.493  INFO 12236 --- [  restartedMain] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]
2017-10-15 15:44:23.800  INFO 12236 --- [  restartedMain] o.e.jetty.server.handler.ContextHandler  : Started o.s.b.c.e.j.JettyEmbeddedWebAppContext@644a0c70{/,[file:///private/var/folders/6j/5_yt6n012hb5jfqnp_ysjwlc0000gn/T/jetty-docbase.3924921936440654396.9000/],AVAILABLE}
2017-10-15 15:44:23.801  INFO 12236 --- [  restartedMain] org.eclipse.jetty.server.Server          : Started @3822ms
Sun Oct 15 15:44:24 BRST 2017 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
2017-10-15 15:44:24.422 ERROR 12236 --- [  restartedMain] o.a.tomcat.jdbc.pool.ConnectionPool      : Unable to create initial connections of pool.

java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:963) ~[mysql-connector-java-5.1.39.jar:5.1.39]
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3966) ~[mysql-connector-java-5.1.39.jar:5.1.39]
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3902) ~[mysql-connector-java-5.1.39.jar:5.1.39]
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:875) ~[mysql-connector-java-5.1.39.jar:5.1.39]
    at com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(MysqlIO.java:1712) ~[mysql-connector-java-5.1.39.jar:5.1.39]
    at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1228) ~[mysql-connector-java-5.1.39.jar:5.1.39]
    at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2253) ~[mysql-connector-java-5.1.39.jar:5.1.39]
    at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2284) ~[mysql-connector-java-5.1.39.jar:5.1.39]
    at com.mys
...
5 respostas

Esse e meu arquivo application.properties:

server.port=9000
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=root 
spring.datasource.url=jdbc:mysql://localhost:3306/listavip
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true

Eae Renato beleza?

Basicamente, esse erro indica que no seu application.properties você configurou de forma errada o username e password do seu banco de dados. Tem certeza q o username e o password estão certos? Normalmente, quando estamos em desenvolvimento um padrão de usuario/senha é : root e sem senha. Tem certeza que sua senha também é root?

Outra coisa, se eu não me engano vc n precisa colocar esse ":3306" pois é a porta padrão, acho que se vc deixar apenas localhost ele vai funcionar da mesma forma.

Espero que eu tenha conseguido te ajudar e bons estudos!

Fala ae M'ario,

Bem, quando eu executo minha aplicacao com as configuracoes no arquivo Configuracao.java, conforme feito no curso:

package br.com.alura.listavip;

import javax.sql.DataSource;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

/**
 * Classe usada para as configuracoes do Spring Boot
 * 
 * @author renatoramos
 *
 */
@SpringBootApplication
public class Configuracao {

    public static void main(String[] args) {
        SpringApplication.run(Configuracao.class, args);
    }

    @Bean
    public DataSource dataSource(){
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://localhost:3306/listavip");
        dataSource.setUsername("root");
        dataSource.setPassword("root");

        return dataSource;
    }
}

Porem, quando eu migro as configuracoes para o arquivo application.properties a aplicacao nao roda e da esse erro que postei, eis meu arquivo properties:

server.port=9000

# Banco de dados
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.url=jdbc:mysql://localhost:3306/listavip
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true

OBS - Eu apago o metodo dataSource() para testar as configuracoes do arquivo application.properties

Fiz tal qual o instrutor fez no video dele.

Att,

Renato Ramos

Olá Renato, Experimenta comentar a linha abaixo:

spring.jpa.hibernate.ddl-auto=update

E depois volte aqui dizendo se deu certo ou não, por favor.

Se der certo, provavelmente sua aplicação está tentando realizar algum comando DDL, por conta da linha acima no seu properties, e o seu usuário root não tem permissão para isso.

Caso seja isso, você também pode conceder os privilégios necessários ao seu usuário, em vez de comentar a linha cima.

Tive um problema parecido fiz as seguintes mudancas:

Adicionei no pom:

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
            <version>2.0.0.RELEASE</version>
        </dependency>

No configuracao:

@SpringBootApplication
@EnableJpaRepositories
public class Configuracao {

    public static void main(String[] args) {
        SpringApplication.run(Configuracao.class, args);
    }


}

Refazer os imports da classe convidados:

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;