[英]Unable to connect to MySQL using Java Spring
伙計們 3 天我一直在尋找答案,但找不到我的問題的確切答案。 所以我正在做一些關於 Spring 和 Hibernate 的教程,並在嘗試在 Spring Boot 項目、MySQL 數據庫和 Tomcat 服務器之間建立連接時卡住了。 這是我的“pom.xml”文件:
<?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>
這是我的屬性:
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
我正在使用 XAMPP 的 Tomcat - 7.0.56 版。 MySQL Server 和連接器都是 8 版。不使用 Tomcat,我完全能夠連接到任何數據庫,編輯它等。
以下是發生的錯誤:
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.
可以在此處查看完整的錯誤日志。 我嘗試了很多東西。 請幫幫我。
編輯:好的。 我更仔細地閱讀了錯誤日志。 我正在使用 XAMPP 版本 7.056 中的 Tomcat,但 Spring 以版本 8 啟動 Tomcat。我不知道我是否應該提出另一個問題,但是當我將<tomcat.version>7.0.56</tomcat.version>
到我的屬性,錯誤日志改成了這個
抱歉。 我現在檢查了跟蹤。
當您檢查錯誤報告時,可能已在 connector/J 5.1.41 中修復。
將此更改為您的 pom.xml
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.15</version>
</dependency>
您是否在 mysql 上創建了名為bookstoredatabase 的數據庫? 我認為你應該將 mysql 連接器的依賴添加到 pom.xml
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.