最近在学java,连接数据库的时候遇到了很多问题,明明是按照老师的代码写的,无奈。。
问题1:5Xjar连接出现WARN: Establishing SSL connection without server’s identity verification is not recommended.
原因:如果不设置显式选项,则必须建立默认的SSL连接。
问题代码:
conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/s ",
"root","123456");
解决方案:
conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/s?useUnicode=true&characterEncoding=utf-8&useSSL=false",
"root","123456");
问题2:jar的版本导致代码的不同
主要有两个不同:
(1)加载驱动时:
5X版本:Class.forName("com.mysql.jdbc.Driver");
8X版本:Class.forName("com.mysql.cj.jdbc.Driver");
(2)建立连接时:
5X版本:conn = DriverManager.getConnection( "jdbc:mysql://localhost:3306/s?useUnicode=true&characterEncoding=utf-8&useSSL=false", "root","123456");
8X版本:conn = DriverManager.getConnection( "jdbc:mysql://localhost:3306/s?serverTimezone=UTC ","root","123456");
源代码
mysql-cnnector-java-5.1.jar
代码:
package MySQLconnect;
import java