use of java.sql.Timestamp in project dropwizard by dropwizard.
the class ZonedDateTimeArgument method apply.
@Override
public void apply(final int position, final PreparedStatement statement, final StatementContext ctx) throws SQLException {
if (value != null) {
if (calendar.isPresent()) {
// We need to make a clone, because Calendar is not thread-safe
// and some JDBC drivers mutate it during time calculations
final Calendar calendarClone = (Calendar) calendar.get().clone();
statement.setTimestamp(position, new Timestamp(value.toInstant().toEpochMilli()), calendarClone);
} else {
statement.setTimestamp(position, new Timestamp(value.toInstant().toEpochMilli()));
}
} else {
statement.setNull(position, Types.TIMESTAMP);
}
}
use of java.sql.Timestamp in project dropwizard by dropwizard.
the class JDBITest method setUp.
@Before
public void setUp() throws Exception {
when(environment.healthChecks()).thenReturn(healthChecks);
when(environment.lifecycle()).thenReturn(lifecycleEnvironment);
when(environment.metrics()).thenReturn(metricRegistry);
when(environment.getHealthCheckExecutorService()).thenReturn(Executors.newSingleThreadExecutor());
this.dbi = factory.build(environment, hsqlConfig, "hsql");
final ArgumentCaptor<Managed> managedCaptor = ArgumentCaptor.forClass(Managed.class);
verify(lifecycleEnvironment).manage(managedCaptor.capture());
managed.addAll(managedCaptor.getAllValues());
for (Managed obj : managed) {
obj.start();
}
try (Handle handle = dbi.open()) {
handle.createCall("DROP TABLE people IF EXISTS").invoke();
handle.createCall("CREATE TABLE people (name varchar(100) primary key, email varchar(100), age int, created_at timestamp)").invoke();
handle.createStatement("INSERT INTO people VALUES (?, ?, ?, ?)").bind(0, "Coda Hale").bind(1, "chale@yammer-inc.com").bind(2, 30).bind(3, new Timestamp(1365465078000L)).execute();
handle.createStatement("INSERT INTO people VALUES (?, ?, ?, ?)").bind(0, "Kris Gale").bind(1, "kgale@yammer-inc.com").bind(2, 32).bind(3, new Timestamp(1365465078000L)).execute();
handle.createStatement("INSERT INTO people VALUES (?, ?, ?, ?)").bind(0, "Old Guy").bindNull(1, Types.VARCHAR).bind(2, 99).bind(3, new Timestamp(1365465078000L)).execute();
handle.createStatement("INSERT INTO people VALUES (?, ?, ?, ?)").bind(0, "Alice Example").bind(1, "alice@example.org").bind(2, 99).bindNull(3, Types.TIMESTAMP).execute();
}
}
use of java.sql.Timestamp in project druid by druid-io.
the class JDBCExtractionNamespaceCacheFactory method lastUpdates.
private Long lastUpdates(CacheScheduler.EntryImpl<JDBCExtractionNamespace> id, JDBCExtractionNamespace namespace) {
final DBI dbi = ensureDBI(id, namespace);
final String table = namespace.getTable();
final String tsColumn = namespace.getTsColumn();
if (tsColumn == null) {
return null;
}
final Timestamp update = dbi.withHandle(new HandleCallback<Timestamp>() {
@Override
public Timestamp withHandle(Handle handle) throws Exception {
final String query = String.format("SELECT MAX(%s) FROM %s", tsColumn, table);
return handle.createQuery(query).map(TimestampMapper.FIRST).first();
}
});
return update.getTime();
}
use of java.sql.Timestamp in project elastic-job by dangdangdotcom.
the class StatisticRdbRepository method add.
/**
* 添加运行中的任务统计数据.
*
* @param taskRunningStatistics 运行中的任务统计数据对象
* @return 添加操作是否成功
*/
public boolean add(final TaskRunningStatistics taskRunningStatistics) {
boolean result = false;
String sql = "INSERT INTO `" + TABLE_TASK_RUNNING_STATISTICS + "` (`running_count`, `statistics_time`, `creation_time`) VALUES (?, ?, ?);";
try (Connection conn = dataSource.getConnection();
PreparedStatement preparedStatement = conn.prepareStatement(sql)) {
preparedStatement.setInt(1, taskRunningStatistics.getRunningCount());
preparedStatement.setTimestamp(2, new Timestamp(taskRunningStatistics.getStatisticsTime().getTime()));
preparedStatement.setTimestamp(3, new Timestamp(taskRunningStatistics.getCreationTime().getTime()));
preparedStatement.execute();
result = true;
} catch (final SQLException ex) {
// TODO 记录失败直接输出日志,未来可考虑配置化
log.error("Insert taskRunningStatistics to DB error:", ex);
}
return result;
}
use of java.sql.Timestamp in project elastic-job by dangdangdotcom.
the class JobEventRdbStorage method insertJobExecutionEvent.
private boolean insertJobExecutionEvent(final JobExecutionEvent jobExecutionEvent) {
boolean result = false;
String sql = "INSERT INTO `" + TABLE_JOB_EXECUTION_LOG + "` (`id`, `job_name`, `task_id`, `hostname`, `ip`, `sharding_item`, `execution_source`, `is_success`, `start_time`) " + "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?);";
try (Connection conn = dataSource.getConnection();
PreparedStatement preparedStatement = conn.prepareStatement(sql)) {
preparedStatement.setString(1, jobExecutionEvent.getId());
preparedStatement.setString(2, jobExecutionEvent.getJobName());
preparedStatement.setString(3, jobExecutionEvent.getTaskId());
preparedStatement.setString(4, jobExecutionEvent.getHostname());
preparedStatement.setString(5, jobExecutionEvent.getIp());
preparedStatement.setInt(6, jobExecutionEvent.getShardingItem());
preparedStatement.setString(7, jobExecutionEvent.getSource().toString());
preparedStatement.setBoolean(8, jobExecutionEvent.isSuccess());
preparedStatement.setTimestamp(9, new Timestamp(jobExecutionEvent.getStartTime().getTime()));
preparedStatement.execute();
result = true;
} catch (final SQLException ex) {
if (!isDuplicateRecord(ex)) {
// TODO 记录失败直接输出日志,未来可考虑配置化
log.error(ex.getMessage());
}
}
return result;
}
Aggregations