🚀 Master Java database programming, JDBC BLOB/CLOB handling, and MySQL!
🔔 Subscribe to Ram N Java on YouTubeReading Large Files and Documents from MySQL with JDBC
Databases frequently store documents, configurations, text files, and binary assets inside character or binary columns such as TEXT, LONGTEXT, CLOB, or BLOB.
In JDBC, you can retrieve these files by fetching them as a character stream using getCharacterStream() or as a reader from the ResultSet. By piping the stream through a FileWriter or FileOutputStream, your Java application can export and reconstruct the exact file onto your local filesystem.
The MySQL Database Table Structure
Consider a table designed to store file details along with text content or file data:
CREATE TABLE `file_repository` (
`id` INT NOT NULL AUTO_INCREMENT,
`file_name` VARCHAR(255) NOT NULL,
`file_data` LONGTEXT,
PRIMARY KEY (`id`)
);
Complete Java Source Code Example
The following Java application connects to MySQL, queries the record containing the stored file data, streams the content using a Reader, and writes it directly to a local target destination:
package com.ram.jdbc;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class RetrieveFileFromDBDemo {
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost:3306/world";
static final String USER = "root";
static final String PASS = "root";
public static void main(String[] args) {
RetrieveFileFromDBDemo demo = new RetrieveFileFromDBDemo();
// Retrieve file for record ID 1 and save to destination directory
demo.retrieveFile(1, "output_retrieved.txt");
}
public void retrieveFile(int recordId, String outputFileName) {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
FileWriter writer = null;
Reader reader = null;
try {
// Step 1: Register driver and establish connection
Class.forName(JDBC_DRIVER);
conn = DriverManager.getConnection(DB_URL, USER, PASS);
// Step 2: Prepare query to fetch the file record
String sql = "SELECT file_name, file_data FROM file_repository WHERE id = ?";
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, recordId);
// Step 3: Execute query
rs = pstmt.executeQuery();
if (rs.next()) {
String originalFileName = rs.getString("file_name");
System.out.println("Found record for file: " + originalFileName);
// Step 4: Stream character data using Reader
reader = rs.getCharacterStream("file_data");
File outputFile = new File(outputFileName);
writer = new FileWriter(outputFile);
int character;
while ((character = reader.read()) != -1) {
writer.write(character);
}
System.out.println("File successfully downloaded and saved to: " + outputFile.getAbsolutePath());
} else {
System.out.println("No file found with ID: " + recordId);
}
} catch (ClassNotFoundException | SQLException | IOException e) {
e.printStackTrace();
} finally {
// Step 5: Clean up IO and database resources safely
try {
if (writer != null) writer.close();
if (reader != null) reader.close();
if (rs != null) rs.close();
if (pstmt != null) pstmt.close();
if (conn != null) conn.close();
} catch (SQLException | IOException se) {
se.printStackTrace();
}
}
}
}
Key Steps & Best Practices
-
Stream Large Payloads: Avoid loading massive text or binary records entirely into memory strings; use
rs.getCharacterStream()orrs.getBinaryStream()instead. - Chunked Writing: Reading and writing byte-by-byte or via buffer arrays ensures your application consumes negligible heap memory even with large files.
- PreparedStatement Safety: Always use parameterized SQL statements to filter file IDs safely.
-
Thorough Cleanup: Ensure both stream readers/writers and database resources (
ResultSet,Statement,Connection) are closed in thefinallyblock.
Related Tutorials You Might Enjoy
Click here to watch in Youtube : https://www.youtube.com/watch?v=MAgdMFodV0Q
Click the below Image to Enlarge
![]() |
| JDBC : Retrieve File |
![]() |
| JDBC : Retrieve File |
![]() |
| JDBC : Retrieve File |
![]() |
| JDBC : Retrieve File |
![]() |
| JDBC : Retrieve File |
![]() |
| JDBC : Retrieve File |






No comments:
Post a Comment