2016-07-28 110 views
0

我收到一個奇怪的異常,試圖連接到本地mysql數據庫。我是建設春天mvc應用程序。不能連接到MySQL數據庫

這是我如何配置使用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-連接器 - java的。我將版本從6.0.3更改爲5.1.39。我只是在我的pom.xml文件改變了版本

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

您正在使用哪種版本的Oracle JDBC? – Julian

+0

好吧,要使用它,只需將'dependency'標籤添加到''pom.xml'文件中,Maven將爲您完成所有工作(如果您使用的是Maven) – Julian

+0

看來您所遇到的問題可能會已經與[this]相關(http://stackoverflow.com/questions/2805907/setting-session-timezone-with-spring-jdbc-oracle)。 – Julian

回答

1

通過 jdbc:mysql://localhost:3306/db?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC 更換你的JDBC URL我希望工程

0

根據堆棧errors.You應指定您的時區value.Add here是同一個問題。