List of usage examples for java.sql Time valueOf
@SuppressWarnings("deprecation") public static Time valueOf(LocalTime time)
From source file:org.apache.cocoon.util.JDBCTypeConversions.java
/** * Set the Statement column so that the results are mapped correctly. * * @param statement the prepared statement * @param position the position of the column * @param value the value of the column//from w w w . j a v a 2s. com */ public static void setColumn(PreparedStatement statement, int position, Object value, Integer typeObject) throws Exception { if (value instanceof String) { value = ((String) value).trim(); } if (typeObject == null) { throw new SQLException("Can't set column because the type is unrecognized"); } if (value == null) { /** If the value is null, set the column value null and return **/ statement.setNull(position, typeObject.intValue()); return; } if ("".equals(value)) { switch (typeObject.intValue()) { case Types.CHAR: case Types.CLOB: case Types.VARCHAR: /** If the value is an empty string and the column is a string type, we can continue **/ break; default: /** If the value is an empty string and the column is something else, we treat it as a null value **/ statement.setNull(position, typeObject.intValue()); return; } } File file = null; int length = -1; InputStream asciiStream = null; //System.out.println("========================================================================"); //System.out.println("JDBCTypeConversions: setting type "+typeObject.intValue()); switch (typeObject.intValue()) { case Types.CLOB: //System.out.println("CLOB"); Clob clob = null; if (value instanceof Clob) { clob = (Clob) value; } else if (value instanceof File) { File asciiFile = (File) value; asciiStream = new BufferedInputStream(new FileInputStream(asciiFile)); length = (int) asciiFile.length(); clob = new ClobHelper(asciiStream, length); } else if (value instanceof Part) { Part anyFile = (Part) value; asciiStream = new BufferedInputStream(anyFile.getInputStream()); length = anyFile.getSize(); clob = new ClobHelper(asciiStream, length); } else if (value instanceof JDBCxlobHelper) { asciiStream = ((JDBCxlobHelper) value).inputStream; length = ((JDBCxlobHelper) value).length; clob = new ClobHelper(asciiStream, length); } else if (value instanceof Source) { asciiStream = ((Source) value).getInputStream(); length = (int) ((Source) value).getContentLength(); clob = new ClobHelper(asciiStream, length); } else { String asciiText = value.toString(); asciiStream = new ByteArrayInputStream(asciiText.getBytes()); length = asciiText.length(); clob = new ClobHelper(asciiStream, length); } statement.setClob(position, clob); break; case Types.CHAR: // simple large object, e.g. Informix's TEXT //System.out.println("CHAR"); if (value instanceof File) { File asciiFile = (File) value; asciiStream = new BufferedInputStream(new FileInputStream(asciiFile)); length = (int) asciiFile.length(); } else if (value instanceof JDBCxlobHelper) { asciiStream = ((JDBCxlobHelper) value).inputStream; length = ((JDBCxlobHelper) value).length; } else if (value instanceof Source) { asciiStream = ((Source) value).getInputStream(); length = (int) ((Source) value).getContentLength(); } else if (value instanceof Part) { Part anyFile = (Part) value; asciiStream = new BufferedInputStream(anyFile.getInputStream()); length = anyFile.getSize(); clob = new ClobHelper(asciiStream, length); } else { String asciiText = value.toString(); asciiStream = new BufferedInputStream(new ByteArrayInputStream(asciiText.getBytes())); length = asciiText.length(); } statement.setAsciiStream(position, asciiStream, length); break; case Types.BIGINT: //System.out.println("BIGINT"); BigDecimal bd = null; if (value instanceof BigDecimal) { bd = (BigDecimal) value; } else if (value instanceof Number) { bd = BigDecimal.valueOf(((Number) value).longValue()); } else { bd = new BigDecimal(value.toString()); } statement.setBigDecimal(position, bd); break; case Types.TINYINT: //System.out.println("TINYINT"); Byte b = null; if (value instanceof Byte) { b = (Byte) value; } else if (value instanceof Number) { b = new Byte(((Number) value).byteValue()); } else { b = new Byte(value.toString()); } statement.setByte(position, b.byteValue()); break; case Types.DATE: //System.out.println("DATE"); Date d = null; if (value instanceof Date) { d = (Date) value; } else if (value instanceof java.util.Date) { d = new Date(((java.util.Date) value).getTime()); } else if (value instanceof Calendar) { d = new Date(((Calendar) value).getTime().getTime()); } else { d = Date.valueOf(value.toString()); } statement.setDate(position, d); break; case Types.DOUBLE: //System.out.println("DOUBLE"); double db; if (value instanceof Number) { db = (((Number) value).doubleValue()); } else { db = Double.parseDouble(value.toString()); } statement.setDouble(position, db); break; case Types.FLOAT: //System.out.println("FLOAT"); float f; if (value instanceof Number) { f = (((Number) value).floatValue()); } else { f = Float.parseFloat(value.toString()); } statement.setFloat(position, f); break; case Types.NUMERIC: //System.out.println("NUMERIC"); long l; if (value instanceof Number) { l = (((Number) value).longValue()); } else { l = Long.parseLong(value.toString()); } statement.setLong(position, l); break; case Types.SMALLINT: //System.out.println("SMALLINT"); Short s = null; if (value instanceof Short) { s = (Short) value; } else if (value instanceof Number) { s = new Short(((Number) value).shortValue()); } else { s = new Short(value.toString()); } statement.setShort(position, s.shortValue()); break; case Types.TIME: //System.out.println("TIME"); Time t = null; if (value instanceof Time) { t = (Time) value; } else if (value instanceof java.util.Date) { t = new Time(((java.util.Date) value).getTime()); } else { t = Time.valueOf(value.toString()); } statement.setTime(position, t); break; case Types.TIMESTAMP: //System.out.println("TIMESTAMP"); Timestamp ts = null; if (value instanceof Time) { ts = (Timestamp) value; } else if (value instanceof java.util.Date) { ts = new Timestamp(((java.util.Date) value).getTime()); } else { ts = Timestamp.valueOf(value.toString()); } statement.setTimestamp(position, ts); break; case Types.ARRAY: //System.out.println("ARRAY"); statement.setArray(position, (Array) value); // no way to convert string to array break; case Types.STRUCT: //System.out.println("STRUCT"); case Types.OTHER: //System.out.println("OTHER"); statement.setObject(position, value); break; case Types.LONGVARBINARY: //System.out.println("LONGVARBINARY"); statement.setTimestamp(position, new Timestamp((new java.util.Date()).getTime())); break; case Types.VARCHAR: //System.out.println("VARCHAR"); statement.setString(position, value.toString()); break; case Types.BLOB: //System.out.println("BLOB"); if (value instanceof JDBCxlobHelper) { statement.setBinaryStream(position, ((JDBCxlobHelper) value).inputStream, ((JDBCxlobHelper) value).length); } else if (value instanceof Source) { statement.setBinaryStream(position, ((Source) value).getInputStream(), (int) ((Source) value).getContentLength()); } else { Blob blob = null; if (value instanceof Blob) { blob = (Blob) value; } else if (value instanceof File) { file = (File) value; blob = new BlobHelper(new FileInputStream(file), (int) file.length()); } else if (value instanceof String) { file = new File((String) value); blob = new BlobHelper(new FileInputStream(file), (int) file.length()); } else if (value instanceof Part) { Part anyFile = (Part) value; blob = new BlobHelper(new BufferedInputStream(anyFile.getInputStream()), anyFile.getSize()); } else { throw new SQLException("Invalid type for blob: " + value.getClass().getName()); } //InputStream input = new BufferedInputStream(new FileInputStream(file)); statement.setBlob(position, blob); } break; case Types.VARBINARY: //System.out.println("VARBINARY"); if (value instanceof JDBCxlobHelper) { statement.setBinaryStream(position, ((JDBCxlobHelper) value).inputStream, ((JDBCxlobHelper) value).length); } else if (value instanceof Source) { statement.setBinaryStream(position, ((Source) value).getInputStream(), (int) ((Source) value).getContentLength()); } else if (value instanceof Part) { statement.setBinaryStream(position, ((Part) value).getInputStream(), ((Part) value).getSize()); } else { if (value instanceof File) { file = (File) value; } else if (value instanceof String) { file = new File((String) value); } else { throw new SQLException("Invalid type for blob: " + value.getClass().getName()); } //InputStream input = new BufferedInputStream(new FileInputStream(file)); FileInputStream input = new FileInputStream(file); statement.setBinaryStream(position, input, (int) file.length()); } break; case Types.INTEGER: //System.out.println("INTEGER"); Integer i = null; if (value instanceof Integer) { i = (Integer) value; } else if (value instanceof Number) { i = new Integer(((Number) value).intValue()); } else { i = new Integer(value.toString()); } statement.setInt(position, i.intValue()); break; case Types.BIT: //System.out.println("BIT"); Boolean bo = null; if (value instanceof Boolean) { bo = (Boolean) value; } else if (value instanceof Number) { bo = BooleanUtils.toBooleanObject(((Number) value).intValue() == 1); } else { bo = BooleanUtils.toBooleanObject(value.toString()); } statement.setBoolean(position, bo.booleanValue()); break; default: //System.out.println("default"); throw new SQLException("Impossible exception - invalid type "); } //System.out.println("========================================================================"); }
From source file:org.apache.ddlutils.model.Column.java
/** * Tries to parse the default value of the column and returns it as an object of the * corresponding java type. If the value could not be parsed, then the original * definition is returned./* w w w .j av a 2 s .co m*/ * * @return The parsed default value */ public Object getParsedDefaultValue() { if ((_defaultValue != null) && (_defaultValue.length() > 0)) { try { switch (_typeCode) { case Types.TINYINT: case Types.SMALLINT: return new Short(_defaultValue); case Types.INTEGER: return new Integer(_defaultValue); case Types.BIGINT: return new Long(_defaultValue); case Types.DECIMAL: case Types.NUMERIC: return new BigDecimal(_defaultValue); case Types.REAL: return new Float(_defaultValue); case Types.DOUBLE: case Types.FLOAT: return new Double(_defaultValue); case Types.DATE: return Date.valueOf(_defaultValue); case Types.TIME: return Time.valueOf(_defaultValue); case Types.TIMESTAMP: return Timestamp.valueOf(_defaultValue); case Types.BIT: case Types.BOOLEAN: return ConvertUtils.convert(_defaultValue, Boolean.class); } } catch (NumberFormatException ex) { return null; } catch (IllegalArgumentException ex) { return null; } } return _defaultValue; }
From source file:org.apache.ddlutils.platform.DefaultValueHelper.java
/** * Converts the given default value from the specified original to the target * jdbc type./*from w w w. j av a 2s . c o m*/ * * @param defaultValue The default value * @param originalTypeCode The original type code * @param targetTypeCode The target type code * @return The converted default value */ public String convert(String defaultValue, int originalTypeCode, int targetTypeCode) { String result = defaultValue; if (defaultValue != null) { switch (originalTypeCode) { case Types.BIT: case Types.BOOLEAN: result = convertBoolean(defaultValue, targetTypeCode).toString(); break; case Types.DATE: if (targetTypeCode == Types.TIMESTAMP) { try { Date date = Date.valueOf(result); return new Timestamp(date.getTime()).toString(); } catch (IllegalArgumentException ex) { } } break; case Types.TIME: if (targetTypeCode == Types.TIMESTAMP) { try { Time time = Time.valueOf(result); return new Timestamp(time.getTime()).toString(); } catch (IllegalArgumentException ex) { } } break; } } return result; }
From source file:org.apache.ddlutils.platform.oracle.Oracle8ModelReader.java
/** * {@inheritDoc}//ww w . j a v a 2s .c o m */ protected Column readColumn(DatabaseMetaDataWrapper metaData, Map values) throws SQLException { Column column = super.readColumn(metaData, values); if (column.getDefaultValue() != null) { // Oracle pads the default value with spaces column.setDefaultValue(column.getDefaultValue().trim()); } if (column.getTypeCode() == Types.DECIMAL) { // We're back-mapping the NUMBER columns returned by Oracle // Note that the JDBC driver returns DECIMAL for these NUMBER columns switch (column.getSizeAsInt()) { case 1: if (column.getScale() == 0) { column.setTypeCode(Types.BIT); } break; case 3: if (column.getScale() == 0) { column.setTypeCode(Types.TINYINT); } break; case 5: if (column.getScale() == 0) { column.setTypeCode(Types.SMALLINT); } break; case 18: column.setTypeCode(Types.REAL); break; case 22: if (column.getScale() == 0) { column.setTypeCode(Types.INTEGER); } break; case 38: if (column.getScale() == 0) { column.setTypeCode(Types.BIGINT); } else { column.setTypeCode(Types.DOUBLE); } break; } } else if (column.getTypeCode() == Types.FLOAT) { // Same for REAL, FLOAT, DOUBLE PRECISION, which all back-map to FLOAT but with // different sizes (63 for REAL, 126 for FLOAT/DOUBLE PRECISION) switch (column.getSizeAsInt()) { case 63: column.setTypeCode(Types.REAL); break; case 126: column.setTypeCode(Types.DOUBLE); break; } } else if ((column.getTypeCode() == Types.DATE) || (column.getTypeCode() == Types.TIMESTAMP)) { // Oracle has only one DATE/TIME type, so we can't know which it is and thus map // it back to TIMESTAMP column.setTypeCode(Types.TIMESTAMP); // we also reverse the ISO-format adaptation, and adjust the default value to timestamp if (column.getDefaultValue() != null) { Matcher matcher = _oracleIsoTimestampPattern.matcher(column.getDefaultValue()); Timestamp timestamp = null; if (matcher.matches()) { String timestampVal = matcher.group(1); timestamp = Timestamp.valueOf(timestampVal); } else { matcher = _oracleIsoDatePattern.matcher(column.getDefaultValue()); if (matcher.matches()) { String dateVal = matcher.group(1); timestamp = new Timestamp(Date.valueOf(dateVal).getTime()); } else { matcher = _oracleIsoTimePattern.matcher(column.getDefaultValue()); if (matcher.matches()) { String timeVal = matcher.group(1); timestamp = new Timestamp(Time.valueOf(timeVal).getTime()); } } } if (timestamp != null) { column.setDefaultValue(timestamp.toString()); } } } else if (TypeMap.isTextType(column.getTypeCode())) { column.setDefaultValue(unescape(column.getDefaultValue(), "'", "''")); } return column; }
From source file:org.apache.ignite.internal.processors.query.h2.database.InlineIndexHelperTest.java
/** */ public void testTime() throws Exception { testPutGet(ValueTime.get(Time.valueOf("10:01:01")), ValueTime.get(Time.valueOf("11:02:02")), ValueTime.get(Time.valueOf("12:03:03"))); }
From source file:org.apache.jmeter.protocol.jdbc.AbstractJDBCTestElement.java
private void setArgument(PreparedStatement pstmt, String argument, int targetSqlType, int index) throws SQLException { switch (targetSqlType) { case Types.INTEGER: pstmt.setInt(index, Integer.parseInt(argument)); break;//from w w w . ja v a2s . c om case Types.DECIMAL: case Types.NUMERIC: pstmt.setBigDecimal(index, new BigDecimal(argument)); break; case Types.DOUBLE: case Types.FLOAT: pstmt.setDouble(index, Double.parseDouble(argument)); break; case Types.CHAR: case Types.LONGVARCHAR: case Types.VARCHAR: pstmt.setString(index, argument); break; case Types.BIT: case Types.BOOLEAN: pstmt.setBoolean(index, Boolean.parseBoolean(argument)); break; case Types.BIGINT: pstmt.setLong(index, Long.parseLong(argument)); break; case Types.DATE: pstmt.setDate(index, Date.valueOf(argument)); break; case Types.REAL: pstmt.setFloat(index, Float.parseFloat(argument)); break; case Types.TINYINT: pstmt.setByte(index, Byte.parseByte(argument)); break; case Types.SMALLINT: pstmt.setShort(index, Short.parseShort(argument)); break; case Types.TIMESTAMP: pstmt.setTimestamp(index, Timestamp.valueOf(argument)); break; case Types.TIME: pstmt.setTime(index, Time.valueOf(argument)); break; case Types.BINARY: case Types.VARBINARY: case Types.LONGVARBINARY: pstmt.setBytes(index, argument.getBytes()); break; case Types.NULL: pstmt.setNull(index, targetSqlType); break; default: pstmt.setObject(index, argument, targetSqlType); } }
From source file:org.apache.kylin.jdbc.KylinClient.java
public static Object wrapObject(String value, int sqlType) { if (null == value) { return null; }/* w ww . java 2s .com*/ switch (sqlType) { case Types.CHAR: case Types.VARCHAR: case Types.LONGVARCHAR: return value; case Types.NUMERIC: case Types.DECIMAL: return new BigDecimal(value); case Types.BIT: return Boolean.parseBoolean(value); case Types.TINYINT: return Byte.valueOf(value); case Types.SMALLINT: return Short.valueOf(value); case Types.INTEGER: return Integer.parseInt(value); case Types.BIGINT: return Long.parseLong(value); case Types.FLOAT: return Float.parseFloat(value); case Types.REAL: case Types.DOUBLE: return Double.parseDouble(value); case Types.BINARY: case Types.VARBINARY: case Types.LONGVARBINARY: return value.getBytes(); case Types.DATE: return Date.valueOf(value); case Types.TIME: return Time.valueOf(value); case Types.TIMESTAMP: return Timestamp.valueOf(value); default: //do nothing break; } return value; }
From source file:org.apache.kylin.rest.service.QueryService.java
/** * @param preparedState/*w w w . j av a2 s.c o m*/ * @param param * @throws SQLException */ private void setParam(PreparedStatement preparedState, int index, PrepareSqlRequest.StateParam param) throws SQLException { boolean isNull = (null == param.getValue()); Class<?> clazz; try { clazz = Class.forName(param.getClassName()); } catch (ClassNotFoundException e) { throw new InternalErrorException(e); } Rep rep = Rep.of(clazz); switch (rep) { case PRIMITIVE_CHAR: case CHARACTER: case STRING: preparedState.setString(index, isNull ? null : String.valueOf(param.getValue())); break; case PRIMITIVE_INT: case INTEGER: preparedState.setInt(index, isNull ? 0 : Integer.valueOf(param.getValue())); break; case PRIMITIVE_SHORT: case SHORT: preparedState.setShort(index, isNull ? 0 : Short.valueOf(param.getValue())); break; case PRIMITIVE_LONG: case LONG: preparedState.setLong(index, isNull ? 0 : Long.valueOf(param.getValue())); break; case PRIMITIVE_FLOAT: case FLOAT: preparedState.setFloat(index, isNull ? 0 : Float.valueOf(param.getValue())); break; case PRIMITIVE_DOUBLE: case DOUBLE: preparedState.setDouble(index, isNull ? 0 : Double.valueOf(param.getValue())); break; case PRIMITIVE_BOOLEAN: case BOOLEAN: preparedState.setBoolean(index, !isNull && Boolean.parseBoolean(param.getValue())); break; case PRIMITIVE_BYTE: case BYTE: preparedState.setByte(index, isNull ? 0 : Byte.valueOf(param.getValue())); break; case JAVA_UTIL_DATE: case JAVA_SQL_DATE: preparedState.setDate(index, isNull ? null : java.sql.Date.valueOf(param.getValue())); break; case JAVA_SQL_TIME: preparedState.setTime(index, isNull ? null : Time.valueOf(param.getValue())); break; case JAVA_SQL_TIMESTAMP: preparedState.setTimestamp(index, isNull ? null : Timestamp.valueOf(param.getValue())); break; default: preparedState.setObject(index, isNull ? null : param.getValue()); } }
From source file:org.apache.ojb.broker.accesslayer.conversions.TimeList2VarcharFieldConversion.java
public Object sqlToJava(Object source) throws ConversionException { if (source == null) { return null; }/*ww w .java 2s . com*/ if (source.toString().equals(NULLVALUE)) { return null; } if (source.toString().equals(EMPTYCOLLEC)) { return new ArrayList(); } List v = new ArrayList(); String input = source.toString(); int pos = input.indexOf("#"); while (pos >= 0) { if (pos == 0) { v.add(""); } else { v.add(Time.valueOf(input.substring(0, pos))); } if (pos + 1 > input.length()) { //# at end causes outof bounds break; } input = input.substring(pos + 1, input.length()); pos = input.indexOf("#"); } return v; }
From source file:org.apache.openjpa.jdbc.schema.Column.java
/** * Return the default value set for this column, if any. If only a default * string has been set, attempts to convert it to the right type based * on the Java type set for this column. *//*from ww w . j a va 2 s . c om*/ public Object getDefault() { if (_default != null) return _default; if (_defaultStr == null) return null; switch (_javaType) { case JavaTypes.BOOLEAN: case JavaTypes.BOOLEAN_OBJ: _default = ("true".equals(_defaultStr)) ? Boolean.TRUE : Boolean.FALSE; break; case JavaTypes.BYTE: case JavaTypes.BYTE_OBJ: _default = new Byte(_defaultStr); break; case JavaTypes.CHAR: case JavaTypes.CHAR_OBJ: _default = Character.valueOf(_defaultStr.charAt(0)); break; case JavaTypes.DOUBLE: case JavaTypes.DOUBLE_OBJ: _default = new Double(_defaultStr); break; case JavaTypes.FLOAT: case JavaTypes.FLOAT_OBJ: _default = new Float(_defaultStr); break; case JavaTypes.INT: case JavaTypes.INT_OBJ: _default = Integer.parseInt(_defaultStr); break; case JavaTypes.LONG: case JavaTypes.LONG_OBJ: _default = Long.parseLong(_defaultStr); break; case JavaTypes.NUMBER: case JavaTypes.BIGDECIMAL: _default = new BigDecimal(_defaultStr); break; case JavaTypes.SHORT: case JavaTypes.SHORT_OBJ: _default = new Short(_defaultStr); break; case JavaTypes.DATE: _default = new java.util.Date(_defaultStr); break; case JavaTypes.BIGINTEGER: _default = new BigInteger(_defaultStr); break; case JavaSQLTypes.SQL_DATE: _default = Date.valueOf(_defaultStr); break; case JavaSQLTypes.TIMESTAMP: _default = Timestamp.valueOf(_defaultStr); break; case JavaSQLTypes.TIME: _default = Time.valueOf(_defaultStr); break; default: _default = _defaultStr; } return _default; }