我发现没有办法将日期/时间插入到标准区域(如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中的任何数据类型都可能被建议 - 不仅仅是我在这里介绍的。