I seem to have a problem with a Java batch insert. What it does is inserting values with single quotes into an Oracle table.
java.sql.BatchUpdateException: ORA-12899: value too large for column "P40_001_CM"."PRODUCTDATA"."P_NAME" (actual: 76, maximum: 75)
The fact is that the value I need to insert as P_NAME is :
Best of Hollywood 2 Movie Collector's Pack: Todeszug nach Yuma / Recue Dawn
As you can see (or count if you like); this is exactly 75 characters in length, including the single quote.
Code snippet I am using to handle single quotes :
case Types.VARCHAR:
pstmt.setString(paramIndex, param.getValue().replace("'", "''"));
break;
This approach works, and it does for most of my inserts, but in this particular case it will count the added single quote resulting the entire string to be 76 in length. And thus causes the exception.
Thing is that when I run the insert statement in Squirrel, it just inserts without any problems. And it should, the actual value is still 75 long, only one quote was added to escape another.
Somehow, the internally used OraclePreparedStatement does some kind of check (obviously not a very good one) on the length of the String that is passed and dismisses this before it can actually reach Oracle itself, or so it seems.
How can I fix this ? I can’t be the only one who ever ran into this behaviour ?
Ah, btw, before anyone suggests, yes, I have tried :
`insert into GERMANSTUFF values('Best of Hollywood 2 Movie Collector'||chr(39)||'s Pack: Todeszug nach Zuma / Recue Dawn');`
and
insert into GERMANSTUFF values(translate('Best of Hollywood 2 Movie Collector^s Pack: Todeszug nach Puma / Recue Dawn','^',chr(39)));
and although this works in Squirrel as a direct SQL statement, it only made the String value on which the OraclePreparedStatement throws an error larger. Which seems logical I guess.
I know I can replace the ' by a ^ or just enlarge my destination column, but I don’t consider these ‘solutions’ as the right way to solve this problem. Maybe it’s something simple which I just haven’t seen yet ?
AFAIK, I don’t think you need to handle the escaping yourself, especially if you’re using
PreparedStatement. Just doing the below should take care of the escaping internally.