Failed to load driver class com.mysql.cj.jdbc.Driver exception-IntellIJ Idea-backend project connection database configuration

Preface : When the back-end project connects to the database configuration, the following application.properties database connection configuration is added

server.port= 8081

spring.datasource.url=jdbc:mysql://localhost:3306/small_pass?characterEncoding=utf-8&useUnicode=true&serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=root

insert image description here

When the project clicks to run, the following series of problems appear


问题一、Failed to load driver class com.mysql.cj.jdbc.Driver in either of HikariConfig class loader or Thread context classloader

This situation is usually caused by one of the following reasons:

1. The mysql-connectorjar package is not introduced into the project, or the imported package version does not match the JDBC driver. Solution: Add related dependencies in the project pom.xml.

2. There are multiple versions of JDBC drivers in the project, and they are incompatible. Solution: Unify the UJDBC driver version used in the project and correctly reference it in the code.

3. The driver is not properly registered. Solution: Register the MySQLJDBC driver in the program, for example, add Class.forName("com.mysql.cj.jdbc.Driver") in the Java code
to register the driver.

4. The connection properties in the configuration file are not set correctly. Solution: You need to check whether the database connection properties are set correctly, such as checking whether the database URL, user name, password and other information match and are correct.

5. The database is not started or cannot connect to the database. Solution: Check that the database is running and make sure you can connect to the database through the correct database URL.

This is category 1 - solution : add mybatis plugin in pom.xml

<!-- Mybatis整合Spring Boot的依赖项 -->
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.2.0</version>
</dependency>
<!-- MySQL的依赖项 -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <scope>runtime</scope>
    <version>5.1.38</version>
</dependency>

Question 2. Dependency 'mysql:mysql-connector-java:' not found

Reason : The mysql dependency cannot be downloaded, and
the solution cannot be found : Enter the Maven tool interface, select the right button of the project, if there is no create setting.xml, select create 'setting.xml', fill in Ali for configuration

insert image description here



Question 3: Click to start, or Failed to load driver class com.mysql.cj.jdbc.Driver problem

insert image description here

The reason this time is : the mysql version 5.1.38 configured in my pom.xml is too low and needs to be updated

Solution : update the mysql version to 8.0.11
insert image description here

Problem solved: the project is running normally;

insert image description here

Detect the successful configuration of the connection databaseinsert image description here

Create value, happy to share! 776147358

Guess you like

Origin blog.csdn.net/ly_xiamu/article/details/132233586