How do you execute a SQL query using JDBC?
Run SELECT and INSERT queries with JDBC using PreparedStatement and try-with-resources, and learn why concatenated SQL is unsafe.
2,887 posts about Java: questions with answers, tutorials and articles, newest first.
Run SELECT and INSERT queries with JDBC using PreparedStatement and try-with-resources, and learn why concatenated SQL is unsafe.
JDBC is the standard Java API for connecting to relational databases. See how drivers, connections, prepared statements, transactions and pooling fit together.
Learn how to look up JNDI resources such as a DataSource with InitialContext, declare them in the server, inject them, and avoid common errors.
JNDI gives Java one API to look up named resources and search directories like LDAP. See DataSource and LDAP examples and how to avoid JNDI injection.
Use try for risky code, catch for specific failures and try-with-resources for closeable objects. Learn exactly when finally runs and what to avoid.
In Java, throw raises an exception inside a method body, while throws declares checked exceptions in the signature. Learn how they work together with examples.
Extend Exception or RuntimeException, add message and cause constructors and carry useful fields. Covers checked vs unchecked, chaining and hierarchies.
Exception chaining wraps a caught exception as the cause of a new one so context is added without losing the original error. See examples and best practices.
AutoCloseable lets try-with-resources release files, connections and executors automatically. See the contract, suppressed exceptions and AutoCloseable versus Closeable.
Try-with-resources closes AutoCloseable resources automatically, even when exceptions occur. See syntax, closing order, suppressed exceptions and best practices.
Try-with-resources closes files, streams and database connections automatically, in reverse order, and keeps the original exception when close() fails too.
Benchmark sequential and parallel streams with JMH instead of manual timers. Learn what decides the speedup and how to confirm it under real load.
A parallel stream runs stream operations on multiple threads using the common ForkJoinPool. Learn how it works, when it helps, and the pitfalls to avoid.
Call stream() on any Collection to get a sequential stream, then filter, map and collect. Includes other sources, parallel conversion and common mistakes.
A Collection stores elements, a Stream processes them lazily in a single-use pipeline. See the differences, examples with toList and groupingBy, and when to choose each.
Use DateTimeFormatter with the parse method of LocalDate, LocalDateTime or ZonedDateTime to turn text into dates, with examples for patterns, locales, strict mode and errors.
Duration measures time in seconds and nanoseconds, Period measures dates in years, months and days. Compare them with runnable Java examples.
Use ZonedDateTime.withZoneSameInstant with region ZoneId values to convert a moment between zones. Covers DST gaps, LocalDateTime input and storing UTC.
ZoneId identifies a time zone in java.time and carries its daylight saving rules. See how to create it, convert times and avoid common mistakes.
Add and subtract days, months and years, measure the gap between two dates and handle time zones in Java using LocalDate, Period, Duration and ChronoUnit.