Nearby lessons

21 of 37

JDBC - Streaming BLOB and CLOB Data

📌 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

Streaming BLOB and CLOB Data is a fundamental part of database programming with JDBC. This lesson explains Storing an Image (BLOB) — Complete Program and Storing a Large Text (CLOB) with complete, runnable code examples, clear step-by-step explanations, and common mistakes to avoid with exam-style MCQs at the end.

Storing an Image (BLOB) — Complete Program

Example01
JCode Cell
1import java.sql.*;
2import java.io.*;
3 
4public class BlobDemo {
5 public static void main(String[] args) throws Exception {
6 Connection con = DriverManager.getConnection(
7 "jdbc:mysql://localhost:3306/company", "root", "password");
8 
9 // 1. Store an image file into the photo column
10 FileInputStream fis = new FileInputStream("photo.jpg");
11 PreparedStatement pst = con.prepareStatement(
12 "INSERT INTO employee (id, name, photo) VALUES (?, ?, ?)");
13 pst.setInt(1, 101);
14 pst.setString(2, "Rahul");
15 pst.setBlob(3, fis); // setBlob with an input stream
16 pst.executeUpdate();
17 fis.close();
18 System.out.println("Image stored");
19 
20 // 2. Read the image back and save it to a new file
21 Statement st = con.createStatement();
22 ResultSet rs = st.executeQuery("SELECT photo FROM employee WHERE id=101");
23 rs.next();
24 Blob blob = rs.getBlob(1); // get the BLOB
25 byte[] data = blob.getBytes(1, (int) blob.length());
26 
27 FileOutputStream fos = new FileOutputStream("photo_out.jpg");
28 fos.write(data);
29 fos.close();
30 System.out.println("Image read back");
31 con.close();
32 }
33}

Storing a Large Text (CLOB)

Trainer's Note: Trainer tip: for very large files, always use setBinaryStream/setCharacterStream with a buffer instead of loading everything into memory at once. In real projects, many teams prefer to store the file path in the database and keep the file on disk — that keeps the database light. BLOB/CLOB is still the right answer when the data must live inside the database.
Example02
JCode Cell
1import java.sql.*;
2 
3public class ClobDemo {
4 public static void main(String[] args) throws Exception {
5 Connection con = DriverManager.getConnection(
6 "jdbc:mysql://localhost:3306/company", "root", "password");
7 
8 // STORE a large text as a CLOB
9 String longText = "This is a very long article... (many pages of text)";
10 PreparedStatement pst = con.prepareStatement(
11 "INSERT INTO article (id, content) VALUES (?, ?)");
12 pst.setInt(1, 1);
13 pst.setString(2, longText); // JDBC handles it as a CLOB
14 pst.executeUpdate();
15 
16 // READ it back
17 Statement st = con.createStatement();
18 ResultSet rs = st.executeQuery("SELECT content FROM article WHERE id=1");
19 rs.next();
20 Clob clob = rs.getClob(1);
21 String content = clob.getSubString(1, (int) clob.length());
22 System.out.println("Read back: " + content);
23 con.close();
24 }
25}
Output
Read back: This is a very long article... (many pages of text)
📝 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