JDBD连接MySQL中的驱动与时区问题

1.在进行jdbc与mysql连接的时候应注意,加载驱动的方式根据MySQL版本内容来说 有变化

  5.7版本之前:

  

String driver= "com.mysql.jdbc.Driver";
String url="jdbc:mysql://localhost:3306/数据库名字";
String user="数据库用户名";
String password="数据库密码";

  5.7版本之后:

            String driver= "com.mysql.cj.jdbc.Driver";
            String url="jdbc:mysql://localhost:3306/dbnamel";
            String user="数据库用户名";
            String password="数据库密码";

    注意标红字体,加载驱动语句并不一样

2. MySQL8.0版本之后,连接数据库会有时区不一致的错误

  错误信息:Error querying database.  Cause: 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

  输入以下命令:

  show variables like '%time_zone%';

  查看时区信息

  然后输入

  set global time_zone='+8:00';

    

   输入exit退出,然后重新登入数据库并输入命令:

  show variables like '%time_zone%';

   

 此时时区问题就已经解决好了

猜你喜欢

转载自www.cnblogs.com/mengang/p/11882014.html
今日推荐