NewJavaEnthusiast
NewJavaEnthusiast

Reputation: 103

Unable to connect to MySQL using Java Spring

Guys for 3 days I am searching for an answer and can't find the exact answer for my problem. So I am doing some tutorials on Spring and Hibernate and get stuck when trying to establish a connection between Spring Boot project, MySQL database and Tomcat server. Here is my "pom.xml" file:

<?xml version="1.0" encoding="UTF-8"?>
  <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.bookstore</groupId>
    <artifactId>bookstore</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>Bookstore</name>
    <description>frontend part for our bookstore project</description>

    <parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>1.4.3.RELEASE</version>
      <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
      <java.version>1.8</java.version>
    </properties>

    <dependencies>
      <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
      </dependency>

      <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
      </dependency>
    </dependencies>

    <build>
      <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
      </plugins>
    </build>

And here are my properties:

spring.thymeleaf.cache=false

# ===============================
# = DATA SOURCE
# ===============================

# Set here configurations for the database connection
spring.datasource.url=jdbc:mysql://localhost:3306/bookstoredatabase?autoReconnect=true&useSSL=false

# Username and secret
spring.datasource.username=root
spring.datasource.password=target

# Keep the connection alive if idle for a long time (needed in production)
spring.datasource.testWhileIdle = true
spring.datasource.validationQuery = SELECT 1

# ===============================
# = JPA / HIBERNATE
# ===============================

# Use spring.jpa.properties.* for Hibernate native properties (the prefix is
# stripped before adding them to the entity manager).

# Show or not log for each sql query
spring.jpa.show-sql=true

# Hibernate ddl auto (create, create-drop, update): with "update" the database
# schema will be automatically updated accordingly to java entities found in
# the project
spring.jpa.hibernate.ddl-auto = update

# Allows Hibernate to generate SQL optimized for a particular DBMS
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect

I am using Tomcat from XAMPP - version 7.0.56. MySQL Server and the connector are both version 8. Without using Tomcat, I am perfectly able to connect to any database, edit it, etc.

Here are the errors, which occur:

INFO 15816 --- [main] org.hibernate.Version: HHH000412: Hibernate Core {5.0.11.Final}
INFO 15816 --- [main] org.hibernate.cfg.Environment: HHH000206: hibernate.properties not found
INFO 15816 --- [main] org.hibernate.cfg.Environment: HHH000021: Bytecode provider name:javassist
INFO 15816 --- [main] o.hibernate.annotations.common.Version: HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
ERROR 15816 --- [main] o.a.tomcat.jdbc.pool.ConnectionPool: Unable to create initial connections of pool. com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server. Attempted reconnect 3 times. Giving up.

The full error log can be seen here . I have tried so many things. Please help me guys.

EDIT: Okay. I have read the error log more carefully. I am using Tomcat from XAMPP, version 7.056, but Spring starts Tomcat with version 8. I don't know if I should make another question, but when I added <tomcat.version>7.0.56</tomcat.version> to my properties, the error log changed into this

Upvotes: 2

Views: 802

Answers (2)

Doo Su Jung
Doo Su Jung

Reputation: 57

I'm sorry. I checked Trace now.

When you check the bug report, was probably fixed in connector/J 5.1.41.

change this to your pom.xml

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.15</version>
</dependency>

Upvotes: 2

Segni
Segni

Reputation: 1

Have you created database called bookstoredatabase on mysql? and I think you should add dependency of mysql connector to pom.xml

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

Upvotes: 0

Related Questions