java - 使用Spring连接数据库的问题

标签 java mysql spring-mvc

我正在使用带有 Spring MVC 框架的简单 mySQL 数据库设置 CRUD 应用程序,但我在连接到数据库时遇到问题。

我收到以下错误:

com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure

最后一个数据包已成功发送到服务器。驱动程序尚未收到来自服务器的任何数据包。

这是 DAO 的代码:

package com.ApplicationDevis.demo;

import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface ProjetDAO extends JpaRepository<Utilisateur, Integer> // entité à gerer et le type de l'ID 
{
}

对于实体:

@Entity 

public class Utilisateur
{
            @Id // pour dire que notre id et la cle primère
    @GeneratedValue // pour dire qu'elle est auto incrémenté 
    int id;


    String Nom;
    String Prenom;
    String Societe;

    String Ville;

    String CP;

    String Num_Tel;

    String email;

对于我的application.properties:

spring.datasource.url=jdbc:mysql://localhost:8080/BDD_Vanerum_Test
spring.datasource.username=root
spring.datasource.password=rida
spring.jpa.show-sql=true 

and finaly my pom.xml :

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

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

最佳答案

实际上这只是JAVA版本问题,我使用以下方法修复了它:

jdbc:mysql://localhost/BDD_Vanerum_Test?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC

而不是我在之前的代码中使用的行。

关于java - 使用Spring连接数据库的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56754249/

相关文章:

java - 使用@ControllerAdvice 和@ResponseStatus 更改响应状态代码

java - 复制 Android Java 类的正确方法 w。所有必需的资源

java - 在两个 Android 应用程序之间共享数据

Java : How to add date in to mysql database and get back

php - 如何将数组分配给sql查询的变量

java - JsonIgnore 不适用于 Spring

java - 如何通过 webdriver 测试输入字段功能的屏蔽

php - 在 mysql 的特定表中插入 JSON 文件

mysql - 将 Mysql 列从一个表添加到另一个表

java - 无法在 spring mvc 中加载默认索引页面