使用 Types.NVARCHAR 和 oracle JDBC 驱动程序来处理西里尔字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4158129/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using Types.NVARCHAR with oracle JDBC driver to work with Cyrillic chars
提问by L. Holanda
I am trying to use the "New Methods for National Character Set Type Data in JDK 1.6", to get a standard JDBCsolution to handle cyrillic chars, but when the execution reaches any line with NVARCHAR type, for instance:
我正在尝试使用“JDK 1.6 中的国家字符集类型数据的新方法”来获得标准的 JDBC解决方案来处理西里尔字符,但是当执行到达 NVARCHAR 类型的任何行时,例如:
preparedSelect.setObject(3, "суббота", Types.NVARCHAR);
Then I get this exception:
然后我得到这个异常:
java.sql.SQLException: Invalid column type
at oracle.jdbc.driver.SQLStateMapping.newSQLException(SQLStateMapping.java:70)
at oracle.jdbc.driver.DatabaseError.newSQLException(DatabaseError.java:131)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:197)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:261)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:269)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:490)
at oracle.jdbc.driver.OraclePreparedStatement.setObjectCritical(OraclePreparedStatement.java:7922)
at oracle.jdbc.driver.OraclePreparedStatement.setObjectInternal(OraclePreparedStatement.java:7502)
at oracle.jdbc.driver.OraclePreparedStatement.setObject(OraclePreparedStatement.java:7975)
at oracle.jdbc.driver.OraclePreparedStatementWrapper.setObject(OraclePreparedStatementWrapper.java:222)
I also tried to use setNString() but I get an even more strange exception:
我也尝试使用 setNString() 但我得到了一个更奇怪的异常:
java.lang.AbstractMethodError: oracle.jdbc.driver.OraclePreparedStatementWrapper.setNString(ILjava/lang/String;)V
If I use java -Doracle.jdbc.defaultNChar=true myApplication with regular Types.VARCHAR, the Russian words are stored correctly. But using -Doracle.jdbc.defaultNChar=true is not an option since I'm working on a legacy application, I do not have control of running production environment, I'm just writing a component to it. Furthermore, this "Readme for NChar How-to"states that "This conversion has a substantial performance impact". So setting everything to NChar by default when only less than 1% of my tables needs this conversion in not a smart choice.
如果我将 java -Doracle.jdbc.defaultNChar=true myApplication 与常规 Types.VARCHAR 一起使用,则会正确存储俄语单词。但是使用 -Doracle.jdbc.defaultNChar=true 不是一个选项,因为我正在处理遗留应用程序,我无法控制运行生产环境,我只是向它编写一个组件。此外,此“NChar How-to 自述文件”指出“此转换对性能有重大影响”。因此,当我只有不到 1% 的表需要这种转换时,默认情况下将所有内容设置为 NChar,这不是一个明智的选择。
I'm using oracle thin driver and I have ojdbc6.jar and orai18n.jar in my classpath.
我正在使用 oracle 瘦驱动程序,并且我的类路径中有 ojdbc6.jar 和 orai18n.jar。
I'm looking for a standard JDBC solution. I can not use any methods or constants with "oracle" on them. OraclePreparedStatement is not an option for me.
我正在寻找标准的 JDBC 解决方案。我不能使用任何带有“oracle”的方法或常量。OraclePreparedStatement 不是我的选择。
I tried using Types.NVARCHAR with MSSQL Server and it runs fine.
我尝试将 Types.NVARCHAR 与 MSSQL Server 一起使用,并且运行良好。
采纳答案by L. Holanda
I found the solution!
我找到了解决方案!
I was using ojdbc 11.2.0.1. When I switched to 11.2.0.2, I could get setNString()
working properly.
But I'm still getting the same java.sql.SQLException: Invalid column type
if I use setObject()
with Type.NVARCHAR
. Shame on you Oracle...
我使用的是 ojdbc 11.2.0.1。当我切换到 11.2.0.2 时,我可以setNString()
正常工作。但是java.sql.SQLException: Invalid column type
如果我使用setObject()
with ,我仍然得到相同的结果Type.NVARCHAR
。为你的 Oracle 感到羞耻...
Anyway, the solution: switch to ojdbc 11.2.0.2
反正解决办法:切换到ojdbc 11.2.0.2
回答by Flavio
I managed to make it work some time ago with the following incantations. These methods are an excerpt from a bigger class.
前段时间我设法使用以下咒语使其工作。这些方法摘自一个更大的类。
import com.mchange.v2.c3p0.C3P0ProxyStatement;
import oracle.jdbc.OraclePreparedStatement;
static {
try {
SET_FORM_OF_USE_METHOD = OraclePreparedStatement.class.getDeclaredMethod("setFormOfUse", new Class[] { Integer.TYPE, Short.TYPE });
} catch (NoSuchMethodException ex) {
LOG.fatal("Can't find the setFormOfUse method", ex);
throw new ExceptionInInitializerError(ex);
}
}
public void nullSafeSet(PreparedStatement st, Object value, int index, SessionImplementor session) throws SQLException {
if (st instanceof OraclePreparedStatement) {
((OraclePreparedStatement)st).setFormOfUse(index, OraclePreparedStatement.FORM_NCHAR);
} else if (st instanceof C3P0ProxyStatement) {
try {
C3P0ProxyStatement c3p0St = (C3P0ProxyStatement) st;
c3p0St.rawStatementOperation(SET_FORM_OF_USE_METHOD, C3P0ProxyStatement.RAW_STATEMENT, new Object[]{index, OraclePreparedStatement.FORM_NCHAR});
} catch (IllegalAccessException ex) {
throw new UnexpectedException("Error calling setFormOfUse through C3P0", ex);
} catch (IllegalArgumentException ex) {
throw new UnexpectedException("Error calling setFormOfUse through C3P0", ex);
} catch (InvocationTargetException ex) {
throw new UnexpectedException("Error calling setFormOfUse through C3P0", ex);
}
} else {
throw new IllegalArgumentException("Unkown PreparedStatement implementation: " + st.getClass() + ". Maybe an unknown connection pool is hiding the OraclePreparedStatement?");
}
st.setString(index, (String) value);
}
The nullSafeSet method is structured to work directly on OraclePreparedStatement instances, or on C3P0ProxyStatement in case you have the C3P0 connection pool; other options will have to be used in case of different pools.
nullSafeSet 方法的结构是直接在 OraclePreparedStatement 实例上工作,或者在 C3P0ProxyStatement 上工作(如果您有 C3P0 连接池);如果池不同,则必须使用其他选项。
This method worked with the ojdbc14.jar from Oracle 10.2.0.4.
此方法适用于 Oracle 10.2.0.4 中的 ojdbc14.jar。