1、注册驱动
在学习jdbc过程中遇到以下常见问题,都是与数据库版本有关,MySQL5.7以上的新版本好多地方进行了修改,所以现在对遇到的问题进行展示解决:
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.
这个问题是由于新版本将com.mysql.jdbc.Driver的包名改为了com.com.mysql.cj.jdbc.Driver,所以ji将注册的
Class.forName("com.mysql.jdbc.Driver");
改为
Class.forName("com.mysql.cj.jdbc.Driver");
然后运行错误消失。
2、获得数据库连接
获得数据库连接的url,以前的版本不用关心ssl安全、编码与时区问题,而新版本在url中需要加入ssl安全、编码与与时区:
Mon Aug 13 21:03:53 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 for server certificate verification.
java.sql.SQLException: The server time zone value '???ú±ê×??±??' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:89)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:63)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:73)
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:76)
at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:832)
at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:456)
at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:240)
at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:207)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:247)
at com.itcast.demo.JDBCDemo.main(JDBCDemo.java:45)
未加入ssl安全、编码与时区问题
String url = "jdbc:mysql://localhost:3306/mybase";
加入后
String url = "jdbc:mysql://localhost:3306/mybase?characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false";
然后注册与获取数据库连接问题就完美解决了。
try {
//Class.forName("com.mysql.jdbc.Driver");
Class.forName("com.mysql.cj.jdbc.Driver");
//获得数据库连接
//String url = "jdbc:mysql://localhost:3306/mybase";
String url = "jdbc:mysql://localhost:3306/mybase?characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false";
String username = "root";
String password = "root123";
Connection con = DriverManager.getConnection(url, username, password);
System.out.println(con);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
com.mysql.cj.jdbc.ConnectionImpl@6956de9