来自深渊的祝福 2019-01-14 12:15 采纳率: 100%
浏览 3679
已采纳

为什么我用springboot连接mysql始终不成功?

之前按照教程写不能成功,现在直接复制别人完整的代码还是不行
一些配置
application.properties

spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
spring.datasource.url=jdbc:mysql://localhost:3306/test?useSSL=false&useUnicode=true&characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

UserMapper.java

@Mapper
public interface UserMapper {

    @Select("select * from user")
    List<User> findAll();

    @Insert("insert into user(username,password)values(#{username},#{password}")
    public int add(User user);
}


UserSrevice.java

@Service
public class UserService {
    @Autowired
    private UserMapper userMapper;

    public List<User> findAll(){


        return userMapper.findAll();
    }

    public int add(User user){

        return userMapper.add(user);

    }
}



每次刷新页面
图片说明

错误提示

2019-01-14 11:51:15.363 ERROR 752 --- [nio-8080-exec-1] o.a.tomcat.jdbc.pool.ConnectionPool      : Unable to create initial connections of pool.

java.sql.SQLException: Unknown character set index for field '255' received from server.
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:910) ~[mysql-connector-java-5.0.3.jar:na]
    at com.mysql.jdbc.Connection.getCharsetNameForIndex(Connection.java:3396) ~[mysql-connector-java-5.0.3.jar:na]
    at com.mysql.jdbc.Field.<init>(Field.java:191) ~[mysql-connector-java-5.0.3.jar:na]

    ERROR 752 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException: 
### Error querying database.  Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is java.sql.SQLException: Unknown character set index for field '255' received from server.
### The error may exist in com/polaris/springboot_mybatis_jsp/mapper/UserMapper.java (best guess)
### The error may involve com.polaris.springboot_mybatis_jsp.mapper.UserMapper.findAll
### The error occurred while executing a query
### Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is java.sql.SQLException: Unknown character set index for field '255' received from server.] with root cause

其中会指向这两行

(UserService.java:18)
return userMapper.findAll();

(UserController.java:24)
List<User> list = userService.findAll();

求大神解答

  • 写回答

1条回答 默认 最新

  • L星火燎原 2019-01-14 14:25
    关注

    原因是MySQL版本高,mysql的jar版本低造成mysql与jar的编码错乱引起的,你可以看看你的版本是多少?详细可以参考哈这个
    https://blog.csdn.net/qq_38674645/article/details/81105266

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥200 如何使用postGis实现最短领规划?
  • ¥15 pyinstaller打包错误
  • ¥20 cesm的气溶胶排放文件
  • ¥15 逐月累计,月份不连续,补齐月份
  • ¥15 应用简单的Python代码完成一个学生成绩管理系统
  • ¥15 用matlab求微分方程初值问题
  • ¥15 vscode下编写第三方库opencv与pcl代码时没有代码提示
  • ¥15 能够跑通不报错,如何解决?(标签-matlab)
  • ¥15 MOS在RDS较大,频率高时开关波形异常
  • ¥15 SCENIC分析报错求解答