MySql连接 alibaba.druid.support.logging.JakartaCommonsLoggingImpl.error create con

·  阅读 345

在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及后续版本中的
复制代码

这里有一个疑问?

  1. driverClassName没配置应用也可以正常访问数据库,配置了也可以,这个是什么原理?

百度了暂时没找到为什么 2. 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没配置应用也可以正常访问数据库,配置了也可以,这个是什么原理? 有大佬指点可能是以下情况(未验证,先记录了后面有时间在验证吧)

  1. 在mysql的驱动jar中已经配置了

  1. 在com.alibaba.druid.pool.DruidDataSource配置的

分类:
后端
标签: