Results 1 to 13 of 13
- 01-05-2009, 05:16 AM #1
- 01-12-2009, 03:56 AM #2
helllo any one plz help for this thread
- 01-18-2009, 07:04 PM #3
I don't think Hibernate is designed for the purpose you are describing. If you want to store images in an Oracle database, you need to read the image bytes into memory and then write them out to the database, and vice versa.
I suggest using a header and detail table, chunking the image data and storing it in multiple detail records using a variable-length binary field. The header holds information about the image itself.
- 01-19-2009, 05:45 AM #4
comparing it with dotnet...
actually i need to display the image from database. i have used byte[] to convert to blob... but here again a prob raises... i can convert only gif file...my jpg images are not recognized by io reader class..
- 01-19-2009, 08:06 PM #5
I worked with a image database that contained millions of images, and I assume you are doing something similar. If that is the case, Hibernate is not a good approach.
Keep is simple. Read the data from the blob, which I assume provides you with an InputStream, and make it available to the display application.
I'd use a simple stream, such as buffered input stream, to put the raw bytes when storing the image (a one time process) and reading it (performed many times). You don't care what kind of data is in the stream. Save the file name, with extension, in a varchar field in the database, or at least the extension. That way, you don't care what kind of image it is.
If this is a Web application, you can stream the data to a File on the Web server in a directory that allows browser access and create an image tag that specifies the file. That's why I suggested saving the file name.
Note you will have to maintain the directory so that it does not fill up with too many files, but it also gives you the opportunity to cache frequently accessed images simply by leaving their files in the directory.
- 01-21-2009, 05:28 AM #6
:)
So i can use ordinary jdbc connection for retrieving image from database with the file name(with extension from varchar field ) .
thanks Steve...
its a timely help from you...
- 01-21-2009, 03:20 PM #7
Yes. You will have to look into how Oracle makes blob's accessible; other databases seem to provide a stream. If you want to write the data out to a File, you can just do the read a block/write a block technique.
- 01-22-2009, 06:15 AM #8
hi steve,
i am using FileInputstream to insert into DB...but the problem is it accepts only gif format.
private void uploadphoto(String upload)throws Exception {
//upload will have absolute path
System.out.println("ok "+i);
Class.forName("oracle.jdbc.driver.OracleDriver");
System.out.println("file data "+upload);
System.out.println("After "+upload);
Connection con=DriverManager.getConnection("jdbcracle:thin: @MT-TESTORADB:1521:ORACL","xxx", "xxx");
PreparedStatement pstmt = con.prepareStatement ("INSERT INTO temp1 VALUES (?,?)");
File fBlob=new File(upload);
FileInputStream is = new FileInputStream ( fBlob );
pstmt.setBinaryStream (1, is, (int) fBlob.length() );
pstmt.setInt(2,i);
pstmt.executeUpdate();
System.out.println("The End");
con.close();
// TODO Auto-generated method stub
}
this is my program...this accepts only gif format....One Life!!! Y Serious??? :)
- 01-22-2009, 02:39 PM #9
Glancing through your code, it looks right. The blob field in the database should now contain all the bytes from the file.
There is nothing about ".gif" in the code you showed here, just a File and byte streams. What you are doing will work with any image format, text, or a Word document.
When you read the data back, you need a File, and you create a FileOutputStream from it. What do you name the File? The extension must match the type image data.
Try adding another field to your database table, called fileName. Make it varchar(50). Store only the file name, not the path. You can obtain that name from fBlob. When you want to retrieve the data, create a new file in the output directory with the same name, and put the data back in there.
Also, be sure to close your streams, especially an output stream.
- 01-23-2009, 04:03 AM #10
Yes its working but still i have a problem.
I think the problem is with FileInputStream class.
Because if i give .gif file as input it works perfectly
but
if i give .jpg file as input it doesnt works, while retrieve
from DB, i get a 0kb file.One Life!!! Y Serious??? :)
- 01-23-2009, 02:50 PM #11
It sounds like the code to retrieve the images is working correctly. If you are retrieving 0 bytes, then check the database record to see how much data is in the blob. I'll be it's 0 bytes.
Most likely, the problem is the method that stores the data in the first place. Ideally, you would do the reverse of the logic that reads the record back. Open FileInputStream against the JPEG file, read the bytes, write the bytes to the blob using an BufferedOutputStream.
- 01-27-2009, 05:38 AM #12
Can U Give Me A Sample Program?
One Life!!! Y Serious??? :)
- 01-27-2009, 03:02 PM #13
Similar Threads
-
org.hibernate.exception.GenericJDBCException: could not insert
By Peter in forum JDBCReplies: 3Last Post: 07-12-2010, 07:15 AM -
How to insert java Object in oracle database
By Thilkumar82 in forum Advanced JavaReplies: 9Last Post: 08-13-2008, 11:33 AM -
Probeleme with insert into database oracle
By bachtoutou in forum New To JavaReplies: 0Last Post: 05-24-2008, 11:56 AM -
Using PreparedStatement to insert image into database
By Java Tip in forum Java TipReplies: 0Last Post: 02-10-2008, 11:25 AM -
can hibernate work with Oracle 10g
By javadev in forum JDBCReplies: 4Last Post: 07-08-2007, 08:47 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks