连接mysql数据库时,通常的做法是在类的static块中写连接数据库的部分。如下图:
static { try { Class.forName("com.mysql.jdbc.Driver"); Connection conn = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "123456"); statement = (Statement) conn.createStatement(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }
我使用的mysql是8.0,mysql-connector-java.jar版本是5.1.46,如果直接运行,程序不会报错,一样执行成功,但会有如下的提示:
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 for server certificate verification.
大概意思就是在mysql更高版本中,需要使用useSSL建立连接,如果不需要用到可以将useSSL改为false。
方法就是在原有连接字符串加上useSSL=false。
jdbc:mysql://localhost:3306/test
替换为
jdbc:mysql://localhost:3306/test?characterEncoding=UTF-8&useSSL=false