我發現沒有辦法將日期/時間插入到標準區域(如UTC中)的MySQL數據庫中。將特定區域的日期/時間插入到MySQL數據庫中
以下代碼段使用Apache DBCP對用戶進行身份驗證。
public final class AuthenticationDAO {
public boolean isAuthenticated(String userName, String password) throws SQLException {
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
boolean status = false;
try {
connection = DatabaseConnection.getConnection();
preparedStatement = connection.prepareStatement("select * from admin_tbl where lower(admin_id)=lower(?) and BINARY password=?");
preparedStatement.setString(1, userName);
preparedStatement.setString(2, password);
resultSet = preparedStatement.executeQuery();
status = resultSet.next();
if (status) {
preparedStatement = connection.prepareStatement("update admin_tbl set last_login=? where lower(admin_id)=lower(?) and BINARY password=?");
preparedStatement.setTimestamp(1, new Timestamp(new Date().getTime())); // java.util.Date/java.sql.Timestamp
preparedStatement.setString(2, userName);
preparedStatement.setString(3, password);
preparedStatement.executeUpdate();
}
} finally {
if (connection != null) {connection.close();}
if (resultSet != null) {resultSet.close();}
if (preparedStatement != null) {preparedStatement.close();}
}
return status;
}
}
此代碼只有一行符合我們的興趣。
preparedStatement.setTimestamp(1, new Timestamp(new Date().getTime()));
當用戶/管理員的登錄嘗試成功時,此行更新登錄日期/時間。 MySQL中的列last_login
的類型爲DATETIME
。
我想插入日期/時間在MySQL中沒有發生的UTC區。它似乎需要一個本地區域。
通過任何方式,是否可以在UTC區插入日期/時間到MySQL中? Java和/或MySQL中的任何數據類型都可能被建議 - 不僅僅是我在這裏介紹的。