aspose file tools*
The moose likes JDBC and the fly likes how to insert an image into MYSQL database using java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "how to insert an image into MYSQL database using java " Watch "how to insert an image into MYSQL database using java " New topic

how to insert an image into MYSQL database using java

Srinivas Saranu

Joined: Oct 02, 2002
Posts: 7
i want to insert an image to MYSQL database using a java program.can anybody tell me how to do this
thanks to all in advance
Matthew Phillips
Ranch Hand

Joined: Mar 09, 2001
Posts: 2676
It is generally recommended that instead of inserting images into a db that you save the image to a folder and insert a link to that folder in the db.

Matthew Phillips
Ken Shamrock
Ranch Hand

Joined: Jan 23, 2002
Posts: 139
But if I don't want people can guess the link of the photo/binary data, I think inserting into the db directly is a good idea (?)

Thanks everyone who helped me
Chris Stewart
Ranch Hand

Joined: Sep 14, 2002
Posts: 184
Once you see the performance, you'll take Matthew's idea instead.
saager mhatre
Ranch Hand

Joined: Dec 20, 2000
Posts: 61
Matthew and james are right on the performance issue, besides MySQL isn't designed to handle BLOB's or Object Types. You'd have to use an Object RDBMS like Cloudscape or any other RDBMS that supports BLOB's.
David Yutzy
tumbleweed and gunslinger
Ranch Hand

Joined: Jun 29, 2001
Posts: 192
Although I agree about the performance issue inserting into a DB, the comment made about mySQL not handling images is incorrect.
There are many PHP apps. that do it all the time.
* I've done in in Java by uploadig the file to a
temp folder/file
* Use the mySQL LOADFILE() function call in the SQL

I've had various refresh and other issues trying to insert directly using Stream. Loading from an uploaded file was the most stable.
Again, I don't really recommend that way, but, it works...
[ November 26, 2002: Message edited by: David Yutzy ]
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
hi david,
i am not pretty sure but what saager referred to might have to do with MySQL JDBC Driver and not the database itself...
saager, david, any corrections for me here?
saranya sen

Joined: Nov 07, 2009
Posts: 1
import java.sql.*;

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"

/* 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(?,?,?,?)");
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.

Datatype for image is Blob
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 38363
Welcome to JavaRanch

I am afraid I can't read your code because you hadn't found the CODE button. It will be easier if you go to your post with the EDIT button and add the CODE tags to your quoted code. And always consider whether it is useful to add to a thread nearly 7 years old
I agree. Here's the link:
subject: how to insert an image into MYSQL database using java