Results 1 to 13 of 13
  1. #1
    Thennarasu's Avatar
    Thennarasu is offline Member
    Join Date
    Jan 2009
    Location
    Chennai, Tamilnadu
    Posts
    39
    Rep Power
    0

    Exclamation insert image to oracle using hibernate

    hi everyone,
    how to store and retrieve image into oracle using hibernate(java).

    Post your replies as soon as possible.

  2. #2
    Thennarasu's Avatar
    Thennarasu is offline Member
    Join Date
    Jan 2009
    Location
    Chennai, Tamilnadu
    Posts
    39
    Rep Power
    0

    Unhappy

    helllo any one plz help for this thread

  3. #3
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    8

    Default

    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.

  4. #4
    Thennarasu's Avatar
    Thennarasu is offline Member
    Join Date
    Jan 2009
    Location
    Chennai, Tamilnadu
    Posts
    39
    Rep Power
    0

    Exclamation 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..

  5. #5
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    8

    Default

    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.

  6. #6
    Thennarasu's Avatar
    Thennarasu is offline Member
    Join Date
    Jan 2009
    Location
    Chennai, Tamilnadu
    Posts
    39
    Rep Power
    0

    Default

    :)
    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...

  7. #7
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    8

    Default

    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.

  8. #8
    Thennarasu's Avatar
    Thennarasu is offline Member
    Join Date
    Jan 2009
    Location
    Chennai, Tamilnadu
    Posts
    39
    Rep Power
    0

    Default

    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??? :)

  9. #9
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    8

    Default

    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.

  10. #10
    Thennarasu's Avatar
    Thennarasu is offline Member
    Join Date
    Jan 2009
    Location
    Chennai, Tamilnadu
    Posts
    39
    Rep Power
    0

    Question

    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??? :)

  11. #11
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    8

    Default

    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.

  12. #12
    Thennarasu's Avatar
    Thennarasu is offline Member
    Join Date
    Jan 2009
    Location
    Chennai, Tamilnadu
    Posts
    39
    Rep Power
    0

    Default

    Can U Give Me A Sample Program?
    One Life!!! Y Serious??? :)

  13. #13
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    8

    Default

    Well, I don't have an Oracle database handy.

    Looking at the code that reads the image back, it looks OK. Do exactly the same, in reverse. Open the image file, read the bytes, write them to the blob.

Similar Threads

  1. Replies: 3
    Last Post: 07-12-2010, 08:15 AM
  2. How to insert java Object in oracle database
    By Thilkumar82 in forum Advanced Java
    Replies: 9
    Last Post: 08-13-2008, 12:33 PM
  3. Probeleme with insert into database oracle
    By bachtoutou in forum New To Java
    Replies: 0
    Last Post: 05-24-2008, 12:56 PM
  4. Replies: 0
    Last Post: 02-10-2008, 12:25 PM
  5. can hibernate work with Oracle 10g
    By javadev in forum JDBC
    Replies: 4
    Last Post: 07-08-2007, 09:47 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •