因为换新的电脑设备,为其安装一些开发需要的应用及环境,下载了新版的Mysql8.0.13,在Eclipse中测试连接时遇到一些新的问题,遂记录。
1. Mysql 5.* 版本JDBC连接
a. 常规导入 5.* jar 包
b. 编写测试程序
packagewqz.mysql.test;importjava.sql.DriverManager;public classTest {private static String url = "jdbc:mysql://localhost:3306/你的库名";//数据库服务地址private static String driver = "com.mysql.jdbc.Driver";//驱动路径private static String username = "root";private static String passworld = "你的密码";public static void main(String[] args) throwsException {
Class.forName(driver).newInstance();//如果能连接成功,则打印连接
System.out.println(DriverManager.getConnection(url, username, passworld));
}
}
c. 运行程序
如果数据库是Mysql 5.* 版本 ,在没有出现输入错误的连接信息情况下,会成功输出数据库连接对象。
2. 测试 Mysql 8.* 的连接
使用和上述驱动和程序 连接 Mysql 8.*,没有输出连接对象,异常信息如下 :
Tue Oct 30 10:20:06 CST 2018 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore forserver certificate verification.
Exception in thread"main"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:404)
at com.mysql.jdbc.Util.getInstance(Util.java:387)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:917)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:896)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:885)
at com.mysql.jdbc.SQLError.createSQLException(SQL