IDEA连接MySql8.0的一些坑

IDEA连接MySql8.0的一些坑

1. maven配置

当maven更改为阿里云镜像后,默认是会下载mysql驱动包

具体在pom.xml的配置如下:

<dependencies>
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <version>8.0.19version>
        dependency>
 dependencies>

2. 注册驱动

直接运行时可能会报以下错误:

Loading class com.mysql.jdbc.Driver'. This is deprecated. The new driver class iscom.mysql.cj.jdbc.Driver’. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.

这是因为新版MySQL不推荐使用 com.mysql.jdbc.Driver ,所以注册驱动时改为如下即可

 DriverManager.registerDriver(new com.mysql.cj.jdbc.Driver());

3.连接数据库

这里以数据库名为 test 为例

当直接使用jdbc:mysql://localhost:3306/***时会跳出以下错误

Exception in thread “main” java.sql.SQLException: The server time zone value ‘�й���׼ʱ��’ is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the ‘serverTimezone’ configuration property) to use a more specifc time zone value if you want to utilize time zone support.

我一开始是改为之前那样子带一个修改字符集的参数

jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8

结果还是不行,原因是到MySQL8.0相应的驱动包也发生了更新,所以找到了一种新的方法
,参数在原来的基础上加上&useSSL=false&serverTimezone = GMT,如下

jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone = GMT

连接成功

注意:在xml文件中是不允许用&符号的,所以应该用&替代

jdbc:mysql://localhost:3306/test?useUnicode=true&amp;characterEncoding=UTF-8&amp;useSSL=false&amp;serverTimezone = GMT

4. jdk版本低

数据库测试调试后,发现报黄色错误,输入影响运行但是还是解决下

错误信息为

Warning:java: 源值1.5已过时, 将在未来所有发行版中删除

Warning:java: 目标值1.5已过时, 将在未来所有发行版中删除

解决方法

  • 修改Maven的Settings.xml文件添加如下内容

    <profile>
      <id>jdk-1.8id>
      <activation>
        <activeByDefault>trueactiveByDefault>
        <jdk>1.8jdk>
      activation>
      <properties>
        <maven.compiler.source>1.8maven.compiler.source>
        <maven.compiler.target>1.8maven.compiler.target>
        <maven.compiler.compilerVersion>1.8maven.compiler.compilerVersion>
      properties>
    profile>
    
  • 在项目的pom.xml文件中添加

    <properties>
     <maven.compiler.source>1.8maven.compiler.source>
      <maven.compiler.target>1.8maven.compiler.target>
    properties>
    
  • 打开项目配置,设置Modules的Language Level为”8”

IDEA连接MySql8.0的一些坑_第1张图片

  • 最后按”Ctrl+Alt+S”打开设置,搜索”Java Compiler”,将默认jdk和当前modual的jdk版本切换为1.8即可

你可能感兴趣的:(后端学习,#,数据库)