Mysql最新jar包连接数据库报错
今天准备开始从最基础的jdbc复习复习,于是就在maven里下载了最新的mysql jar包:
jar包:
连接数据库查询
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方法写完后提交报错信息:
也就是报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,就会警告:
若有:
这个警告,那么把com.mysql.jdbc.Driver改成com.mysql.cj.jdbc.Driver就可以了
最后运行出来的结果就是: