java同步mysql的数据到PostgreSQL时报错ERROR: invalid byte sequence for encoding "UTF8": 0x00

发布时间 2023-05-31 10:47:47作者: 一曲终两人遇

最近,同事在做一个功能,通过java程序将mysql中的一张表的数据同步到pgsql中,在同步过程中,插入到pgsql中出现了如下错误:
`### Error updating database. Cause: org.postgresql.util.PSQLException: ERROR: invalid byte sequence for encoding "UTF8": 0x00
在位置:unnamed portal parameter $13

The error may involve com.absin.modules.shop.dao.UprodCollectionProductDao.insert-Inline

The error occurred while setting parameters

SQL: INSERT INTO uprod_collection_product ( "center_id", "create_uid", "write_uid", "name", "state", "manufacturer_code", "brand", "erp_category", "univ_category_id", "official_website_url", "specs", "futures", "remark", "is_trial", "molecular_weight_non_ab", "storage_condition", "price", "sales_discount_type", "cas_code", "specification_url", "dangerous_goods", "other_name", "references", "molecular_formula", "appearance", "solubility", "business_type", "proxy_area", "authorized_customer_type", "working_hours", "classify_id", "need_login", "supplier_name", "supplier_code", "laboratory_level", "root_code", "is_delete", "search_keywords", "create_date", "write_date", "absin_json_info" ) VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )

Cause: org.postgresql.util.PSQLException: ERROR: invalid byte sequence for encoding "UTF8": 0x00

在位置:unnamed portal parameter $13
; ERROR: invalid byte sequence for encoding "UTF8": 0x00
在位置:unnamed portal parameter $13; nested exception is org.postgresql.util.PSQLException: ERROR: invalid byte sequence for encoding "UTF8": 0x00
在位置:unnamed portal parameter $13`

和他一起通过查看一些资料,和咨询了公司的DBA定位到了问题,问题出现的原因是mysql的表中有些字段有转义字符 \0, 导致获取到数据后同步到pgsql中报错。
解决方法:在java中将从mysql中获取到的String类型的字段里的 \0 替换成 ""(空字符串):str.replaceAll("\0",""); 然后再插入到pgsql中。