在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及后续版本中的
这里有一个疑问?
<!--声明数据源,连接数据库-->
<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配置的