Search in sources :

Example 1 with Timestamp

use of java.sql.Timestamp in project dropwizard by dropwizard.

the class JodaDateTimeArgument 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.getMillis()), calendarClone);
        } else {
            statement.setTimestamp(position, new Timestamp(value.getMillis()));
        }
    } else {
        statement.setNull(position, Types.TIMESTAMP);
    }
}
Also used : Calendar(java.util.Calendar) Timestamp(java.sql.Timestamp)

Example 2 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 3 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 4 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 5 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)

Aggregations

Timestamp (java.sql.Timestamp)3290 Test (org.junit.Test)546 PreparedStatement (java.sql.PreparedStatement)498 Date (java.util.Date)479 SQLException (java.sql.SQLException)409 ResultSet (java.sql.ResultSet)379 BigDecimal (java.math.BigDecimal)363 ArrayList (java.util.ArrayList)257 Connection (java.sql.Connection)245 Date (java.sql.Date)225 HashMap (java.util.HashMap)215 GenericValue (org.apache.ofbiz.entity.GenericValue)194 Calendar (java.util.Calendar)187 Time (java.sql.Time)177 GenericEntityException (org.apache.ofbiz.entity.GenericEntityException)167 SimpleDateFormat (java.text.SimpleDateFormat)159 Delegator (org.apache.ofbiz.entity.Delegator)157 IOException (java.io.IOException)136 Locale (java.util.Locale)130 Map (java.util.Map)115