Example usage for java.sql PreparedStatement setNClob

List of usage examples for java.sql PreparedStatement setNClob

Introduction

In this page you can find the example usage for java.sql PreparedStatement setNClob.

Prototype

void setNClob(int parameterIndex, Reader reader, long length) throws SQLException;

Source Link

Document

Sets the designated parameter to a Reader object.

Usage

From source file:org.springframework.jdbc.core.StatementCreatorUtils.java

private static void setValue(PreparedStatement ps, int paramIndex, int sqlType, @Nullable String typeName,
        @Nullable Integer scale, Object inValue) throws SQLException {

    if (inValue instanceof SqlTypeValue) {
        ((SqlTypeValue) inValue).setTypeValue(ps, paramIndex, sqlType, typeName);
    } else if (inValue instanceof SqlValue) {
        ((SqlValue) inValue).setValue(ps, paramIndex);
    } else if (sqlType == Types.VARCHAR || sqlType == Types.LONGVARCHAR) {
        ps.setString(paramIndex, inValue.toString());
    } else if (sqlType == Types.NVARCHAR || sqlType == Types.LONGNVARCHAR) {
        ps.setNString(paramIndex, inValue.toString());
    } else if ((sqlType == Types.CLOB || sqlType == Types.NCLOB) && isStringValue(inValue.getClass())) {
        String strVal = inValue.toString();
        if (strVal.length() > 4000) {
            // Necessary for older Oracle drivers, in particular when running against an Oracle 10 database.
            // Should also work fine against other drivers/databases since it uses standard JDBC 4.0 API.
            if (sqlType == Types.NCLOB) {
                ps.setNClob(paramIndex, new StringReader(strVal), strVal.length());
            } else {
                ps.setClob(paramIndex, new StringReader(strVal), strVal.length());
            }/*  www .j a  v  a 2s  .co  m*/
            return;
        } else {
            // Fallback: setString or setNString binding
            if (sqlType == Types.NCLOB) {
                ps.setNString(paramIndex, strVal);
            } else {
                ps.setString(paramIndex, strVal);
            }
        }
    } else if (sqlType == Types.DECIMAL || sqlType == Types.NUMERIC) {
        if (inValue instanceof BigDecimal) {
            ps.setBigDecimal(paramIndex, (BigDecimal) inValue);
        } else if (scale != null) {
            ps.setObject(paramIndex, inValue, sqlType, scale);
        } else {
            ps.setObject(paramIndex, inValue, sqlType);
        }
    } else if (sqlType == Types.BOOLEAN) {
        if (inValue instanceof Boolean) {
            ps.setBoolean(paramIndex, (Boolean) inValue);
        } else {
            ps.setObject(paramIndex, inValue, Types.BOOLEAN);
        }
    } else if (sqlType == Types.DATE) {
        if (inValue instanceof java.util.Date) {
            if (inValue instanceof java.sql.Date) {
                ps.setDate(paramIndex, (java.sql.Date) inValue);
            } else {
                ps.setDate(paramIndex, new java.sql.Date(((java.util.Date) inValue).getTime()));
            }
        } else if (inValue instanceof Calendar) {
            Calendar cal = (Calendar) inValue;
            ps.setDate(paramIndex, new java.sql.Date(cal.getTime().getTime()), cal);
        } else {
            ps.setObject(paramIndex, inValue, Types.DATE);
        }
    } else if (sqlType == Types.TIME) {
        if (inValue instanceof java.util.Date) {
            if (inValue instanceof java.sql.Time) {
                ps.setTime(paramIndex, (java.sql.Time) inValue);
            } else {
                ps.setTime(paramIndex, new java.sql.Time(((java.util.Date) inValue).getTime()));
            }
        } else if (inValue instanceof Calendar) {
            Calendar cal = (Calendar) inValue;
            ps.setTime(paramIndex, new java.sql.Time(cal.getTime().getTime()), cal);
        } else {
            ps.setObject(paramIndex, inValue, Types.TIME);
        }
    } else if (sqlType == Types.TIMESTAMP) {
        if (inValue instanceof java.util.Date) {
            if (inValue instanceof java.sql.Timestamp) {
                ps.setTimestamp(paramIndex, (java.sql.Timestamp) inValue);
            } else {
                ps.setTimestamp(paramIndex, new java.sql.Timestamp(((java.util.Date) inValue).getTime()));
            }
        } else if (inValue instanceof Calendar) {
            Calendar cal = (Calendar) inValue;
            ps.setTimestamp(paramIndex, new java.sql.Timestamp(cal.getTime().getTime()), cal);
        } else {
            ps.setObject(paramIndex, inValue, Types.TIMESTAMP);
        }
    } else if (sqlType == SqlTypeValue.TYPE_UNKNOWN || (sqlType == Types.OTHER
            && "Oracle".equals(ps.getConnection().getMetaData().getDatabaseProductName()))) {
        if (isStringValue(inValue.getClass())) {
            ps.setString(paramIndex, inValue.toString());
        } else if (isDateValue(inValue.getClass())) {
            ps.setTimestamp(paramIndex, new java.sql.Timestamp(((java.util.Date) inValue).getTime()));
        } else if (inValue instanceof Calendar) {
            Calendar cal = (Calendar) inValue;
            ps.setTimestamp(paramIndex, new java.sql.Timestamp(cal.getTime().getTime()), cal);
        } else {
            // Fall back to generic setObject call without SQL type specified.
            ps.setObject(paramIndex, inValue);
        }
    } else {
        // Fall back to generic setObject call with SQL type specified.
        ps.setObject(paramIndex, inValue, sqlType);
    }
}

From source file:org.wso2.carbon.dataservices.core.odata.RDBMSDataHandler.java

/**
 * This method bind values to prepared statement.
 *
 * @param type            data Type//from  www .  ja v a2  s  . c  om
 * @param value           String value
 * @param ordinalPosition Ordinal Position
 * @param sqlStatement    Statement
 * @throws SQLException
 * @throws ParseException
 * @throws ODataServiceFault
 */
private void bindValuesToPreparedStatement(int type, String value, int ordinalPosition,
        PreparedStatement sqlStatement) throws SQLException, ParseException, ODataServiceFault {
    byte[] data;
    try {
        switch (type) {
        case Types.INTEGER:
            if (value == null) {
                sqlStatement.setNull(ordinalPosition, type);
            } else {
                sqlStatement.setInt(ordinalPosition, ConverterUtil.convertToInt(value));
            }
            break;
        case Types.TINYINT:
            if (value == null) {
                sqlStatement.setNull(ordinalPosition, type);
            } else {
                sqlStatement.setByte(ordinalPosition, ConverterUtil.convertToByte(value));
            }
            break;
        case Types.SMALLINT:
            if (value == null) {
                sqlStatement.setNull(ordinalPosition, type);
            } else {
                sqlStatement.setShort(ordinalPosition, ConverterUtil.convertToShort(value));
            }
            break;
        case Types.DOUBLE:
            if (value == null) {
                sqlStatement.setNull(ordinalPosition, type);
            } else {
                sqlStatement.setDouble(ordinalPosition, ConverterUtil.convertToDouble(value));
            }
            break;
        case Types.VARCHAR:
            /* fall through */
        case Types.CHAR:
            /* fall through */
        case Types.LONGVARCHAR:
            if (value == null) {
                sqlStatement.setNull(ordinalPosition, type);
            } else {
                sqlStatement.setString(ordinalPosition, value);
            }
            break;
        case Types.CLOB:
            if (value == null) {
                sqlStatement.setNull(ordinalPosition, type);
            } else {
                sqlStatement.setClob(ordinalPosition, new BufferedReader(new StringReader(value)),
                        value.length());
            }
            break;
        case Types.BOOLEAN:
            /* fall through */
        case Types.BIT:
            if (value == null) {
                sqlStatement.setNull(ordinalPosition, type);
            } else {
                sqlStatement.setBoolean(ordinalPosition, ConverterUtil.convertToBoolean(value));
            }
            break;
        case Types.BLOB:
            /* fall through */
        case Types.LONGVARBINARY:
            if (value == null) {
                sqlStatement.setNull(ordinalPosition, type);
            } else {
                data = this.getBytesFromBase64String(value);
                sqlStatement.setBlob(ordinalPosition, new ByteArrayInputStream(data), data.length);
            }
            break;
        case Types.BINARY:
            /* fall through */
        case Types.VARBINARY:
            if (value == null) {
                sqlStatement.setNull(ordinalPosition, type);
            } else {
                data = this.getBytesFromBase64String(value);
                sqlStatement.setBinaryStream(ordinalPosition, new ByteArrayInputStream(data), data.length);
            }
            break;
        case Types.DATE:
            if (value == null) {
                sqlStatement.setNull(ordinalPosition, type);
            } else {
                sqlStatement.setDate(ordinalPosition, DBUtils.getDate(value));
            }
            break;
        case Types.DECIMAL:
            /* fall through */
        case Types.NUMERIC:
            if (value == null) {
                sqlStatement.setNull(ordinalPosition, type);
            } else {
                sqlStatement.setBigDecimal(ordinalPosition, ConverterUtil.convertToBigDecimal(value));
            }
            break;
        case Types.FLOAT:
            /* fall through */
        case Types.REAL:
            if (value == null) {
                sqlStatement.setNull(ordinalPosition, type);
            } else {
                sqlStatement.setFloat(ordinalPosition, ConverterUtil.convertToFloat(value));
            }
            break;
        case Types.TIME:
            if (value == null) {
                sqlStatement.setNull(ordinalPosition, type);
            } else {
                sqlStatement.setTime(ordinalPosition, DBUtils.getTime(value));
            }
            break;
        case Types.LONGNVARCHAR:
            /* fall through */
        case Types.NCHAR:
            /* fall through */
        case Types.NVARCHAR:
            if (value == null) {
                sqlStatement.setNull(ordinalPosition, type);
            } else {
                sqlStatement.setNString(ordinalPosition, value);
            }
            break;
        case Types.NCLOB:
            if (value == null) {
                sqlStatement.setNull(ordinalPosition, type);
            } else {
                sqlStatement.setNClob(ordinalPosition, new BufferedReader(new StringReader(value)),
                        value.length());
            }
            break;
        case Types.BIGINT:
            if (value == null) {
                sqlStatement.setNull(ordinalPosition, type);
            } else {
                sqlStatement.setLong(ordinalPosition, ConverterUtil.convertToLong(value));
            }
            break;
        case Types.TIMESTAMP:
            if (value == null) {
                sqlStatement.setNull(ordinalPosition, type);
            } else {
                sqlStatement.setTimestamp(ordinalPosition, DBUtils.getTimestamp(value));
            }
            break;
        default:
            if (value == null) {
                sqlStatement.setNull(ordinalPosition, type);
            } else {
                sqlStatement.setString(ordinalPosition, value);
            }
            break;
        }
    } catch (DataServiceFault e) {
        throw new ODataServiceFault(e, "Error occurred while binding values. :" + e.getMessage());
    }
}