Master Optional for null safety, the modern Date/Time API, and database connectivity with JDBC.
Learn null-safe programming, modern date handling, and database operations.
import java.util.Optional;Optional.of(value) - value must not be nullOptional.ofNullable(value) - value can be nullOptional.empty() - empty OptionalisPresent() - returns true if value existsisEmpty() - returns true if no value (Java 11+)get() - gets value (throws if empty)orElse(default) - gets value or defaultorElseGet(supplier) - lazy defaultorElseThrow() - throws if emptymap(function) - transform valuefilter(predicate) - conditional checkifPresent(consumer) - execute if present--- Optional Basics ---
Has value: true
Value: Hello Optional!
With default: Hello Optional!
Empty with default: Default Value
--- Optional Methods ---
Uppercase: HELLO OPTIONAL!
Filtered (length > 5): Optional[Hello Optional!]
Filtered (length > 100): Optional.empty
--- Practical Example ---
Found user: Alice
User not found: Unknown User
Optional.of(val) or Optional.ofNullable(val)opt.isPresent() returns booleanopt.orElse("default")opt.map(v -> v.toUpperCase())opt.ifPresent(v -> System.out.println(v))Review feedback below
import java.time.*;import java.time.format.*;LocalDate - date only (year, month, day)LocalTime - time only (hour, minute, second)LocalDateTime - date and time combinedPeriod - date-based amount (days, months, years)Duration - time-based amount (hours, minutes, seconds)LocalDate.now(), LocalDate.of(2024, 1, 15)date.plusDays(10), date.minusMonths(1)date.getDayOfWeek(), date.getMonth()DateTimeFormatter.ofPattern("yyyy-MM-dd")date.format(formatter)LocalDate.parse("2024-01-15", formatter)--- LocalDate ---
Today: 2024-01-15
Specific date: 2024-12-25
Day of week: WEDNESDAY
Plus 30 days: 2024-02-14
--- LocalTime ---
Current time: 14:30:00
Specific time: 09:15:30
Plus 2 hours: 16:30:00
--- LocalDateTime ---
Now: 2024-01-15T14:30:00
Formatted: 15/01/2024 14:30
--- Period & Duration ---
Period between dates: P11M10D
Days until Christmas: 345
LocalDate.now() or LocalDate.of(2024, 1, 15)LocalTime.of(14, 30, 0)date.plusDays(10), time.plusHours(2)DateTimeFormatter.ofPattern("dd/MM/yyyy")Period.between(date1, date2)Review feedback below
import java.sql.*;Connection conn = DriverManager.getConnection(url, user, pass);jdbc:mysql://localhost:3306/dbnameStatement stmt = conn.createStatement();ResultSet rs = stmt.executeQuery("SELECT...");int rows = stmt.executeUpdate("INSERT...");while (rs.next()) { }rs.getString("column"), rs.getInt("column")PreparedStatement ps = conn.prepareStatement("SELECT * WHERE id=?");ps.setInt(1, value);--- Database Connection ---
Connected to database successfully!
--- Query Results ---
ID: 1, Name: Alice, Email: alice@example.com
ID: 2, Name: Bob, Email: bob@example.com
ID: 3, Name: Charlie, Email: charlie@example.com
--- Insert with PreparedStatement ---
Inserted 1 row(s) successfully!
--- Update Example ---
Updated 1 row(s)
Connection closed.
DriverManager.getConnection(url, user, pass)stmt.executeQuery("SELECT...") returns ResultSetstmt.executeUpdate("INSERT...") returns row countwhile(rs.next()) { rs.getString("col"); }ps.setString(1, value) - index starts at 1Review feedback below