数据库
首页 > 数据库> > jdbc连接数据库问题解决

jdbc连接数据库问题解决

作者:互联网

一、mysql-connector-java与mysql版本不相符

1、报错信息

com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server.
	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:409)
	at com.mysql.jdbc.Util.getInstance(Util.java:384)
	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1013)
	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:987)
	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:973)
	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:918)
	at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2588)
	at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2321)
	at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:832)
	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:409)
	at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:417)
	at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:344)
	at java.sql.DriverManager.getConnection(DriverManager.java:664)
	at java.sql.DriverManager.getConnection(DriverManager.java:247)
	at JdbcDemo.main(JdbcDemo.java:23)
Caused by: java.lang.NullPointerException
	at com.mysql.jdbc.ConnectionImpl.getServerCharacterEncoding(ConnectionImpl.java:3307)
	at com.mysql.jdbc.MysqlIO.sendConnectionAttributes(MysqlIO.java:1985)
	at com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(MysqlIO.java:1911)
	at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1288)
	at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2506)
	at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2539)
	... 13 more

2、问题分析

首先给大家看下我maven库mysql-connector的版本以及mysql的版本

2、1 maven库mysql-connector版本

<dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.30</version>
 </dependency>

2、2 mysql版本

mysql-installer-community-8.0.19.0

3、结论

maven工程中导入的mysql的jar版本和我的MySQL版本不相符,必须相一致才行!比如,你安装的MySQL是8.x,必须使用高版本对应的mysql-connector-java-8.x.x

二、url参数问题

1、报错信息

com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server.
	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:409)
	at com.mysql.jdbc.Util.getInstance(Util.java:384)
	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1013)
	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:987)
	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:973)
	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:918)
	at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2588)
	at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2321)
	at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:832)
	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:409)
	at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:417)
	at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:344)
	at java.sql.DriverManager.getConnection(DriverManager.java:664)
	at java.sql.DriverManager.getConnection(DriverManager.java:247)
	at JdbcDemo.main(JdbcDemo.java:24)
Caused by: java.lang.NullPointerException
	at com.mysql.jdbc.ConnectionImpl.getServerCharacterEncoding(ConnectionImpl.java:3307)
	at com.mysql.jdbc.MysqlIO.sendConnectionAttributes(MysqlIO.java:1985)
	at com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(MysqlIO.java:1911)
	at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1288)
	at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2506)
	at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2539)
	... 13 more

2、问题分析

2、1 jdbc连接url

conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/yingyong?useUnicode=true&characterEncoding=UTF-8", "root", "root");

经过一系列百度,我认为极有可能是mysql8为了防止时差问题,强制要求指定时区,所以我就试着加上了时区设置,如下:

conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/yingyong?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai", "root", "root");

修改完成之后,发现执行成功,但是出现一个警告信息,如下:

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

这个我们稍后会说

3、结论

mysql8为了防止时差问题,强制要求指定时区。

三、驱动问题

1、报错信息

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.

2、问题分析

我们先来翻译一下报错信息,它的意思是,加载类“com.mysql.jdbc.Driver”。这是弃用。新的驱动程序类是’ com.mysql. jc .jdbc. driver '。驱动程序是通过SPI自动注册的,通常不需要手动加载驱动程序类。通过报错信息我们可以很容易的定位到是版本问题。看一下我的驱动,如下:

Class.forName("com.mysql.jdbc.Driver");

所以我把它改成了报错信息提示的,如下:

Class.forName("com.mysql.cj.jdbc.Driver");

执行,警告信息果然没了。

结论

com.mysql.jdbc.Driver 是 mysql-connector-java 6以下版本的,com.mysql.cj.jdbc.Driver 是 mysql-connector-java 6以上版本推荐使用的,但是需要指定时区serverTimezone,大家根据mysql版本使用即可。

标签:jdbc,java,数据库,ConnectionImpl,SQLError,mysql,com,连接
来源: https://blog.csdn.net/weixin_44321211/article/details/111295895