【问题标题】:Problem with the connexion to the data base using Spring使用 Spring 连接到数据库的问题
【发布时间】:2019-11-07 07:17:58
【问题描述】:

我正在使用带有 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>

【问题讨论】:

  • 你确定mysql连接字符串“jdbc:mysql://localhost:8080/BDD_Vanerum_Test”吗?你试过用不同的mysql客户端吗? (端口可能是 3306 是默认的 mysql 端口,8080 端口可能是您的 Web 应用程序端口)
  • 看起来 mysql 没有响应。您可以使用来自终端的相同凭据进行连接吗?此外,如果您要扩展 JpaRepository,则不需要 @Repository 注释
  • @FouadHAMDI 我都试过了,它给了我同样的错误。 “BDD_Vanerum_Test”是我从终端创建的数据库的名称,它出现在“phpMyadmin”中
  • 可以从终端连接数据库吗?

标签: java mysql spring-mvc


【解决方案1】:

其实只是JAVA版本的问题,我修复了:

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

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

【讨论】:

    猜你喜欢
    • 2020-12-13
    • 1970-01-01
    • 2020-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-10
    相关资源
    最近更新 更多