|
16 | 16 |
|
17 | 17 | package org.springframework.jdbc.core;
|
18 | 18 |
|
| 19 | +import java.io.StringReader; |
19 | 20 | import java.io.StringWriter;
|
20 | 21 | import java.math.BigDecimal;
|
21 | 22 | import java.math.BigInteger;
|
|
24 | 25 | import java.sql.DatabaseMetaData;
|
25 | 26 | import java.sql.PreparedStatement;
|
26 | 27 | import java.sql.SQLException;
|
| 28 | +import java.sql.SQLFeatureNotSupportedException; |
27 | 29 | import java.sql.Types;
|
28 | 30 | import java.util.Arrays;
|
29 | 31 | import java.util.Calendar;
|
@@ -319,10 +321,33 @@ else if (inValue instanceof SqlValue) {
|
319 | 321 | ((SqlValue) inValue).setValue(ps, paramIndex);
|
320 | 322 | }
|
321 | 323 | else if (sqlType == Types.VARCHAR || sqlType == Types.NVARCHAR ||
|
322 |
| - sqlType == Types.LONGVARCHAR || sqlType == Types.LONGNVARCHAR || |
323 |
| - ((sqlType == Types.CLOB || sqlType == Types.NCLOB) && isStringValue(inValue.getClass()))) { |
| 324 | + sqlType == Types.LONGVARCHAR || sqlType == Types.LONGNVARCHAR) { |
324 | 325 | ps.setString(paramIndex, inValue.toString());
|
325 | 326 | }
|
| 327 | + else if ((sqlType == Types.CLOB || sqlType == Types.NCLOB) && isStringValue(inValue.getClass())) { |
| 328 | + String strVal = inValue.toString(); |
| 329 | + if (strVal.length() > 4000) { |
| 330 | + // Necessary for older Oracle drivers, in particular when running against an Oracle 10 database. |
| 331 | + // Should also work fine against other drivers/databases since it uses standard JDBC 4.0 API. |
| 332 | + try { |
| 333 | + if (sqlType == Types.NCLOB) { |
| 334 | + ps.setNClob(paramIndex, new StringReader(strVal), strVal.length()); |
| 335 | + } |
| 336 | + else { |
| 337 | + ps.setClob(paramIndex, new StringReader(strVal), strVal.length()); |
| 338 | + } |
| 339 | + return; |
| 340 | + } |
| 341 | + catch (AbstractMethodError err) { |
| 342 | + logger.debug("JDBC driver does not implement JDBC 4.0 'setClob(int, Reader, long)' method", err); |
| 343 | + } |
| 344 | + catch (SQLFeatureNotSupportedException ex) { |
| 345 | + logger.debug("JDBC driver does not support JDBC 4.0 'setClob(int, Reader, long)' method", ex); |
| 346 | + } |
| 347 | + } |
| 348 | + // Fallback: regular setString binding |
| 349 | + ps.setString(paramIndex, strVal); |
| 350 | + } |
326 | 351 | else if (sqlType == Types.DECIMAL || sqlType == Types.NUMERIC) {
|
327 | 352 | if (inValue instanceof BigDecimal) {
|
328 | 353 | ps.setBigDecimal(paramIndex, (BigDecimal) inValue);
|
|
0 commit comments