java - 无法连接mysql数据库

标签 java mysql spring

我在尝试连接到本地 mysql 数据库时遇到奇怪的异常。我正在构建 spring mvc 应用程序。

这就是我配置 DriverManagerDataSource 的方式:

@Bean
public DriverManagerDataSource dataSource(){
    DriverManagerDataSource driverManagerDataSource = new DriverManagerDataSource();
    driverManagerDataSource.setDriverClassName("com.mysql.jdbc.Driver");
    driverManagerDataSource.setUrl("jdbc:mysql://localhost:3306/test");
    driverManagerDataSource.setUsername("****");
    driverManagerDataSource.setPassword("****");
    return driverManagerDataSource;
}

这些是我尝试连接的日志详细信息:

    root cause

org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is java.sql.SQLException: The server time zone value '�rodkowoeuropejski czas letni' 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.
    org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:80)
    org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:394)
    org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:474)
    org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:484)
    com.packt.webstore.domain.repository.impl.CustomerRepositoryImpl.getAllCustomers(CustomerRepositoryImpl.java:35)
    com.packt.webstore.controller.HomeController.showCustomers(HomeController.java:36)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.lang.reflect.Method.invoke(Method.java:497)
    org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:215)
    org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
    org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:749)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:689)
    org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:938)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:870)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:852)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:624)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:731)

我发现这一定是mysql-connector-java的问题。我将版本从6.0.3更改为5.1.39。我刚刚更改了 pom.xml 文件中的版本

 <dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.39</version>
    </dependency>

最佳答案

将您的 jdbc url 替换为 jdbc:mysql://localhost:3306/db?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC 我希望它能起作用

关于java - 无法连接mysql数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38640883/

相关文章:

java - Selenium:在 Java 中使用 cssSelector 提取 div 的文本

java - 如何运行定时器?

mysql - 从表中获取 id

java - -Xlint 重新编译。为什么默认不开启?

java - JDom 如何注释现有行

javascript - 使用MySQL数据和PHP获取的Google图表?

PHP Mysql 安全密码

mysql - 在mysql中用数字和字符对列进行排序

spring - 如何将切面限制为用名称指定的 bean?

spring - 异步微服务通信如何从UI的 Angular 表现得更好?