* @throws SQLException
*/
public static void setStatementValue(final PreparedStatement st,
final int valueIndex, final Column column, Object value)
throws SQLException {
final ColumnType type = (column == null ? null : column.getType());
if (type == null || type == ColumnType.OTHER) {
// type is not known - nothing more we can do to narrow the type
st.setObject(valueIndex, value);
return;
}
if (value == null && type != null) {
try {
final int jdbcType = type.getJdbcType();
st.setNull(valueIndex, jdbcType);
return;
} catch (Exception e) {
logger.warn(
"Exception occurred while calling setNull(...) for value index "
+ valueIndex
+ ". Attempting value-based setter method instead.",
e);
}
}
if (type == ColumnType.VARCHAR && value instanceof Date) {
// some drivers (SQLite and JTDS for MS SQL server) treat dates as
// VARCHARS. In that case we need to convert the dates to the
// correct format
String nativeType = column.getNativeType();
Date date = (Date) value;
if ("DATE".equalsIgnoreCase(nativeType)) {
value = FormatHelper
.formatSqlTime(ColumnType.DATE, date, false);
} else if ("TIME".equalsIgnoreCase(nativeType)) {
value = FormatHelper
.formatSqlTime(ColumnType.TIME, date, false);
} else if ("TIMESTAMP".equalsIgnoreCase(nativeType)
|| "DATETIME".equalsIgnoreCase(nativeType)) {
value = FormatHelper.formatSqlTime(ColumnType.TIMESTAMP, date,
false);
}
}
if (type != null && type.isTimeBased() && value instanceof String) {
value = FormatHelper.parseSqlTime(type, (String) value);
}
try {
if (type == ColumnType.DATE && value instanceof Date) {
Calendar cal = Calendar.getInstance();
cal.setTime((Date) value);
st.setDate(valueIndex,
new java.sql.Date(cal.getTimeInMillis()), cal);
} else if (type == ColumnType.TIME && value instanceof Date) {
Calendar cal = Calendar.getInstance();
cal.setTime((Date) value);
st.setTime(valueIndex,
new java.sql.Time(cal.getTimeInMillis()), cal);
} else if (type == ColumnType.TIMESTAMP && value instanceof Date) {
Calendar cal = Calendar.getInstance();
cal.setTime((Date) value);
st.setTimestamp(valueIndex,
new java.sql.Timestamp(cal.getTimeInMillis()), cal);
} else if (type == ColumnType.CLOB || type == ColumnType.NCLOB) {
if (value instanceof InputStream) {
InputStream inputStream = (InputStream) value;
st.setAsciiStream(valueIndex, inputStream);
} else if (value instanceof Reader) {
Reader reader = (Reader) value;
st.setCharacterStream(valueIndex, reader);
} else if (value instanceof NClob) {
NClob nclob = (NClob) value;
st.setNClob(valueIndex, nclob);
} else if (value instanceof Clob) {
Clob clob = (Clob) value;
st.setClob(valueIndex, clob);
} else if (value instanceof String) {
st.setString(valueIndex, (String) value);
} else {
st.setObject(valueIndex, value);
}
} else if (type == ColumnType.BLOB || type == ColumnType.BINARY) {
if (value instanceof byte[]) {
byte[] bytes = (byte[]) value;
st.setBytes(valueIndex, bytes);
} else if (value instanceof InputStream) {
InputStream inputStream = (InputStream) value;
st.setBinaryStream(valueIndex, inputStream);
} else if (value instanceof Blob) {
Blob blob = (Blob) value;
st.setBlob(valueIndex, blob);
} else {
st.setObject(valueIndex, value);
}
} else if (type.isLiteral()) {
final String str;
if (value instanceof Reader) {
Reader reader = (Reader) value;
str = FileHelper.readAsString(reader);
} else {