这个错误是无法检索只读服务,查询后发现,是jdbc驱动包和mysql的版本对不上,我用的mysql是8.0.10版本,而jdbc的版本是5.1.32,所以需要下载一个版本相同的jdbc驱动包。
下载链接:https://www.mysql.com/downloads/
具体步骤如图:
1、在这里插入图片描述
2、
在这里插入图片描述
3、
在这里插入图片描述
4、
在这里插入图片描述
5、因为我用的是Windows系统,所以选择对应版本的压缩包进行下载
在这里插入图片描述
6、直接下载
在这里插入图片描述
下载结束后解压,找到mysql-connector-java-8.0.18.jar包,然后将其拷贝到eclipse的lib文件夹下。
如图:
在这里插入图片描述
这一步结束之后,还需要注意在加载驱动和获取连接的时候,和旧版本也并不相同。
旧版本代码:

   Class.forName("com.mysql.jdbc.Driver");
   String url = "jdbc:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=utf-8";
   String user = "root";
   String password = "具体密码";
   Connection conn = DriverManager.getConnection(url, user, password);

新版本的Class.forName和url值都不一样,具体看代码:

   Class.forName("com.mysql.cj.jdbc.Driver");
   String url = "jdbc:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC";
   String user = "root";
   String password = "具体密码";
   Connection conn = DriverManager.getConnection(url, user, password);

补充:
如果还用Class.forName(“com.mysql.jdbc.Driver”);报错信息为:

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.
java.sql.SQLIntegrityConstraintViolationException: Duplicate entry '12' for key 'PRIMARY'
 at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:117)
 at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
 at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)
 at com.mysql.cj.jdbc.ClientPreparedStatement.executeInternal(ClientPreparedStatement.java:953)
 at com.mysql.cj.jdbc.ClientPreparedStatement.executeUpdateInternal(ClientPreparedStatement.java:1092)
 at com.mysql.cj.jdbc.ClientPreparedStatement.executeUpdateInternal(ClientPreparedStatement.java:1040)
 at com.mysql.cj.jdbc.ClientPreparedStatement.executeLargeUpdate(ClientPreparedStatement.java:1347)
 at com.mysql.cj.jdbc.ClientPreparedStatement.executeUpdate(ClientPreparedStatement.java:1025)
 at com.test.jdbc.day22.Day22Demo3.main(Day22Demo3.java:36)

如果只改了Class.forName(“com.mysql.cj.jdbc.Driver”);,而String url = “jdbc:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=utf-8"并没有添加”&useSSL=false&serverTimezone=UTC",则报错信息为:

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.
 at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129)
 at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
 at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:89)
 at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:63)
 at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:73)
 at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:76)
 at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:836)
 at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:456)
 at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:246)
 at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:199)
 at java.sql.DriverManager.getConnection(Unknown Source)
 at java.sql.DriverManager.getConnection(Unknown Source)
 at com.test.jdbc.day22.Day22Demo3.main(Day22Demo3.java:16)
Caused by: com.mysql.cj.exceptions.InvalidConnectionAttributeException: 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.
 at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
 at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
 at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
 at java.lang.reflect.Constructor.newInstance(Unknown Source)
 at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:61)
 at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:85)
 at com.mysql.cj.util.TimeUtil.getCanonicalTimezone(TimeUtil.java:132)
 at com.mysql.cj.protocol.a.NativeProtocol.configureTimezone(NativeProtocol.java:2121)
 at com.mysql.cj.protocol.a.NativeProtocol.initServerSession(NativeProtocol.java:2145)
 at com.mysql.cj.jdbc.ConnectionImpl.initializePropsFromServer(ConnectionImpl.java:1310)
 at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:967)
 at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:826)
 ... 6 more
Logo

更多推荐