Share & learn

Through Innovative Digital Library

How to retrieve pdf file from mysql database?

Write a code in Java to retrieve a pdf file from mysql database.
 



Answers

Posted by Paul on April 25th 2018 13:30 PM

Here a pdf file stored in mysql database will be extracted and stored in a newly created pdf file.

Table structure:

CREATE TABLE IMAGE
(

id varchar(10),
IMG BLOB

);

Posted by Paul on April 25th 2018 13:32 PM

Here is the code : Output image shows the newly created pdf after extracting from database.

// Java program to store pdf file to mysql database
import java.io.OutputStream;
import java.sql.Blob;
import java.sql.Connection;
import java.io.*;
import java.io.InputStream;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.*;

public class 
DBToPDF {
    
    
Connection connection;
    
Statement statement;
    
ResultSet rs;
    
PreparedStatement pst;
    
Blob blob;
    
InputStream in;
    
InputStream input;
    
OutputStream output;

    
DBToPDF() {
        
        
// database connectivity
        
try {
            
            Class.
forName("com.mysql.jdbc.Driver");
            
connection DriverManager.getConnection("jdbc:mysql:///file_db""root""root");
        
        } catch (
ClassNotFoundException cnf) {
            
cnf.printStackTrace();
        } catch (
java.sql.SQLException sql) {
            
sql.printStackTrace();
        }
        
    }

    public 
void fetchFile() {
        try {
            
            
statement connection.createStatement();
            
            
//query to fetch file from database
            
rs statement.executeQuery("Select img from image");
            
in null;
            while (
rs.next()) {
                
in rs.getBinaryStream(1);
            }

            
int available1 in.available();
            
byte[] bt = new byte[available1];
            
in.read(bt);

            
// new pdf file to store extracted data
            
FileOutputStream fout = new FileOutputStream("D:/test2.pdf");
            
DataOutputStream dout = new DataOutputStream(fout);
            
dout.write(bt0bt.length);
            
fout.close();
            
        } catch (
Exception e) {
            
System.out.println(e);
        }

    }

    public static 
void main(String args[]) {
        
        
// create instance of class
        
DBToPDF obj = new DBToPDF();
        
        
// call to method
        
obj.fetchFile();
        
        
// message after successful execution of program
        
System.out.println("Data extracted successfully!!!");
        
    }
}

Posted by anonymous on April 23rd 2019 23:41 PM

isotretinoin 20mg Generic Viagra Canada Customs Kamagra Oral Jelly Acheter [url=http://corzide.com]viagra[/url] Finasteride Ropropecia How To Buy When Will Alli Be Available Again 2014 Order Viagra Online Cheap

isotretinoin 20mg Generic Viagra Canada Customs Kamagra Oral Jelly Acheter  [url=http://corzide.com]viagra[/url] Finasteride Ropropecia How To Buy When Will Alli Be Available Again 2014 Order Viagra Online Cheap

Posted by anonymous on May 10th 2019 02:21 AM

Amoxicillin Side Effects In Cats Vomit Viagra Zollfrei [url=http://orderlevi.com]usda approved generic levitra[/url] Canda Pharmasey Male Enhancement

Amoxicillin Side Effects In Cats Vomit Viagra Zollfrei  [url=http://orderlevi.com]usda approved generic levitra[/url] Canda Pharmasey Male Enhancement

Posted by anonymous on July 29th 2019 03:45 AM

Dapoxetina Ou Paroxetina Propecia Dosage Or 5mg Does Amoxil Treat Upper Respirator Infection [url=http://cialiprice.com]cialis from canada[/url] Levitra Spese In Farmacia Pharmists In Canada Cialis Sertraline Overnight

Dapoxetina Ou Paroxetina Propecia Dosage Or 5mg Does Amoxil Treat Upper Respirator Infection  [url=http://cialiprice.com]cialis from canada[/url] Levitra Spese In Farmacia Pharmists In Canada Cialis Sertraline Overnight

Posted by anonymous on August 2nd 2019 09:01 AM

Order Cialis Online Pharmacy Levitra Fa Bene Al Cuore [url=http://levipill.com]costco pharmacy prices levitra 52[/url] Kamagra Sublingual Canda Pharmasey Male Enhancement Prix Cialis Tadalafil 5mg

Order Cialis Online Pharmacy Levitra Fa Bene Al Cuore  [url=http://levipill.com]costco pharmacy prices levitra 52[/url] Kamagra Sublingual Canda Pharmasey Male Enhancement Prix Cialis Tadalafil 5mg

Posted by anonymous on August 8th 2019 04:45 AM

How Much Is A Viagra Prescription Cephalexin Brand Name Prezzo Kamagra In Belgio [url=http://rxasian.com]viagra[/url] Generic Viagra Buy

How Much Is A Viagra Prescription Cephalexin Brand Name Prezzo Kamagra In Belgio  [url=http://rxasian.com]viagra[/url] Generic Viagra Buy
Answer Question

Your email address will not be published. Required fields are marked *

  • Minimum 20 character
VLC2

related questions

Most liked questions

Most answered questions

Login
VLC2 Refresh
Register
VLC2 Refresh
Forgot password
Ask a Question
Minimum 20 character
Minimum 20 character
Reply
Minimum 20 character
VLC2 Refresh