兩種主流方式:
String sql = "INSERT INTO t (create_time) VALUES (NOW())";
stmt.executeUpdate(sql);
String sql = "INSERT INTO t (create_time) VALUES (?)";
try (PreparedStatement ps = conn.prepareStatement(sql)) {
ps.setTimestamp(1, new Timestamp(System.currentTimeMillis()));
ps.executeUpdate();
}
Timestamp 對應 MySQL DATETIME/TIMESTAMP。java.util.Date 直接 setObject,類型不匹配易報錯。DATETIME(不隨時區變化)或 TIMESTAMP(自動轉換時區)。serverTimezone=UTC 或明確時區,避免時區偏移。