mysql 连接 jar包_Mysql最新jar包连接数据库报错

今天准备开始从最基础的jdbc复习复习,于是就在maven里下载了最新的mysql jar包:

jar包:

3dd31cb0999bb632eb0b9cefff2162e2.png

连接数据库查询

Connection connection=null;

PreparedStatement preparedStatement=null;

ResultSet resultSet=null;try{

Class.forName("com.mysql.jdbc.Driver");

connection=DriverManager.getConnection("jdbc:mysql://localhost:3306/myshool?characterEncoding=utf-8&useSSL=false","root","root");

String sql="select * from student where name= ? ";

preparedStatement=connection.prepareStatement(sql);

preparedStatement.setString(1,"小明");

resultSet=preparedStatement.executeQuery();while(resultSet.next()){

System.out.println(resultSet.getString("id")+" " +resultSet.getString("name"));

}

}catch(ClassNotFoundException e) {

e.printStackTrace();

}catch(SQLException e) {

e.printStackTrace();

}

在main方法写完后提交报错信息:

6d62ffe36ad8fa0af320b7e9b9617a07.png

也就是报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后添加useSSL=false&serverTimezone=Hongkong

之后试了一下果然有效,但是为什么呢?

后来查了一下,说是用mysql最新的jar包造成的原因,这个版本的包需要加时区,还有若不加useSSL=false,就会警告:

4089a96de4deae989582bda23104d5a3.png

若有:

4013ef5613df02204d6ab879c8fb01ff.png

这个警告,那么把com.mysql.jdbc.Driver改成com.mysql.cj.jdbc.Driver就可以了

最后运行出来的结果就是:

7bf6b4ec491e11aa0134dd6e93859083.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值