1、org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'helloController': Unsatisfied dependency expressed through field 'connection'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'getConnection' defined in class path resource [cn/chinaunicom/Config/BeanConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [java.sql.Connection]: Factory method 'getConnection' threw exception; nested exception is 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.
2、Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'getConnection' defined in class path resource [cn/chinaunicom/Config/BeanConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [java.sql.Connection]: Factory method 'getConnection' threw exception; nested exception is 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.
這些報錯都是來自同一個錯誤就是設置serverTimezone=UTC。把這個設置好了就好了。
但是我有一點不清楚:
我明明在.yml文件里寫了這個配置,但是就讀不出來,就必須在當時用的時候協商寫好:
@Bean public Connection getConnection() throws ClassNotFoundException, SQLException { Class.forName("com.mysql.cj.jdbc.Driver"); Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/text?serverTimezone=UTC", "root", "chen792979650"); return connection; } }
我配置文件里面的東西
server: port: 9090 #配置內嵌服務器的端口 spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver #數據庫url url: jdbc:mysql://localhost:3306/text?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC # 數據庫訪問賬戶 username: root
明明我配置了的,並且訪問的時候也是用的9090端口,說明這個配置文件是生效的。但是為什么不行?
我猜測的原因:應該因為先后順序的原因,在我debug的時候直接先對上面的連接數據庫i的語句執行,再執行配置文件,但是再我先再連接數據庫操作的時候已經出了問題,直接就報錯了。