XML编码探究(一)
起因是用ibatis做数据库写操作时出现了如下编码错误,读操作不受影响,而代码、IDE、jvm的编码中唯一的改动就是jvm的文件读取编码从UTF-8改为了GB18030。
org.springframework.jdbc.UncategorizedSQLException: SqlMapClient operation; uncategorized SQLException for SQL []; SQL state [null]; error code [0];
--- The error occurred while applying a parameter map.
--- Check the WorkOrder.updateWorkOrder-InlineParameterMap.
--- Check the statement (update failed).
--- Cause: Line 8 in XML document from Byte array resource [resource loaded from byte array] is invalid; nested exception is org.xml.sax.SAXParseException; lineNumber: 8; columnNumber: 18; Invalid byte 1 of 1-byte UTF-8 sequence.; nested exception is com.ibatis.common.jdbc.exception.NestedSQLException:
--- The error occurred while applying a parameter map.
--- Check the WorkOrder.updateWorkOrder-InlineParameterMap.
--- Check the statement (update failed).
--- Cause: Line 8 in XML document from Byte array resource [resource loaded from byte array] is invalid; nested exception is org.xml.sax.SAXParseException; lineNumber: 8; columnNumber: 18; Invalid byte 1 of 1-byte UTF-8 sequence.
从字面上理解,错误信息是说SqlMapClient在操作sqlmap_WorkOrder.xml中的updateWorkOrder语句的parameter map时,发现由xml文件读取的UTF-8序列的第8行18列的字节是无效字节
通过分析编码发现,xml文件和eclipse工作空间都是UTF-8编码,即文件以UTF-8编码保存;
而jvm启动参数中,有一项-Dfile.encoding=GB18030,代表tomcat启动和运行时采用的编码。
当一个文件以UTF-8的方式存储,再以GBK或GB18030的编码读取时就会产生乱码,导致tomcat启动日志出现中文乱。
将-Dfile.encoding去掉则jvm会读取默认编码UTF-8,于是乱码消失。
而SQL执行出现报错,与数据库编码有关,在下一篇中分析。

浙公网安备 33010602011771号