Eclipse中使用MySql遇到: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 o
在Eclipse中使用MySQL遇到了点小问题
如果对Eclipse中配置MySql还有疑问的可以参考一下这篇博客:https://blog.csdn.net/qq_38247544/article/details/80419692
参考菜鸟上的例子的代码如下:
当然,这是修改后没问题后的代码
1 package mysqltest; 2 3 import java.sql.*; 4 5 public class Mysql { 6 // jdbc驱动名以及数据库URL 7 // static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver"; 8 static final String DB_URL = "jdbc:mysql://localhost:3306/javamysql" + "?serverTimezone=GMT%2B8"; 9 10 // 数据库的用户名与密码,需要根据自己的设置 11 static final String USER = "root"; 12 static final String PASS = "root"; 13 14 public static void main(String[] args) { 15 Connection conn = null; 16 Statement stmt = null; 17 try { 18 // 注册JDBC驱动 19 Class.forName("com.mysql.jdbc.Driver"); 20 21 // 打开链接 22 System.out.println("连接到数据库……"); 23 conn = DriverManager.getConnection(DB_URL, USER, PASS); 24 25 // 执行查询 26 System.out.println("实例化statement对象……"); 27 stmt = conn.createStatement(); 28 String sql; 29 sql = "SELECT id, name, url FROM websites"; 30 ResultSet rs = stmt.executeQuery(sql); 31 32 // 展开结果集数据库 33 while (rs.next()) { 34 // 通过字段检索 35 int id = rs.getInt("id"); 36 String name = rs.getString("name"); 37 String url = rs.getString("url"); 38 39 // 输出数据 40 System.out.print("ID:" + id); 41 System.out.print(",站点名:" + name); 42 System.out.println(",站点URL:" + url); 43 } 44 45 // 完成后关闭 46 rs.close(); 47 stmt.close(); 48 conn.close(); 49 } catch (SQLException se) { 50 // 处理 JDBC错误 51 se.printStackTrace(); 52 } catch (Exception e) { 53 // 处理Class.forname 错误 54 e.printStackTrace(); 55 } finally { 56 // 关闭资源 57 try { 58 if (stmt != null) 59 stmt.close(); 60 } catch (SQLException se2) { 61 } // 什么都不做 62 try { 63 if (conn != null) 64 conn.close(); 65 } catch (SQLException se) { 66 se.printStackTrace(); 67 } 68 } 69 System.out.println("Goodbye!"); 70 } 71 }
遇到的问题如下:
原来是因为使用最新的驱动包中`com.mysql.jdbc.Driver'类已经过时,新的`com.mysql.cj.jdbc.Driver'通过SPI自动注册,不再需要手动加载驱动类)
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.
但是后面还有一个如下的问题:
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.
找到了这篇博客
需要在数据库 URL中设置serverTimezone属性:(就是代码第八行)
static final String DB_URL = "jdbc:mysql://localhost:3306/RUNOOB" + "?serverTimezone=GMT%2B8"; 这里的 GMT%2B8 代表是东八区。(虽然不太明白为啥要加这个)
jdbc:mysql://localhost:3306/javamysql 端口号后面是你的数据库
最后问题解决了,如果有遇到相同问题的小伙伴可以参考一下!