Friday, August 10, 2018

Store file inside the database

By using this code we can store a file inside the database.

Note: In order to store a file inside database, we need to follow the given guideline

1. Create a database inside MySQL Server, here i am using files_db as a database name, however you can choose any name

>> create database files_db;

2. Use this database

>> use files_db;

3. Create a table inside this database, here i am using files_table as a table name, however you can choose any name 
note: in this table i am using four (that is 4) columns
column 1: filename, to store file name (for this i am using datatype VARCHAR)
column 2: filesize, to store file size (for this i am using datatype BIGINT)
column 3: filetype, to store file extension (for this i am using datatype VARCHAR)
column 4: filedata, to store the data of file (for this i am using datatype LONGBLOB, BLOB, means Binary Large OBject)

>> create table files_table(filename varchar(255),filesize bigint not null,filetype varchar(255) not null,filedata longblob not null, primary key(filename));

note: in order to store big files like (videos) please change the packet size of MySQL Server database

example:
1. open MySQL Server command prompt and provide username and password
2. On MySQL Server command line use the given command

 
4. Create a file as StoreFileInDB.java and copy and paste the given code 

import java.io.File;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.util.Scanner;

public class StoreFileInDB 
{
public static void main(String[] args) 
{
try
{
// create object of scanner to take input from keyboard
Scanner sc = new Scanner(System.in);

System.out.print("Enter the file path to be stored in database: ");

// here we will take path of file that need to be stored
// inside the database
String filepath = sc.nextLine();

// create object of file class to denote this path
File file = new File(filepath);

// check if path of file exists and path also belongs to 
// a file rather than a directory
if(file.exists() && file.isFile())
{
// get the name of file
String filename = file.getName();

// get the size of file
long filesize = file.length();

// get last index of . (that is dot) in file name
int index = filename.lastIndexOf(".");

// get a substring from the given index plus 1
// using this approach we can extract the extension from the filename 
String ext = filename.substring(index + 1);

// now open a a file in read mode
// means now we can fetch the data from this file
FileInputStream fi = new FileInputStream(filepath);

// load the driver class for database connection
Class.forName("com.mysql.jdbc.Driver");

// get object of connection
Connection co = DriverManager.getConnection
("jdbc:mysql://localhost:3306/files_db","root","root");

// get object of PreparedStatement, to execute
// sql statements
PreparedStatement ps = co.prepareStatement
("insert into files_table values(?,?,?,?)");

// provide values in place of ? mark
// set the file name to be stored inside database
ps.setString(1, filename); 

// set the file size to be stored inside database
ps.setLong(2, filesize);

// set the file extension to be stored inside database
ps.setString(3, ext);

// set the actual data of file to be stored inside database
ps.setBinaryStream(4, fi,(int)filesize);

// execute sql statement
ps.executeUpdate();

// show a message on screen
System.out.println("File has been saved inside the database");

// close the database connection
co.close();
}
else
{
System.out.println("Either file not exits or its is a folder");
}

}
// handle runtime error, generated during the program execution
catch (Exception e) 
{
System.out.println("error "+e);
}

} // end of main method
}   // end of class


Output:
Enter the file path to be stored in database: c:\abc.txt
File has been saved inside the database

5 comments: