关于Myeclipse连接不上MySQL8.0

weixin_43585201 2019-06-19 04:43:01
数据库密码都正确,Navicat of Mysql 也能连接mysql,连接的语句也没有错,硬是连接不了。 在这里插入图片描述 后来发现是驱动包的问题,驱动包要升级为 mysql-connector-java-8.0.13.jar,我原本的是mysql-connector-java-5.1.36.jar,所以就出错了。 mysql-connector-java-8.0.13.zip在MySQL的官网就可以下载 //驱动类com.mysql.jdbc.Driver Class.forName("com.mysql.jdbc.Driver"); System.out.println("数据库加载成功!"); // 建立与数据库的Connection连接 Connection c = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/how2java?characterEncoding=UTF-8","root","admin"); 会出现警告① Establishing SSL connection without server's identityverification 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 optionisn't set. For compliance with existing applications not using SSL theverifyServerCertificate property is set to 'false'. You need either toexplicitly disable SSL by setting useSSL=false, or set useSSL=true and providetruststore for server certificate verification. 原因 驱动程序的描述字符串com.mysql.jdbc.Driver是mysql5.0及以前的老写法 而mysql6.0以后 包括我用的8.0需要这样写com.mysql.cj.jdbc.Driver 警告② 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. 原因 MySQL在高版本需要指明是否进行SSL连接。解决方案: 在mysql连接字符串url中加入ssl=true或者false即可,如下所示 "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=Hongkong" 完整的代码如下 import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public class TestJDBC { public static void main(String[] args) { //初始化驱动 try { //老版驱动类com.mysql.jdbc.Driver //如果忘记了第一个步骤的导包,就会抛出ClassNotFoundException Class.forName("com.mysql.cj.jdbc.Driver"); System.out.println("数据库加载成功!"); Connection c = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?" + "useUnicode=true&characterEncoding=utf8&" + "useSSL=false&serverTimezone=Hongkong","root","admin"); System.out.println("连接成功,获取连接对象: " + c); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } } } 其中test是本机数据库名,root和admin是mysql安装时创建的账户和密码。
...全文
322 1 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
睡觉唱国歌 2019-06-20
  • 打赏
  • 举报
回复
就不能用mysql5.5吗

23,408

社区成员

发帖
与我相关
我的任务
社区描述
Java 非技术区
社区管理员
  • 非技术区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧