Nearby lessons

14 of 37

JDBC - Dates, BLOB and CLOB

📌 What You Will Learn
  • 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:

TypeStoresFor
java.sql.DateOnly the date (year-month-day)Birth date, hire date
java.sql.TimeOnly the time (hours-minutes-seconds)Shift start time
java.sql.TimestampDate + time, up to nanosecondsWhen a row was created
Trainer's Note: Simple memory: java.util.Date = date AND time (the general one); java.sql.Date = date only (the database one); java.sql.Timestamp = date + time + fractions for databases.
In simple words: JDBC needs `java.sql` date types, not `java.util.Date`, for database columns. java.sql.Date keeps only the date, Time only the time, and Timestamp the date plus time with nanoseconds.

Converting Between util.Date and sql.Date

In simple words: To convert a `java.util.Date` to a `java.sql.Date`, pass its milliseconds into the constructor. new java.sql.Date(utilDate.getTime()) keeps only the date part and drops the time.
Example02
JCode Cell
1import java.util.*;
2 
3// util.Date -> sql.Date (only the date part is kept)
4java.util.Date utilDate = new java.util.Date();
5java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());
6 
7// For today's date directly:
8java.sql.Date today = java.sql.Date.valueOf(java.time.LocalDate.now());

Inserting and Reading Dates with PreparedStatement

In simple words: `setDate` writes a `java.sql.Date` into a placeholder and `getDate` reads it back. Wrap a string like "2024-01-15" with java.sql.Date.valueOf(...) before setting it.
Example03
JCode Cell
1import java.sql.*;
2 
3public class DateDemo {
4 public static void main(String[] args) throws Exception {
5 Connection con = DriverManager.getConnection(
6 "jdbc:mysql://localhost:3306/company", "root", "password");
7 
8 // Insert with a date
9 String sql = "INSERT INTO employee (id, name, hiredate) VALUES (?, ?, ?)";
10 PreparedStatement pst = con.prepareStatement(sql);
11 pst.setInt(1, 101);
12 pst.setString(2, "Rahul");
13 pst.setDate(3, java.sql.Date.valueOf("2024-01-15")); // setDate
14 pst.executeUpdate();
15 
16 // Read the date back
17 Statement st = con.createStatement();
18 ResultSet rs = st.executeQuery("SELECT hiredate FROM employee WHERE id=101");
19 rs.next();
20 java.sql.Date hd = rs.getDate(1);
21 System.out.println("Hire date: " + hd);
22 con.close();
23 }
24}
Output
Hire date: 2024-01-15

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:

TypeFull formStoresExample
BLOBBinary Large ObjectBinary data (images, videos, audio)A profile photo
CLOBCharacter Large ObjectVery large textA 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.

In simple words: BLOB stores binary files like images and videos, while CLOB stores very large text. Both are java.sql interfaces that point to the data with a locator, and you move the data with streams.
📝 Key Takeaways
  • 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.

🧠 Test Your Knowledge

4 Questions
Progress: 0 / 4