在 SpringMVC+Spring+MyBatis 整合利用中拜访 Mysql 呈现以下报错
具体报错:
19-Feb-2021 11:08:54.808 重大 [Druid-ConnectionPool-Create-1512829159] com.alibaba.druid.support.logging.JakartaCommonsLoggingImpl.error create connection SQLException, url: jdbc:mysql://localhost:3306/springdb, 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.
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1055)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:926)
at com.mysql.jdbc.ConnectionImpl.configureClientCharacterSet(ConnectionImpl.java:1734)
at com.mysql.jdbc.ConnectionImpl.initializePropsFromServer(ConnectionImpl.java:3539)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2151)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:776)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:46)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:352)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:284)
at com.alibaba.druid.pool.DruidAbstractDataSource.createPhysicalConnection(DruidAbstractDataSource.java:1570)
at com.alibaba.druid.pool.DruidAbstractDataSource.createPhysicalConnection(DruidAbstractDataSource.java:1636)
at com.alibaba.druid.pool.DruidDataSource$CreateConnectionThread.run(DruidDataSource.java:2505)
查看,查问 mysql 的版本发现本地的是 8.0.22,更改 maven 的 pom.xml 文件的 mysql 驱动版本为 8.0.22,刷新 pom.xml,重新启动利用即可
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.22</version>
</dependency>
百度过程中还发现 driverClassName 有可能会报这个错,然而这里的不是这个起因
driverClassName: com.mysql.jdbc.Driver # mysql-connector-java 5.x 及之前版本中的
driverClassName: com.mysql.cj.jdbc.Driver # mysql-connector-java 6.x 及后续版本中的
这里有一个疑难?
- driverClassName 没配置利用也能够失常拜访数据库,配置了也能够,这个是什么原理?
百度了临时没找到为什么
- characterEncoding=UTF-8 加不加都能够
<!-- 申明数据源, 连贯数据库 -->
<context:property-placeholder location="classpath:conf/jdbc.properties"/>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
init-method="init" destroy-method="close">
<!-- <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>-->
<!-- <property name="url" value="${jdbc.url}?characterEncoding=UTF-8"/>-->
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
问题 1:driverClassName 没配置利用也能够失常拜访数据库,配置了也能够,这个是什么原理?
有大佬指导可能是以下状况(未验证,先记录了前面有工夫在验证吧)
- 在 mysql 的驱动 jar 中曾经配置了
- 在 com.alibaba.druid.pool.DruidDataSource 配置的