Mac使用druid获取连接时报空指针异常解决方案

若代码写法没有问题,报各种奇怪错误时,尝试升级版本解决。

在Mac上测试使用druid连接池获取连接时,报如下错误:

Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
十一月 17, 2021 10:00:03 下午 com.alibaba.druid.pool.vendor.MySqlValidConnectionChecker warn
警告: Cannot resolve com.mysq.jdbc.Connection.ping method.  Will use 'SELECT 1' instead.
java.lang.NullPointerException
    at com.alibaba.druid.pool.vendor.MySqlValidConnectionChecker.(MySqlValidConnectionChecker.java:48)
    at com.alibaba.druid.pool.DruidDataSource.initValidConnectionChecker(DruidDataSource.java:878)
    at com.alibaba.druid.pool.DruidDataSource.init(DruidDataSource.java:594)
    at com.alibaba.druid.pool.DruidDataSource.getConnection(DruidDataSource.java:915)
    at com.alibaba.druid.pool.DruidDataSource.getConnection(DruidDataSource.java:911)
    at com.alibaba.druid.pool.DruidDataSource.getConnection(DruidDataSource.java:98)
    at cn.itcast.util.JDBCUtils.getConnection(JDBCUtils.java:34)
    at cn.itcast.jdbc.DruidDemo.main(DruidDemo.java:10)

十一月 17, 2021 10:00:05 下午 com.alibaba.druid.pool.DruidDataSource error
严重: init datasource error
java.sql.SQLException: validateConnection false
    at com.alibaba.druid.pool.DruidAbstractDataSource.validateConnection(DruidAbstractDataSource.java:1216)
    at com.alibaba.druid.pool.DruidAbstractDataSource.createPhysicalConnection(DruidAbstractDataSource.java:1439)
    at com.alibaba.druid.pool.DruidDataSource.init(DruidDataSource.java:618)
    at com.alibaba.druid.pool.DruidDataSource.getConnection(DruidDataSource.java:915)
    at com.alibaba.druid.pool.DruidDataSource.getConnection(DruidDataSource.java:911)
    at com.alibaba.druid.pool.DruidDataSource.getConnection(DruidDataSource.java:98)
    at cn.itcast.util.JDBCUtils.getConnection(JDBCUtils.java:34)
    at cn.itcast.jdbc.DruidDemo.main(DruidDemo.java:10)

当前使用的druid的版本为1.0.9,mysql-connector-java的版本为8.0.16,将druid的版本尝试升级为1.1.19时,可正常获取连接。

你可能感兴趣的:(druidjavamysql)