Spring JDBC使用applicationContext.xml连接数据库报错You must configure either the server or JDBC driver....

Spring JDBC使用applicationContext.xml连接数据库报错You must configure either the server or JDBC driver…

  • 数据库 MySQL 8.0

JUnit5测试运行连接数据库时报了一个错:

org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; 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.
....

经过查资料和修改,运行成功

源码如下:
<?xml version="1.0" encoding="UTF-8"?>
<!-- <!DOCTYPE xml> -->
<beans xmlns="http://www.springframework.org/schema/beans" 
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
       xsi:schemaLocation="http://www.springframework.org/schema/beans  
                           http://www.springframework.org/schema/beans/spring-beans-4.3.xsd">
                           
	<!-- 1 配置数据源 -->
	<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<!-- 数据库驱动 -->
		<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
		<!-- 连接数据库的url -->
		<property name="url" value="jdbc:mysql://localhost/spring?serverTimezone=GMT%2B8&amp;allowMultiQueries=true&amp;useUnicode=true&amp;characterEncoding=UTF-8"/>
		<!-- jdbc:mysql://localhost:3306/test?&useSSL=false&serverTimezone=UTC
			jdbc:mysql://localhost/spring?allowMultiQueries=true&amp;useUnicode=true&amp;characterEncoding=UTF-8
		 -->
		<!-- 连接数据库的用户名 -->
		<property name="username" value="username"/>
		<!-- 连接数据库的密码 -->
		<property name="password" value="password"/>
	</bean>
	
	<!-- 2 配置JDBC模板 -->
	<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
		<!-- 默认必须使用数据源 -->
		<property name="dataSource" ref="dataSource"/>
	</bean>
	
</beans>
在连接数据库的url中jdbc:mysql://localhost/spring? 后添加 serverTimezone=GMT%2B8&amp
<!-- jdbc:mysql://localhost:3306/test?&useSSL=false&serverTimezone=UTC
			jdbc:mysql://localhost/spring?serverTimezone=GMT%2B8&amp?allowMultiQueries=true&amp;useUnicode=true&amp;characterEncoding=UTF-8
		 -->
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

FREE_QIU

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值