简体   繁体   中英

Spring Boot can not connect to Mysql

have un problem connection data base with spring boot

I'm the sequel of a developer that is already working. I've been provided the sources but I can't launch the Spring Boot Java project with IntelliJ. I use a sever Xampp for my dataBase. But I have the following error

     :: Spring Boot ::        (v2.3.3.RELEASE)

    2020-12-15 16:39:28.411  INFO 2384 --- [           main] c.a.myapp.myappApplication  : Starting myappApplication on DESKTOP-DRP2JSE with PID 2384 (C:\Users\Admin\Downloads\myapp 2.0 Final\myappBack 2.0 - Final\myappBack\out\production\myappBack started by Admin in C:\Users\Admin\Downloads\myapp 2.0 Final\myappBack 2.0 - Final\myappBack)
    2020-12-15 16:39:28.415  INFO 2384 --- [           main] c.a.myapp.myappApplication  : No active profile set, falling back to default profiles: default
    2020-12-15 16:39:29.382  INFO 2384 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFERRED mode.
    2020-12-15 16:39:29.496  INFO 2384 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 108ms. Found 8 JPA repository interfaces.
    2020-12-15 16:39:30.032  INFO 2384 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
    2020-12-15 16:39:30.040  INFO 2384 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
    2020-12-15 16:39:30.040  INFO 2384 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.37]
    2020-12-15 16:39:30.396  INFO 2384 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
    2020-12-15 16:39:30.396  INFO 2384 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1922 ms
    2020-12-15 16:39:30.498  WARN 2384 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaConfiguration': Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.zaxxer.hikari.HikariDataSource]: Factory method 'dataSource' threw exception; nested exception is org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Failed to determine a suitable driver class
    2020-12-15 16:39:30.500  INFO 2384 --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
    2020-12-15 16:39:30.513  INFO 2384 --- [           main] ConditionEvaluationReportLoggingListener : 

    Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
    2020-12-15 16:39:30.519 ERROR 2384 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

    ***************************
    APPLICATION FAILED TO START
    ***************************

    Description:

    Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.

    Reason: Failed to determine a suitable driver class


    Action:

    Consider the following:
        If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
        If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).


    Process finished with exit code 1
    server.port=8081

    server.servlet.session.timeout=1200
    # JDBC URL of the database.
    spring.datasource.url=jdbc:mysql://localhost:3306/myapp?zeroDateTimeBehavior=CONVERT_TO_NULL&serverTimezone=UTC
    # Login username of the database.
    spring.datasource.username= root
    # Login password of the database.
    spring.datasource.password=
    # Fully qualified name of the JDBC driver. Auto-detected based on the URL by default.
    spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
    # Whether to enable logging of SQL statements.
    spring.jpa.show-sql=true
    # DDL mode. This is actually a shortcut for the "hibernate.hbm2ddl.auto" property. Defaults to "create-drop" when using an embedded database and no schema manager was detected. Otherwise, defaults to "none".
    spring.jpa.hibernate.ddl-auto=update
    # Additional native properties to set on the JPA provider.
    spring.jpa.database-platform: org.hibernate.dialect.MySQL5InnoDBDialect
    spring.jpa.properties.hibernate.storage.storage_engine=innodb



    # Avoid to restart server (only during dev phase) if DevTools are uninstall
    # spring.thymeleaf.cache=false
    # spring.security.user.name="root"
    # spring.security.user.password="123"
    spring.resources.add-mappings=true

The problem is written Reason: Failed to determine a suitable driver class . Classloader can not find driver-class-name in you classpath. Maybe missing MySQL library definition in the maven / gradle configuration.

Maven:

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>

Gradle:

runtimeOnly 'mysql:mysql-connector-java'

If MySQL library is existed, try to change driver-class-name definition as below.

spring.datasource.driver-class-name=com.mysql.jdbc.Driver

... instead of

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

Check your mysql-connector-java version jar if it's 5 it should be

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

if its 8 it should be

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

Also I don't think you should have spaces after equal sign where is your username and password

it works, just force to reload the maven dependency and is ok;

just warning to fix

 SLF4J: Class path contains multiple SLF4J bindings. SLF4J: Found binding in [jar:file:/C:/Users/Admin/Downloads/myapp%202.0%20Final/myappBack%202.0%20-%20Final/myappBack/target/myappService/WEB-INF/lib/logback-classic-1.2.3.jar./org/slf4j/impl/StaticLoggerBinder:class] SLF4J: Found binding in [jar:file:/C./Users/Admin/.m2/repository/ch/qos/logback/logback-classic/1.2.3/logback-classic-1.2.3.jar:/org/slf4j/impl/StaticLoggerBinder:class] SLF4J. See http.//www.slf4j.org/codes:html#multiple_bindings for an explanation. SLF4J. Actual binding is of type [ch.qos.logback.classic.util.ContextSelectorStaticBinder]

Try and remove the spring.datasource.class-name property from the config file. The url should be good enough to tell which classname is to be used.

Also set "logging.level.root=debug" in the properties to get some more details. See if that helps.

配置文件的屏幕截图

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM