File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes I/O and Streams and the fly likes Uploading and Retrieving images using Java/JDBC Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Uploading and Retrieving images using Java/JDBC" Watch "Uploading and Retrieving images using Java/JDBC" New topic
Author

Uploading and Retrieving images using Java/JDBC

raghu dubey
Ranch Hand

Joined: Jul 05, 2007
Posts: 72
Hi

I do not know where to go. Can anyone please help me.

I am writing a swing based Java program and need to upload a photo into mySQL DB and retrieve it and show it on the desktop application. Its a simple application and does not use any JSP servlet libraries. I tried using FileInputStream and ByteArry inputStream but they do not seem to work.

Please help!!

Thanks
Raghu.


Thanks,<br />Raghu.<br /> <br />SCJP 5, SCWCD 1.4, NCFM
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41852
    
  63
Welcome to JavaRanch.

Storing images in a DB is possible, but I wouldn't generally recommend it, especially not if there are lots of images to store.

If you're set on doing it, you'd generally use java.sql.BLOB as the attribute type, which lets you handle the data using streams.


Ping & DNS - my free Android networking tools app
raghu dubey
Ranch Hand

Joined: Jul 05, 2007
Posts: 72
Thanks Ulf
I will try this. is ti possible for you to show some code snippet for doing it please.
raghu dubey
Ranch Hand

Joined: Jul 05, 2007
Posts: 72
Thanks a lot dear,

I am now able to upload and retrieve images: Here is the code that I use:

==========import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.PreparedStatement;
import java.sql.Statement;
import java.sql.Blob;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.*;

class MySqlInsertBlob {
FileOutputStream image;
Connection conn = null;
PreparedStatement pstmt = null;
Statement stmt= null;
ResultSet res = null;
StringBuffer query=null;
String filename ="c:/backblue.gif";

public MySqlInsertBlob(){

try{

query=new StringBuffer("insert into blobs(filename,binarydata,name) values (?,?,?)");
File file= new File(filename);

Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql","root","raghu123");
stmt=conn.createStatement();
pstmt=conn.prepareStatement(query.toString());
pstmt.setString(1, filename);
pstmt.setBinaryStream(2, new FileInputStream(filename),(int)file.length());
pstmt.setString(3,"silviya");
pstmt.executeUpdate();
System.out.println("Successfully inserted into BLOBS .....");

ResultSet rs=stmt.executeQuery("select * from blobs where name='silviya'");
if (rs.next()) {
Blob test=rs.getBlob("binarydata");
InputStream x=test.getBinaryStream();
int size=x.available();
OutputStream out=new FileOutputStream("c:/xyz.gif");
byte b[]= new byte[size];
x.read(b);
out.write(b);


}
}catch(ClassNotFoundException cnfe){

System.out.println("ClassNotFoundException occured :"+cnfe);
}catch(SQLException se){

System.out.println("SQLException occured :"+se);
}catch(FileNotFoundException fe){

System.out.println("File Not Found Exception occured :"+fe);
}catch(Exception e){

System.out.println("Exception occured :"+e);
}finally{
try{
stmt.close();
conn.close();
}catch(Exception e){}
}

=================

Raghu.
Albert Hopkins
Greenhorn

Joined: Jul 21, 2007
Posts: 21
Hello there..
I tried your code.

When compile, it says that "reached end of file while parsing"

why is that??
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8907
    
    8

Since raghu hasn't posted since July, I'll field this one.
Try matching up the braces. His code is missing one.


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Uploading and Retrieving images using Java/JDBC