将Java字符对象值插入到Oracle CHAR(1)列中时出错 [英] Error Inserting Java Character object value into Oracle CHAR(1) column

查看:787
本文介绍了将Java字符对象值插入到Oracle CHAR(1)列中时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Spring jdbcTemplate.update(String sql,Object [] args)在Oracle数据库上执行准备的insert语句。其中一个对象是包含值Y的字符对象,目标列是CHAR(1)类型,但我收到一个

I'm using a Spring jdbcTemplate.update(String sql, Object[] args) to execute a prepared insert statement on an Oracle database. One of the objects is a Character object containing the value 'Y', and the target column is of CHAR(1) type, but I'm receiving a

java.sql.SQLException: Invalid column type

我已经向前和向后调试,毫无疑问,这是导致问题的特定对象。当省略此字符对象时,插入将按预期执行。

I've debugged this backwards and forwards and there is no doubt that it is this one particular object that is causing the problem. The insert executes as expected when this Character Object is omitted.

我也可以输出sql和Object []值,将sql复制到sql开发人员中,替换值占位符(?'s) / code>与对象的实际值,并且插入将工作正常。

I can also output the sql and Object[] values, copy the sql into sql developer, replace the value placeholders (?'s) with the actual values of the Objects, and the insert will work fine.

sql(混淆保护有罪):

The sql (obfuscated to protect the guilty):

INSERT INTO SCHEMA.TABLE(NUMBER_COLUMN,VARCHAR_COLUMN,DATE_COLUMN,CHAR_COLUMN) VALUES (?,?,?,?);

对象值:

values[0] = [123]
values[1] = [Some String]
values[2] = [2012-04-19]
values[3] = [Y]

在sql开发人员手动运行组合, p>

The combination run manually in sql developer and that works just fine:

INSERT INTO SCHEMA.TABLE(NUMBER_COLUMN,VARCHAR_COLUMN,DATE_COLUMN,CHAR_COLUMN) VALUES (123,'Some String','19-Apr-2012','Y');

准备语句sql本身是基于数据中包含的非空实例变量对象动态生成的传递对象(我们希望数据库处理默认值的生成),所以我不能接受任何回答建议我只是重做sql或插入例程。

The prepared statement sql itself is generated dynamically based on the non-null instance variable objects contained within a data transfer object (we want the database to handle generation of default values), so I can't accept any answers suggesting that I just rework the sql or insertion routine.

任何人曾经遇到过这种情况,可以向我解释发生了什么,以及如何解决它?这是令人沮丧的bizzare,我似乎不能插入一个字符对象到CHAR(1)字段。

Anyone ever encountered this and can explain to me what's going on and how to fix it? It's frustratingly bizzare that I can't seem to insert a Character object into a CHAR(1) field. Any help would be much appreciated.

此致,Longtime Lurker首次海报

Sincerely, Longtime Lurker First-time Poster

推荐答案

没有采用字符值的PreparedStatement.setXxx(),Oracle文档声明所有JDBC字符类型都映射到Java字符串。另外,请参见 http://docs.oracle。 com / javase / 1.3 / docs / guide / jdbc / getstart / mapping.html#1039196 ,其中不包含来自Java char Character 到JDBC类型。

There is no PreparedStatement.setXxx() that takes a character value, and the Oracle docs states that all JDBC character types map to Java Strings. Also, see http://docs.oracle.com/javase/1.3/docs/guide/jdbc/getstart/mapping.html#1039196, which does not include a mapping from Java char or Character to a JDBC type.

您必须将值转换为String。

You will have to convert the value to a String.

这篇关于将Java字符对象值插入到Oracle CHAR(1)列中时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆