🚀 Master Java database programming and file handling hands-on!
🔔 Subscribe to Ram N Java on YouTubeStoring Files in Databases Using JDBC
In real-world applications, you often need to persist file data—such as text documents, configuration files, or logs—directly into relational database tables.
Relational databases like MySQL support Large Object data types such as CLOB (Character Large Object) and TEXT / LONGTEXT for storing large text files. With JDBC's PreparedStatement and Java I/O character streams (such as FileReader), you can stream and save large files directly into your database efficiently.
Database Table Structure
Before running the Java program, create the destination table in MySQL to hold your file and its metadata:
CREATE TABLE `city_history` (
`id` INT NOT NULL AUTO_INCREMENT,
`city_name` VARCHAR(100) NOT NULL,
`history` LONGTEXT,
PRIMARY KEY (`id`)
);
Java Program: Storing a File Using PreparedStatement
Here is the complete Java code that reads a local file using FileReader and streams its content into MySQL using setCharacterStream():
package com.ram.jdbc;
import java.io.File;
import java.io.FileReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
public class StoreFileDemo {
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) {
Connection conn = null;
PreparedStatement pstmt = null;
FileReader reader = null;
try {
// Load MySQL Driver & Open Connection
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(DB_URL, USER, PASS);
String sql = "INSERT INTO city_history (city_name, history) VALUES (?, ?)";
pstmt = conn.prepareStatement(sql);
// Set city name parameter
pstmt.setString(1, "New York");
// Locate file and read via FileReader
File file = new File("history.txt");
reader = new FileReader(file);
// Stream file content into database column
pstmt.setCharacterStream(2, reader, (int) file.length());
int rowsInserted = pstmt.executeUpdate();
System.out.println("File stored successfully! Rows inserted: " + rowsInserted);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (reader != null) reader.close();
if (pstmt != null) pstmt.close();
if (conn != null) conn.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
}
Key Takeaways
-
setCharacterStream()allows you to stream character data directly to the database without loading massive files completely into application memory. -
For binary files (like images, audio, or PDFs), use
setBinaryStream()withFileInputStreamandBLOBcolumn types. -
Always properly close the
FileReader,PreparedStatement, andConnectionto avoid file locks and database leaks.
Related Tutorials You Might Enjoy
Click here to watch in Youtube : https://www.youtube.com/watch?v=BMqaLhN2chk
Click the below Image to Enlarge
![]() |
| JDBC : Store File |
![]() |
| JDBC : Store File |
![]() |
| JDBC : Store File |
![]() |
| JDBC : Store File |
![]() |
| JDBC : Store File |





No comments:
Post a Comment