我有 java 代码将 UTF-8 字符串修剪为我的 Oracle (11.2.0.4.0) 列的大小,最终抛出错误,因为 java 和 Oracle 将字符串视为不同的字节长度。我已经验证我NLS_CHARACTERSET
在 Oracle 中的参数是“UTF8”。
我写了一个测试,使用unicode 花栗鼠表情符号(?)
public void test() throws UnsupportedEncodingException, SQLException {
String squirrel = "\uD83D\uDC3F\uFE0F";
int squirrelByteLength = squirrel.getBytes("UTF-8").length; //this is 7
Connection connection = dataSource.getConnection();
connection.prepareStatement("drop table temp").execute();
connection.prepareStatement("create table temp (foo varchar2(" + String.valueOf(squirrelByteLength) + "))").execute();
PreparedStatement statement = connection.prepareStatement("insert into temp (foo) values (?)");
statement.setString(1, squirrel);
statement.executeUpdate();
}
Run Code Online (Sandbox Code Playgroud)
这在测试的最后一行失败,并显示以下消息:
ORA-12899: 列
"MYSCHEMA"."TEMP"."FOO" 的值太大(实际:9,最大值:7)
的设置NLS_LENGTH_SEMANTICS
是BYTE
。不幸的是,我无法改变它,因为它是一个遗留系统。我对增加列大小不感兴趣,只是能够可靠地预测字符串的 Oracle 大小。