Nearby lessons
14 of 37JDBC - Dates, BLOB and CLOB
- Handling dates correctly with JDBC (Date, Time, Timestamp)
- Setting and reading date values with setDate / getDate
- What BLOB and CLOB are and when to use them
- Storing and reading images and large text with JDBC
Dates, BLOB and CLOB is a fundamental part of database programming with JDBC. This lesson explains The Date Problem, Converting Between util.Date and sql.Date and Inserting and Reading Dates with PreparedStatement with complete, runnable code examples, clear step-by-step explanations, and common mistakes to avoid with exam-style MCQs at the end.
The Date Problem
A common mistake: trying to store a java.util.Date directly into a database. JDBC does not accept java.util.Date for date columns — it needs the JDBC date types from the java.sql package:
| Type | Stores | For |
|---|---|---|
| java.sql.Date | Only the date (year-month-day) | Birth date, hire date |
| java.sql.Time | Only the time (hours-minutes-seconds) | Shift start time |
| java.sql.Timestamp | Date + time, up to nanoseconds | When a row was created |
Converting Between util.Date and sql.Date
Inserting and Reading Dates with PreparedStatement
What Are BLOB and CLOB?
Normal columns hold small values. But what about an image, a video, or a long text document? JDBC uses two special types:
| Type | Full form | Stores | Example |
|---|---|---|---|
| BLOB | Binary Large Object | Binary data (images, videos, audio) | A profile photo |
| CLOB | Character Large Object | Very large text | A long article or resume |
Both BLOB and CLOB are interfaces in java.sql, and each database implements them. A BLOB/CLOB value is identified by a locator (a pointer to the data), and you read/write it with streams.
- Use java.sql.Date / Time / Timestamp for database date columns, not java.util.Date.
- setDate / getDate handle dates in PreparedStatement.
- BLOB stores binary data (images, videos); CLOB stores large text.
- setBlob/setClob write; getBlob/getClob read using locators and streams.
- For huge files, stream with buffers or store the file path instead.