sql >> データベース >  >> RDS >> Oracle

Oracle JDBCドライバーで、Javaの日付をTIMESTAMP列に書き込むと、タイムゾーンはどうなりますか?

    何が起こるかを正確に把握するために、いくつかのテストJDBCコードをまとめました。結果は興味深いものでした。 Oracleには、密接に関連する3つのデータ型があります。TIMESTAMPTIMESTAMP WITH TIME ZONE 、およびTIMESTAMP WITH LOCAL TIME ZONE 。まったく同じコードを取得し、2つの異なるボックスから実行しました。1つは「America / New_York」タイムゾーンで、もう1つはUTCで実行しています。両方とも同じデータベースにヒットし、UTCで実行されています。 Oracle11.2.0.2.0ドライバを使用していました。

    • TIMESTAMP 列は、Javaコードを実行しているマシンの現地時間に設定されていました。タイムゾーンの変換は実行されませんでした。
    • TIMESTAMP WITH TIME ZONE 列は、時間をJDBCクライアントが存在するタイムゾーンに変換しました。
    • TIMESTAMP WITH LOCAL TIME ZONE 列はまた、時間をJDBCクライアントが存在するタイムゾーンに変換しました。

    この記事 、少し古いですが、TIMESTAMP WITH TIME ZONE インデックスやパーティションのようなことをしたいのなら、ほとんど役に立たない。ただし、TIMESTAMP WITH LOCAL TIME ZONEのようです。 非常に役立つかもしれません。 (サーバーのタイムゾーンを変更するとどうなるかはわかりませんが、JDBCクライアントのローカルタイムゾーンについてはインテリジェントなようです)。これらのデータ型を使用して、インデックス作成の動作などをテストする機会がありませんでした。

    ご使用の環境でテストを再現したい場合は、以下のサンプルクラスに貼り付けてください。

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.Timestamp;
    import java.util.Date;
    
    // create table x_tst_ts_tab(
    // os_name varchar(256)
    // ts timestamp,
    // ts_with_tz timestamp with time zone,
    // ts_with_local_tz timestamp with local time zone
    // )
    class TSTest {
        public static final void main(String[] argv) throws Exception {
            Class.forName("oracle.jdbc.OracleDriver");
            Connection conn = DriverManager.getConnection(
                "your_connection_string",
                "your_user_name",
                "your_password");
    
            try {
                // Insert some data
                Date nowDate = new Date();
                Timestamp nowTimestamp = new Timestamp(nowDate.getTime());
                PreparedStatement insertStmt = conn.prepareStatement(
                    "INSERT INTO x_tst_ts_tab"
                    + " (os_name, ts, ts_with_tz, ts_with_local_tz)"
                    + " VALUES (?, ?, ?, ?)");
                try {
                    insertStmt.setString(1, System.getProperty("os.name"));
                    insertStmt.setTimestamp(2, nowTimestamp);
                    insertStmt.setTimestamp(3, nowTimestamp);
                    insertStmt.setTimestamp(4, nowTimestamp);
                    insertStmt.executeUpdate();
                } finally {
                    try {
                        insertStmt.close();
                    } catch (Throwable t) {
                        // do nothing
                    }
                }
    
                System.out.println("os_name, ts, ts_with_tz, ts_with_local_tz");
    
                // Read back everything in the DB
                PreparedStatement selectStmt = conn.prepareStatement(
                    "SELECT os_name, ts, ts_with_tz, ts_with_local_tz"
                    + " FROM dom_fraud_beacon.x_tst_ts_tab");
                ResultSet result = null;
                try {
                    result = selectStmt.executeQuery();
                    while (result.next()) {
                        System.out.println(
                            String.format("%s,%s,%s,%s",
                                          result.getString(1),
                                          result.getTimestamp(2).toString(),
                                          result.getTimestamp(3).toString(),
                                          result.getTimestamp(4).toString()
                                          ));
                    }
                } finally {
                    try {
                        result.close();
                    } catch (Throwable t) {
                        // do nothing
                    } finally {
                        try {
                            selectStmt.close();
                        } catch (Throwable t) {
                            // do nothing
                        }
                    }
                }
            } finally {
                try {
                    conn.close();
                } catch (Throwable t) {
                    // do nothing
                }
            }
        }
    }
    



    1. PostgresでGroupByに列がないことを許可しているのはなぜですか?

    2. ROW_NUMBERクエリ

    3. Hibernate、MySQLアプリケーションでimport.sqlファイルを使用してデフォルトデータをテーブルに挿入する方法

    4. PictureBox イメージを Sql Server データベース Varbinary(Max) 列 VB.NET に保存して取得する方法