Saturday, 23 August 2014

JDBC : Retrieve File

🚀 Master Java database programming, JDBC BLOB/CLOB handling, and MySQL!

🔔 Subscribe to Ram N Java on YouTube

Reading 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() or rs.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 the finally block.

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

JDBCRetriveFileDemo.java
import java.io.FileWriter;
import java.io.Reader;
import java.sql.Clob;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class JDBCRetriveFileDemo
{
    // JDBC driver name and database URL
    static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
    static final String DB_URL      = "jdbc:mysql://localhost:3306/world";

    // Database credentials
    static final String USERNAME    = "root";
    static final String PASSWORD    = "root";

    public static void main( String[] args )
    {
        JDBCRetriveFileDemo jdbcRetriveFileDemo = new JDBCRetriveFileDemo();
        jdbcRetriveFileDemo.retriveFile();
    }

    private void retriveFile()
    {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        try
        {
            /*
             * Register the JDBC driver in DriverManager
             */

            Class.forName(JDBC_DRIVER);

            /*
             * Establish connection to the Database using DriverManager
             */

            connection = DriverManager
                    .getConnection(DB_URL, USERNAME, PASSWORD);

            String sql = "select * from CITY_HISTORY";

            /*
             * Execute the query
             */
            preparedStatement = connection.prepareStatement(sql);
            ResultSet rs = preparedStatement.executeQuery();

            while( rs.next() )
            {
                String name = rs.getString(2);
                Clob cityHistoryClob = rs.getClob(3);
                Reader reader = cityHistoryClob.getCharacterStream();

                FileWriter fw = new FileWriter("D:/Downloads/" + name );

                int i;
                while( (i = reader.read()) != -1 )
                {
                    fw.write((char) i);
                }
                System.out.println("D:/Downloads/" + name +" has saved");
                fw.close();
            }

            rs.close();

        }
        catch( SQLException se )
        {

            se.printStackTrace();
        }
        catch( ClassNotFoundException e )
        {
            e.printStackTrace();
        }
        catch( Exception e )
        {
            e.printStackTrace();
        }
        finally
        {
            /*
             * finally block used to close resources
             */
            try
            {
                if( preparedStatement != null )
                {
                    preparedStatement.close();
                }
            }
            catch( SQLException sqlException )
            {
                sqlException.printStackTrace();
            }
            try
            {
                if( connection != null )
                {
                    connection.close();
                }
            }
            catch( SQLException sqlException )
            {
                sqlException.printStackTrace();
            }
        }

    }
}

Output
D:/Downloads/Newyork History.txt has saved

Environment Used 

JDK version : 1.7.0_51
Mysql Server version : 5.6.19 

To Download JDBCRetriveFileDemoApp Project Click the below link

https://sites.google.com/site/javaee4321/jdbc/JDBCRetriveFileDemoApp.zip?attredirects=0&d=1

See also:

  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • No comments:

    Post a Comment

    Tutorials