springBoot初学-MySQL测试错误汇总

springBoot初学-MySQL测试错误汇总

初次学习使用springBoot将MySQL进行了简单的整合测试,针对其间出现的错误进行总结。

初版代码如下:

  1. application.properties文件

    #mySQL数据库配置信息  
    spring.datasource.url=jdbc:mysql://127.0.0.1:3306/springboottest 
    spring.datasource.username=root 
    spring.datasource.password=******* 
    spring.datasource.driver-class-name=com.mysql.jdbc.Driver
    
  2. 测试代码:

    @RunWith(SpringRunner.class) 
    @SpringBootTest
    
    public class BoottestApplicationTests {
    
        @Autowired
         private JdbcTemplate jdbcTemplate;
        @Test
        public void contextLoads() {
    
        }
    
        //Test注解来自于import org.junit.Test; 用于做单元测试
        @Test
        public void ConnectMySqlTest(){
            List<Map<String ,Object>> result = jdbcTemplate.queryForList("select * from spring_boot_test");
            System.out.println("query result is"+result.size());
            System.out.println("sucessful");
        }
    } 
    
    

一、运行时错误

启动执行后,控制台出现以下的错误信息:

org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to
obtain 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 'serverTime

原因:
因为mysql默认的时区和本地时区不一致导致的。

本次解决方案:
application.properties文件的spring.datasource.url参数的值中追加时区参数:

?serverTimezone=GMT

修改后

   #mySQL数据库配置信息  
    spring.datasource.url=jdbc:mysql://127.0.0.1:3306/springboottest?serverTimezone=GMT
    spring.datasource.username=root spring.datasource.password=******* 
    spring.datasource.driver-class-name=com.mysql.jdbc.Driver

server time zone相关错误即可解决。

二、编译及测试用例通过,但编译过程中控制台出现Loading class `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.

控制台的警告问题,由于使用的maven的jar包是最新版本的,而配置的驱动已经过时,系统不太推荐。
将application文件中的spring.datasource.driver-class-name修改为警告中推荐的‘com.mysql.cj.jdbc.Driver’即可。

以上。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值