Search in sources :

Example 91 with Timestamp

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);
    }
}
Also used : Calendar(java.util.Calendar) Timestamp(java.sql.Timestamp)

Example 92 with 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();
    }
}
Also used : Timestamp(java.sql.Timestamp) Managed(io.dropwizard.lifecycle.Managed) Handle(org.skife.jdbi.v2.Handle) Before(org.junit.Before)

Example 93 with Timestamp

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();
}
Also used : DBI(org.skife.jdbi.v2.DBI) Timestamp(java.sql.Timestamp) SQLException(java.sql.SQLException) Handle(org.skife.jdbi.v2.Handle)

Example 94 with Timestamp

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;
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) Timestamp(java.sql.Timestamp)

Example 95 with Timestamp

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;
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) Timestamp(java.sql.Timestamp)

Aggregations

Timestamp (java.sql.Timestamp)1539 PreparedStatement (java.sql.PreparedStatement)265 Test (org.junit.Test)245 SQLException (java.sql.SQLException)236 ResultSet (java.sql.ResultSet)209 BigDecimal (java.math.BigDecimal)204 Date (java.util.Date)165 Date (java.sql.Date)122 Connection (java.sql.Connection)117 ArrayList (java.util.ArrayList)109 Calendar (java.util.Calendar)76 Test (org.testng.annotations.Test)71 GregorianCalendar (java.util.GregorianCalendar)68 Time (java.sql.Time)65 SimpleDateFormat (java.text.SimpleDateFormat)62 Change (com.google.gerrit.reviewdb.client.Change)59 IOException (java.io.IOException)51 BaseTest (util.BaseTest)49 PatchSet (com.google.gerrit.reviewdb.client.PatchSet)45 Properties (java.util.Properties)41