0

JavaクエリからSQLテーブルに日付を挿入し、jodatimeAPIを使用してプログラムから呼び出されたときに日付を取得する方法。受信した日付で計算を行うためにjodatimeAPIを使用しているためですか?

4

1 に答える 1

1

JDBCを使用している場合は、永続化する前にJodaTimeの日付からJDKの日付に変換するだけです。
これらの線に沿った何か:

public void insertDateValue(DateTime value) throws SQLException {

    String insertString = "INSERT INTO tableName(datecolumn) VALUES(?)";

    PreparedStatement insert = null;

    try {
        insert = connection.prepareStatement(insertString);

        // Important part is right here:
        insert.setDate(1, new Date(value.getMillis()));
        // Oh, and the new object should be java.sql.Date


        insert.executeUpdate();
        connection.commit();
    } catch (SQLException e ) {
        if (con != null) {
            try {
                connection.rollback();
            } catch(SQLException excep) {
                // Should maybe do something here
            }
        }
    } finally {
        if (insert != null) {
            insert.close();
        }
    }
}

データベースから取得するときに、その逆を行うことができます。

于 2013-02-28T17:24:53.737 に答える