• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to insert an image into MYSQL database using java

 
Srinivas Saranu
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
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
Posts: 2676
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ken Shamrock
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 (?)
 
Chris Stewart
Ranch Hand
Posts: 184
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Once you see the performance, you'll take Matthew's idea instead.
 
saager mhatre
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
like:

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
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
regards
maulin
 
saranya sen
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,"mahendra");
psmnt.setString(2,"Delhi");
psmnt.setString(4,"123456");
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();
}
}
}


Datatype for image is Blob
 
Campbell Ritchie
Sheriff
Posts: 48374
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic