简体   繁体   English

我用驱动<com.mysql.cj.jdbc.driver> ,但“junit test”不断导致错误</com.mysql.cj.jdbc.driver>

[英]I use the driver <com.mysql.cj.jdbc.Driver>, but 'junit test' keep causing error

version版本

mysql 8.0.18 mysql 8.0.18

java 1.8 java 1.8

spring 5.2.1.RELEASE spring 5.2.1.RELEASE

junit 4.12 junit 4.12

pom.xml 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.hj</groupId>
    <artifactId>groupboard</artifactId>
    <name>groupboard</name>
    <packaging>war</packaging>
    <version>1.0.0-BUILD-SNAPSHOT</version>

    <properties>
        <java-version>1.8</java-version>
        <org.springframework-version>5.2.1.RELEASE</org.springframework-version>
        <org.aspectj-version>1.9.4</org.aspectj-version>
        <org.slf4j-version>1.7.25</org.slf4j-version>
    </properties>

    <dependencies>
        <!-- Spring -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${org.springframework-version}</version>
            <exclusions>
                <!-- Exclude Commons Logging in favor of SLF4j -->
                <exclusion>
                    <groupId>commons-logging</groupId>
                    <artifactId>commons-logging</artifactId>
                 </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${org.springframework-version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>${org.springframework-version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${org.springframework-version}</version>
            <scope>test</scope>
        </dependency>


        <!-- AspectJ -->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>${org.aspectj-version}</version>
        </dependency>

        <!-- log4 -->
        <dependency>
            <groupId>org.bgee.log4jdbc-log4j2</groupId>
            <artifactId>log4jdbc-log4j2-jdbc4</artifactId>
            <version>1.16</version>
        </dependency>
        <!-- Logging -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${org.slf4j-version}</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>jcl-over-slf4j</artifactId>
            <version>${org.slf4j-version}</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>${org.slf4j-version}</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.15</version>
            <exclusions>
                <exclusion>
                    <groupId>javax.mail</groupId>
                    <artifactId>mail</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>javax.jms</groupId>
                    <artifactId>jms</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>com.sun.jdmk</groupId>
                    <artifactId>jmxtools</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>com.sun.jmx</groupId>
                    <artifactId>jmxri</artifactId>
                </exclusion>
            </exclusions>
            <scope>runtime</scope>
        </dependency>

        <!-- @Inject -->
        <dependency>
            <groupId>javax.inject</groupId>
            <artifactId>javax.inject</artifactId>
            <version>1</version>
        </dependency>

        <!-- Servlet -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>javax.servlet.jsp-api</artifactId>
            <version>2.3.1</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/junit/junit -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>


        <!-- mybatis -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.3</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>2.0.3</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.18</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-dbcp2</artifactId>
            <version>2.7.0</version>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter</artifactId>
            <version>RELEASE</version>
            <scope>test</scope>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-eclipse-plugin</artifactId>
                <version>2.9</version>
                <configuration>
                    <additionalProjectnatures>
                        <projectnature>org.springframework.ide.eclipse.core.springnature</projectnature>
                    </additionalProjectnatures>
                    <additionalBuildcommands>
                        <buildcommand>org.springframework.ide.eclipse.core.springbuilder</buildcommand>
                    </additionalBuildcommands>
                    <downloadSources>true</downloadSources>
                    <downloadJavadocs>true</downloadJavadocs>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <compilerArgument>-Xlint:all</compilerArgument>
                    <showWarnings>true</showWarnings>
                    <showDeprecation>true</showDeprecation>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.6.0</version>
                <configuration>
                    <mainClass>org.test.int1.Main</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

/groupboard/src/test/java/com.hj.groupboard.MySQLConnectionTest /groupboard/src/test/java/com.hj.groupboard.MySQLConnectionTest

package com.hj.groupboard;

import org.junit.Test;

import java.sql.Connection;
import java.sql.DriverManager;


public class MySQLConnectionTest {

    private static final String DRIVER = "com.mysql.cj.jdbc.Driver";
    private static final String URL = "jdbc:mysql://localhost:3306/againdb?serverTimezone=UTC";
    private static final String USER = "admin";
    private static final String PASSWORD = "************";

    @Test
    public void testConnection() throws Exception {
        Class.forName(DRIVER);
        try(Connection connection = DriverManager.getConnection(URL, USER, PASSWORD)) {
            System.out.println(connection);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

this is my console image of junit.这是我的 junit 的控制台图像。 please click this link .请点击此链接

I think this log from 'junit'.我认为这个日志来自'junit'。

Loading class 'com.mysql.jdbc.Driver'.正在加载 class 'com.mysql.jdbc.Driver'。 This is deprecated.这已被弃用。 The new driver class is 'com.mysql.cj.jdbc.Driver'.新的驱动程序 class 是 'com.mysql.cj.jdbc.Driver'。 The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.驱动通过SPI自动注册,一般不需要手动加载驱动class。

and I think this log from 'java console log'.我认为这个日志来自“java控制台日志”。

com.mysql.cj.jdbc.ConnectionImpl@4516af24 com.mysql.cj.jdbc.ConnectionImpl@4516af24

I think "java" has not returned error, but "junit" has returned error.我认为“java”没有返回错误,但“junit”返回了错误。

How do I fix that "junit" error or something.我该如何修复那个“junit”错误或其他东西。

Console log image控制台日志图像

============================append====================================== =============================追加===================== ==================

Please find this picture of IDE image.请找到这张 IDE 图像的图片。

IDE image IDE 图像

This error cause on Class.forName("com.mysql.cj.jdbc.Driver");此错误导致Class.forName("com.mysql.cj.jdbc.Driver"); , I think this means MySQLConnectionTest.java class dosent'n have a error, but maybe some library has a error. ,我认为这意味着 MySQLConnectionTest.java class dosent'n 有错误,但可能某些库有错误。

Now I want to find how to fix or change the library.现在我想找到如何修复或更改库。

And many thanks about everyone's help of this fix.非常感谢大家对此修复的帮助。 if I find solve this probleam, then I update this article.如果我找到解决这个问题,那么我更新这篇文章。

I believe your issue resides on your maven's plugin configuration.我相信您的问题出在您的 Maven 插件配置上。 I ran the test manually, with the following code and everything seems to work.我使用以下代码手动运行了测试,一切似乎都正常。 Nonetheless, the driver warning shouldn't cause any error.尽管如此,驱动程序警告不应导致任何错误。

public class Main {

    public static void main(String[] args) {
        JUnitCore junit = new JUnitCore();
        junit.run(MySQLConnectionTest.class);
    }
}

I am running this class inside the test folder, using the following directory structure.我在测试文件夹中运行这个 class,使用以下目录结构。

src/
   test/
       java/
           Main.java
           MySQLConnectionTest.java

That error causing by log4jdbc , that is logger library.log4jdbc引起的错误,即记录器库。

log4jdbc v1.12 is using 'com.mysql.jdbc.Driver' , not 'com.mysql.cj.jdbc.Driver' . log4jdbc v1.12 is using 'com.mysql.jdbc.Driver' , not 'com.mysql.cj.jdbc.Driver' .

https://github.com/arthurblake/log4jdbc/pull/88/commits/287f55a8a839a42e95e20788d80d29f2d995b6a1 https://github.com/arthurblake/log4jdbc/pull/88/commits/287f55a8a839a42e95e20788d80d29f2d995b6a1

But 'log4jdbc' library dose not updated, since 2013, And that dose not fixed that issue.但是自 2013 年以来, 'log4jdbc'库没有更新,并且没有解决该问题。

In conclusion, 'log4jdbc' cannot using with MySQL 5.x or later .总之, 'log4jdbc'不能与MySQL 5.x or later一起使用。

So I think I need to find something another logger library.所以我想我需要找到另一个记录器库。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 配置错误。 找不到 Class [com.mysql.cj.jdbc.Driver]。 EclipseLink, MySQL 驱动程序 - Configuration error. Class [com.mysql.cj.jdbc.Driver] not found. EclipseLink, MySQL driver 在应用程序中,我具有对JDBC驱动程序的访问权限(java.lang.ClassNotFoundException除外:com.mysql.cj.jdbc.Driver) - In application I have acsess to JDBC driver (exception java.lang.ClassNotFoundException: com.mysql.cj.jdbc.Driver) Liquibase Windows MySQL - Cannot find database driver: com.mysql.cj.jdbc.Driver - Liquibase Windows MySQL - Cannot find database driver: com.mysql.cj.jdbc.Driver Caused by: java.lang.RuntimeException: Driver com.mysql.cj.jdbc.Driver claims to not accept jdbcUrl - Caused by: java.lang.RuntimeException: Driver com.mysql.cj.jdbc.Driver claims to not accept jdbcUrl java netbeans新的驱动程序类是`com.mysql.cj.jdbc.Driver&#39;。 - java netbeans The new driver class is `com.mysql.cj.jdbc.Driver' 无法从 HikariConfig 加载驱动程序 class com.mysql.cj.jdbc.Driver - Failed to load driver class com.mysql.cj.jdbc.Driver from HikariConfig 独立的 jpackage 生成 java 应用程序为 com.mysql.cj.Z84BZFFD.5A0D4966AD1C50DA4BEC50Z.mysql.cj.Z84BZFFD.5A0D4967C7ACEr708Driver7A7086 - Stand alone jpackage generated java app generates ClassNotFoundException for com.mysql.cj.jdbc.Driver 应用程序无法加载请求的 class:com.mysql.cj.Z84BEFFD3A0D49636A58CE6080CAADriver.8 - Application could not load requested class : com.mysql.cj.jdbc.Driver 如何为Liferay查找&#39;java.lang.ClassNotFoundException:com.mysql.cj.jdbc.Driver&#39; - How to fiw 'java.lang.ClassNotFoundException: com.mysql.cj.jdbc.Driver' for liferay 我的 application.properties 中的属性显示为灰色。 构建成功但驱动程序 com.mysql.cj.jdbc.Driver 声称不接受 jdbcUrl - Properties in my application.properties are grayed out. Build success but Driver com.mysql.cj.jdbc.Driver claims to not accept jdbcUrl
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM