问题描述1:
本人电脑环境:数据库mysql,版本是8.0.22;
maven依赖中是选的是5版本:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
配置文件:
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test
jdbc.username=root
jdbc.password=root
spring测试数据库连接的时候出现:
com.alibaba.druid.support.logging.JakartaCommonsLoggingImpl error
严重: create connection SQLException, url: jdbc:mysql://localhost:3306/test, errorCode 0, state S1000
java.sql.SQLException: Unknown initial character set index ‘255’ received from server. Initial client character set can be forced via the ‘characterEncoding’ property.
解决方案:
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8
jdbc.username=root
jdbc.password=root
或者这样:
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC&characterEncoding=UTF8
jdbc.username=root
jdbc.password=root
数据库不兼容的问题,添加语言解决。
当换成5.1.32数据库连接时出现问题:
mysql mysql-connector-java 5.1.32 ```
com.alibaba.druid.support.logging.JakartaCommonsLoggingImpl error
严重: create connection SQLException, url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8, errorCode 0, state 08001
com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server.
建议5版本选择5.1.6。
问题描述2:
关于连接mysql 8.0连接url
问题描述:在连接数据库配置都没错,jar包也有,但是还是报
Could not create connection to database server.错误,那是因为mysql8.0版本的使用的数据库连接字符串不一样,而且还对时区有要求,引用下面的连接即可
1.url设置:
jdbc:mysql://localhost:3306/database?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true
2.driver设置:
jdbc.driver=com.mysql.cj.jdbc.Driver
3.同时也要引入较高版本的jar包才能与MySQL8.0版本相匹配
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
</dependency>