Results 1 to 9 of 9
Like Tree2Likes
  • 1 Post By gimbal2
  • 1 Post By JosAH

Thread: store the image into database in netbeans

  1. #1
    chikku is offline Member
    Join Date
    Jan 2014
    Posts
    4
    Rep Power
    0

    Arrow store the image into database in netbeans

    I have a image package consisting of images and they are displayed in a jlabel.I used the code below for that

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

    Image im=new ImageIcon(this.getClass()
    .getResource("/images/" + imageNames[index])).getImage();
    ImageIcon iconLogo = new ImageIcon(im);
    jLabel5.setIcon(iconLogo);
    }
    Now I want to store the image displayed in the jlabel into the database(oracle).From the above code I dont know how to get the absolute path of the images.can anyone help to store the image as blob datatype in the database.Created a table hard(name varchar2(10),img blob).Thank you.

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    757
    Rep Power
    8

    Default Re: store the image into database in netbeans

    Show us some code on what you have tried so far? We might be able to help.

  3. #3
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,654
    Rep Power
    5

    Default Re: store the image into database in netbeans

    There isn't necessarily an absolute path, the image is loaded from the classpath which might just be inside a jar or somewhere else "not directly on disc". You could create a ClassLoader that loads stuff from the internet if you'd want.

    But that doesn't matter, you don't need an absolute path. Only an InputStream. And guess what: there is also a getResourceAsStream() method.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  4. #4
    chikku is offline Member
    Join Date
    Jan 2014
    Posts
    4
    Rep Power
    0

    Default Re: store the image into database in netbeans

    Sir,I searched a lot.But I didnt get a solution for this.See in the above code image object 'im' is displayed on the jlabel.By converting this 'im' into bytes I can store it into the database.But I have no idea about how to convert 'im' to bytes?

  5. #5
    chikku is offline Member
    Join Date
    Jan 2014
    Posts
    4
    Rep Power
    0

    Default Re: store the image into database in netbeans

    @gimbal2:I am taking the images from a image folder within the project.But I dont know how to convert the
    Image im=new ImageIcon(this.getClass().getResource("/images/" + imageNames[index])).getImage();
    into some other form to store into the db.

  6. #6
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,654
    Rep Power
    5

    Default Re: store the image into database in netbeans

    I can only conclude that you did not in fact write the code that you posted but you just copy/pasted it from somewhere, or else my earlier comment would have made sense to you.

    Java Code:
    this.getClass().getResource("/images/" + imageNames[index])
    With this code you load the image from the classpath.

    In this case you're using getResource(), but there is also a getResourceAsStream() which will provide you an InputStream with which you can load the image again so the bytes can be transmitted to the database. If you don't know how to do that, I suggest you get a good book on JDBC and to do some reading on how to use IO streams.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  7. #7
    chikku is offline Member
    Join Date
    Jan 2014
    Posts
    4
    Rep Power
    0

    Default Re: store the image into database in netbeans

    ok sir
    Last edited by chikku; 01-28-2014 at 02:49 PM.

  8. #8
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,654
    Rep Power
    5

    Default Re: store the image into database in netbeans

    Quite advanced stuff for someone who is very new. I'd start with the basics first if I were you, cutting corners is only going to make you confused and angry.
    PhHein likes this.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,336
    Blog Entries
    7
    Rep Power
    20

    Default Re: store the image into database in netbeans

    I don't think the OP can manage; at least to me that blob interface works 'the other way around'; you get an OutputStream that is supposed to write content to the blob and an InputStream retrieves a value from an existing blob; I once managed to get the stuff working by using PipedOutputStreams and PipedInputStreams that encapsulated the other streams ... quite tricky and there is no 'command' as the OP might have hoped for, that does the trick ...

    kind regards,

    Jos
    chikku likes this.
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. How do i store an array from database?
    By iNko in forum New To Java
    Replies: 7
    Last Post: 12-10-2012, 01:21 PM
  2. Replies: 4
    Last Post: 12-07-2012, 11:44 AM
  3. How to Store Gujarati Content in database.
    By jatinkansagara in forum JDBC
    Replies: 9
    Last Post: 07-04-2011, 10:49 AM
  4. how to store images in access database
    By sridarshan in forum JDBC
    Replies: 11
    Last Post: 06-26-2010, 02:26 AM
  5. Replies: 0
    Last Post: 08-15-2007, 01:48 PM

Tags for this Thread

Posting Permissions

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