关于Emoji表情存入数据库报错的原因调查Cause: java.sql.SQLException: Incorrect string value: '\xF0\x9F\x8E\xB1' for

最近在做关于社交项目时,有一个发红包功能,其中涉及到红包描述,一直报错,后来看日志,才发现竟是因为输入了Emoji表情的原因,由于我的mysql数据库是utf8字符集,而且Emoji表情等特殊符号要占四个字节,所以导致数据库不能正常存入。
Caused by: java.sql.SQLException: Incorrect string value: ‘\xF0\x9F\x8E\xB1’ for column ‘redbag_describe’ at row 1
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:964) ~[mysql-connector-java-5.1.44.jar!/:5.1.44]
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3973) ~[mysql-connector-java-5.1.44.jar!/:5.1.44]
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3909) ~[mysql-connector-java-5.1.44.jar!/:5.1.44]
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2527) ~[mysql-connector-java-5.1.44.jar!/:5.1.44]
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2680) ~[mysql-connector-java-5.1.44.jar!/:5.1.44]
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2487) ~[mysql-connector-java-5.1.44.jar!/:5.1.44]
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1858) ~[mysql-connector-java-5.1.44.jar!/:5.1.44]
at com.mysql.jdbc.PreparedStatement.execute(PreparedStatement.java:1197) ~[mysql-connector-java-5.1.44.jar!/:5.1.44]
at sun.reflect.GeneratedMethodAccessor95.invoke(Unknown Source) ~[na:na]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_161]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_161]
at org.apache.tomcat.jdbc.pool.StatementFacade S t a t e m e n t P r o x y . i n v o k e ( S t a t e m e n t F a c a d e . j a v a : 114 )   [ t o m c a t − j d b c − 8.5.23. j a r ! / : n a ] a t c o m . s u n . p r o x y . StatementProxy.invoke(StatementFacade.java:114) ~[tomcat-jdbc-8.5.23.jar!/:na] at com.sun.proxy. StatementProxy.invoke(StatementFacade.java:114) [tomcatjdbc8.5.23.jar!/:na]atcom.sun.proxy.Proxy138.execute(Unknown Source) ~[na:na]
at sun.reflect.GeneratedMethodAccessor95.invoke(Unknown Source) ~[na:na]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_161]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_161]
at org.apache.ibatis.logging.jdbc.PreparedStatementLogger.invoke(PreparedStatementLogger.java:59) ~[mybatis-3.4.6.jar!/:3.4.6]
at com.sun.proxy. P r o x y 139. e x e c u t e ( U n k n o w n S o u r c e )   [ n a : n a ] a t o r g . a p a c h e . i b a t i s . e x e c u t o r . s t a t e m e n t . P r e p a r e d S t a t e m e n t H a n d l e r . u p d a t e ( P r e p a r e d S t a t e m e n t H a n d l e r . j a v a : 46 )   [ m y b a t i s − 3.4.6. j a r ! / : 3.4.6 ] a t o r g . a p a c h e . i b a t i s . e x e c u t o r . s t a t e m e n t . R o u t i n g S t a t e m e n t H a n d l e r . u p d a t e ( R o u t i n g S t a t e m e n t H a n d l e r . j a v a : 74 )   [ m y b a t i s − 3.4.6. j a r ! / : 3.4.6 ] a t o r g . a p a c h e . i b a t i s . e x e c u t o r . S i m p l e E x e c u t o r . d o U p d a t e ( S i m p l e E x e c u t o r . j a v a : 50 )   [ m y b a t i s − 3.4.6. j a r ! / : 3.4.6 ] a t o r g . a p a c h e . i b a t i s . e x e c u t o r . B a s e E x e c u t o r . u p d a t e ( B a s e E x e c u t o r . j a v a : 117 )   [ m y b a t i s − 3.4.6. j a r ! / : 3.4.6 ] a t o r g . a p a c h e . i b a t i s . e x e c u t o r . C a c h i n g E x e c u t o r . u p d a t e ( C a c h i n g E x e c u t o r . j a v a : 76 )   [ m y b a t i s − 3.4.6. j a r ! / : 3.4.6 ] a t o r g . a p a c h e . i b a t i s . s e s s i o n . d e f a u l t s . D e f a u l t S q l S e s s i o n . u p d a t e ( D e f a u l t S q l S e s s i o n . j a v a : 198 )   [ m y b a t i s − 3.4.6. j a r ! / : 3.4.6 ] a t o r g . a p a c h e . i b a t i s . s e s s i o n . d e f a u l t s . D e f a u l t S q l S e s s i o n . i n s e r t ( D e f a u l t S q l S e s s i o n . j a v a : 185 )   [ m y b a t i s − 3.4.6. j a r ! / : 3.4.6 ] a t s u n . r e f l e c t . G e n e r a t e d M e t h o d A c c e s s o r 261. i n v o k e ( U n k n o w n S o u r c e )   [ n a : n a ] a t s u n . r e f l e c t . D e l e g a t i n g M e t h o d A c c e s s o r I m p l . i n v o k e ( D e l e g a t i n g M e t h o d A c c e s s o r I m p l . j a v a : 43 )   [ n a : 1.8. 0 1 61 ] a t j a v a . l a n g . r e f l e c t . M e t h o d . i n v o k e ( M e t h o d . j a v a : 498 )   [ n a : 1.8. 0 1 61 ] a t o r g . m y b a t i s . s p r i n g . S q l S e s s i o n T e m p l a t e Proxy139.execute(Unknown Source) ~[na:na] at org.apache.ibatis.executor.statement.PreparedStatementHandler.update(PreparedStatementHandler.java:46) ~[mybatis-3.4.6.jar!/:3.4.6] at org.apache.ibatis.executor.statement.RoutingStatementHandler.update(RoutingStatementHandler.java:74) ~[mybatis-3.4.6.jar!/:3.4.6] at org.apache.ibatis.executor.SimpleExecutor.doUpdate(SimpleExecutor.java:50) ~[mybatis-3.4.6.jar!/:3.4.6] at org.apache.ibatis.executor.BaseExecutor.update(BaseExecutor.java:117) ~[mybatis-3.4.6.jar!/:3.4.6] at org.apache.ibatis.executor.CachingExecutor.update(CachingExecutor.java:76) ~[mybatis-3.4.6.jar!/:3.4.6] at org.apache.ibatis.session.defaults.DefaultSqlSession.update(DefaultSqlSession.java:198) ~[mybatis-3.4.6.jar!/:3.4.6] at org.apache.ibatis.session.defaults.DefaultSqlSession.insert(DefaultSqlSession.java:185) ~[mybatis-3.4.6.jar!/:3.4.6] at sun.reflect.GeneratedMethodAccessor261.invoke(Unknown Source) ~[na:na] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_161] at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_161] at org.mybatis.spring.SqlSessionTemplate Proxy139.execute(UnknownSource) [na:na]atorg.apache.ibatis.executor.statement.PreparedStatementHandler.update(PreparedStatementHandler.java:46) [mybatis3.4.6.jar!/:3.4.6]atorg.apache.ibatis.executor.statement.RoutingStatementHandler.update(RoutingStatementHandler.java:74) [mybatis3.4.6.jar!/:3.4.6]atorg.apache.ibatis.executor.SimpleExecutor.doUpdate(SimpleExecutor.java:50) [mybatis3.4.6.jar!/:3.4.6]atorg.apache.ibatis.executor.BaseExecutor.update(BaseExecutor.java:117) [mybatis3.4.6.jar!/:3.4.6]atorg.apache.ibatis.executor.CachingExecutor.update(CachingExecutor.java:76) [mybatis3.4.6.jar!/:3.4.6]atorg.apache.ibatis.session.defaults.DefaultSqlSession.update(DefaultSqlSession.java:198) [mybatis3.4.6.jar!/:3.4.6]atorg.apache.ibatis.session.defaults.DefaultSqlSession.insert(DefaultSqlSession.java:185) [mybatis3.4.6.jar!/:3.4.6]atsun.reflect.GeneratedMethodAccessor261.invoke(UnknownSource) [na:na]atsun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) [na:1.8.0161]atjava.lang.reflect.Method.invoke(Method.java:498) [na:1.8.0161]atorg.mybatis.spring.SqlSessionTemplateSqlSessionInterceptor.invoke(SqlSessionTemplate.java:433) ~[mybatis-spring-1.3.2.jar!/:1.3.2]
… 60 common frames omitted
找了很多方法,都没有从根源上解决问题,很是头疼。最后还是发现github上有个很牛逼的轻量级开源工具叫emoji-java,通过这个工具类基本上解决了我大部分的问题,同时配合前端限制Emoji表情输入,才算把问题解决。github地址:https://github.com/vdurmont/emoji-java。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值