static void setParam(PreparedStatement stmt, int paramIndex, Date value) throws SQLException { if (value == null) { stmt.setNull(paramIndex, Types.TIMESTAMP); } else { stmt.setTimestamp(paramIndex, new Timestamp(value.getTime())); } }
@Override public void setValues(PreparedStatement ps) throws SQLException { ps.setTimestamp(1, new Timestamp(new Date().getTime())); } });
private static Object convertDateValue(final Object value, final Class<?> convertType) { Date date = (Date) value; switch (convertType.getName()) { case "java.sql.Date": return new java.sql.Date(date.getTime()); case "java.sql.Time": return new Time(date.getTime()); case "java.sql.Timestamp": return new Timestamp(date.getTime()); default: throw new ShardingException("Unsupported Date type:%s", convertType); } } }
@Test public void testSetParameterValueWithDateAndUnknownType() throws SQLException { java.util.Date date = new java.util.Date(1000); StatementCreatorUtils.setParameterValue(preparedStatement, 1, SqlTypeValue.TYPE_UNKNOWN, null, date); verify(preparedStatement).setTimestamp(1, new java.sql.Timestamp(1000)); }
ps.setString(paramIndex, inValue.toString()); ps.setString(paramIndex, strVal); ps.setDate(paramIndex, new java.sql.Date(((java.util.Date) inValue).getTime())); ps.setDate(paramIndex, new java.sql.Date(cal.getTime().getTime()), cal); ps.setTime(paramIndex, new java.sql.Time(((java.util.Date) inValue).getTime())); ps.setTime(paramIndex, new java.sql.Time(cal.getTime().getTime()), cal); if (inValue instanceof java.util.Date) { if (inValue instanceof java.sql.Timestamp) { ps.setTimestamp(paramIndex, (java.sql.Timestamp) inValue); ps.setTimestamp(paramIndex, new java.sql.Timestamp(((java.util.Date) inValue).getTime())); ps.setTimestamp(paramIndex, new java.sql.Timestamp(cal.getTime().getTime()), cal); "Oracle".equals(ps.getConnection().getMetaData().getDatabaseProductName()))) { if (isStringValue(inValue.getClass())) { ps.setString(paramIndex, inValue.toString()); ps.setTimestamp(paramIndex, new java.sql.Timestamp(((java.util.Date) inValue).getTime())); ps.setTimestamp(paramIndex, new java.sql.Timestamp(cal.getTime().getTime()), cal);
@Test public void testRow() { Row r1 = new Row(); r1.addField(new Field(AccumuloRowSerializer.getBlockFromArray(VARCHAR, ImmutableList.of("a", "b", "c")), new ArrayType(VARCHAR))); r1.addField(true, BOOLEAN); r1.addField(new Field(new Date(new GregorianCalendar(1999, 0, 1).getTime().getTime()), DATE)); r1.addField(123.45678, DOUBLE); r1.addField(new Field(123.45678f, REAL)); r1.addField(12345678, INTEGER); r1.addField(new Field(12345678L, BIGINT)); r1.addField(new Field((short) 12345, SMALLINT)); r1.addField(new GregorianCalendar(1999, 0, 1, 12, 30, 0).getTime().getTime(), TIME); r1.addField(new Field(new Timestamp(new GregorianCalendar(1999, 0, 1, 12, 30, 0).getTime().getTime()), TIMESTAMP)); r1.addField((byte) 123, TINYINT); r1.addField(new Field("O'Leary".getBytes(UTF_8), VARBINARY)); r1.addField("O'Leary", VARCHAR); r1.addField(null, VARCHAR); assertEquals(r1.length(), 14); assertEquals(r1.toString(), "(ARRAY ['a','b','c'],true,DATE '1999-01-01',123.45678,123.45678,12345678,12345678,12345,TIME '12:30:00',TIMESTAMP '1999-01-01 12:30:00.0',123,CAST('O''Leary' AS VARBINARY),'O''Leary',null)"); Row r2 = new Row(r1); assertEquals(r2, r1); }
try { if (java.sql.Date.class.equals(type)) { return new java.sql.Date(convertTimestamp2TimeMillis(value.toString())); } else if (java.sql.Time.class.equals(type)) { return new java.sql.Time(convertTimestamp2TimeMillis(value.toString())); } else if (Timestamp.class.equals(type)) { return new Timestamp(convertTimestamp2TimeMillis(value.toString())); } else { return new Timestamp(convertTimestamp2TimeMillis(value.toString()));
public Map<FileHistoryId, List<FileVersion>> getFileHistoriesToPurgeInInterval(long beginTimestamp, long endTimestamp, TimeUnit timeUnit) { try (PreparedStatement preparedStatement = getStatement("fileversion.select.all.getPurgeVersionsByInterval.sql")) { String timeUnitIdentifier = timeUnitSqlTimeUnitMap.get(timeUnit); preparedStatement.setString(1, timeUnitIdentifier); preparedStatement.setTimestamp(2, new Timestamp(beginTimestamp)); preparedStatement.setTimestamp(3, new Timestamp(endTimestamp)); return getAllVersionsInQuery(preparedStatement); } catch (SQLException e) { throw new RuntimeException(e); } }
@Test public void testTimestampsInResponse() throws Exception { final ResultSet resultSet = client.createStatement().executeQuery( "SELECT __time, CAST(__time AS DATE) AS t2 FROM druid.foo LIMIT 1" ); Assert.assertEquals( ImmutableList.of( ImmutableMap.of( "__time", new Timestamp(DateTimes.of("2000-01-01T00:00:00.000Z").getMillis()), "t2", new Date(DateTimes.of("2000-01-01").getMillis()) ) ), getRows(resultSet) ); }
@Override public void set(final PreparedStatement st, final int index, final JulianDate value, final int dbSqlType) throws SQLException { if (value == null) { st.setNull(index, dbSqlType); return; } if (dbSqlType == Types.TIMESTAMP) { st.setTimestamp(index, new Timestamp(value.toMilliseconds())); return; } st.setLong(index, value.toMilliseconds()); }
public java.util.Date readDate() { byte length = read(); int year = readUB2(); byte month = read(); byte date = read(); int hour = read(); int minute = read(); int second = read(); if (length == 11) { long nanos = readUB4(); Calendar cal = getLocalCalendar(); cal.set(year, --month, date, hour, minute, second); Timestamp time = new Timestamp(cal.getTimeInMillis()); time.setNanos((int) nanos); return time; } else { Calendar cal = getLocalCalendar(); cal.set(year, --month, date, hour, minute, second); return new java.sql.Date(cal.getTimeInMillis()); } }
PreparedStatement preparedStatement = connect.prepareStatement("INSERT into employee (time, name) VALUES (?,?)", Statement.RETURN_GENERATED_KEYS); preparedStatement.setTimestamp(1, new java.sql.Timestamp(new java.util.Date().getTime())); preparedStatement.setString(2, "Test"); preparedStatement.executeUpdate(); ResultSet tableKeys = preparedStatement.getGeneratedKeys(); tableKeys.next(); int autoGeneratedID = tableKeys.getInt(1);
@SuppressWarnings({ "unchecked" }) public <X> X unwrap(Calendar value, Class<X> type, WrapperOptions options) { if ( value == null ) { return null; } if ( Calendar.class.isAssignableFrom( type ) ) { return (X) value; } if ( java.sql.Date.class.isAssignableFrom( type ) ) { return (X) new java.sql.Date( value.getTimeInMillis() ); } if ( java.sql.Time.class.isAssignableFrom( type ) ) { return (X) new java.sql.Time( value.getTimeInMillis() ); } if ( java.sql.Timestamp.class.isAssignableFrom( type ) ) { return (X) new java.sql.Timestamp( value.getTimeInMillis() ); } if ( java.util.Date.class.isAssignableFrom( type ) ) { return (X) new java.util.Date( value.getTimeInMillis() ); } throw unknownUnwrap( type ); }
java.util.Date date = new Date(); Object param = new java.sql.Timestamp(date.getTime()); // The JDBC driver knows what to do with a java.sql type: preparedStatement.setObject(param);
/** * @throws Exception If failed. */ @Test public void testTimestampArrayField() throws Exception { Timestamp ts1 = new Timestamp(new Date().getTime()); Timestamp ts2 = new Timestamp(new Date().getTime() + 1000); ts1.setNanos(1000); ts2.setNanos(2000); Timestamp[] tsArr = new Timestamp[] { ts1, ts2 }; assertTrue(Arrays.equals(tsArr, builder("C").setField("ta", tsArr).build().<Timestamp[]>field("ta"))); }