Save an image (profile picture) within a database as byte[] or string (url)? What are the advantages and disadvantages?
2 Answers
You should definitely prefer URL instead of byte[].
The main reason is, not each language have same bit patterns while storing images to byte arrays. For example java's byte is signed (ranges -128 to 127) whereas C#'s byte is unsigned (ranges 0 to 255). This means, if you store the image in byte arrays using C#, the platform retransforming the byte array back to image has to be c# (unless you don`t want to deal with low level convertions)
On the other hand A URL is a URI that, in addition to identifying a web resource. As you can understand from the name its Uniform Resource Locator its platform independent. A user can use this info in any kind of platform.
Comments
use blob to save image files in data bases here is the code in java create table as
CREATE TABLE save_image (
id int(5) NOT NULL auto_increment,
name varchar(25) default NULL,
city varchar(20) default NULL,
image blob,
Phone varchar(15) default NULL,
PRIMARY KEY (`id`)
);
java file here to save image by user
import java.sql.*;
import java.io.*;
class SaveImageToDatabase {
public static void main(String[] args) throws SQLException {
// declare a connection by using Connection interface
Connection connection = null;
/* Create string of connection url within specified format with machine
name, port number and database name. Here machine name id localhost
and database name is mahendra. */
String connectionURL = "jdbc:mysql://localhost:3306/mahendra";
/*declare a resultSet that works as a table resulted by execute a specified
sql query. */
ResultSet rs = null;
// Declare prepare statement.
PreparedStatement psmnt = null;
// declare FileInputStream object to store binary stream of given image.
FileInputStream fis;
try {
// Load JDBC driver "com.mysql.jdbc.Driver"
Class.forName("com.mysql.jdbc.Driver").newInstance();
/* Create a connection by using getConnection() method that takes
parameters of string type connection url, user name and password to
connect to database. */
connection = DriverManager.getConnection(connectionURL, "root", "root");
// create a file object for image by specifying full path of image as parameter
File image = new File("C:/image.jpg");
/* prepareStatement() is used for create statement object that is
used for sending sql statements to the specified database. */
psmnt = connection.prepareStatement
("insert into save_image(name, city, image, Phone) "+ "values(?,?,?,?)");
psmnt.setString(1,"SmashCode");
psmnt.setString(2,"GPU");
psmnt.setString(4,"127001");
fis = new FileInputStream(image);
psmnt.setBinaryStream(3, (InputStream)fis, (int)(image.length()));
/* executeUpdate() method execute specified sql query. Here this query
insert data and image from specified address. */
int s = psmnt.executeUpdate();
if(s>0) {
System.out.println("Uploaded successfully !");
}
else {
System.out.println("unsucessfull to upload image.");
}
}
// catch if found any exception during rum time.
catch (Exception ex) {
System.out.println("Found some error : "+ex);
}
finally {
// close all the connections.
connection.close();
psmnt.close();
}
}
}
you can retrieve image you may refer this site here here
i used mostly code here hope my work makes you happy thumbs up my answer if you like