• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Saving a jpg image to derby Blob

 
Ranch Hand
Posts: 74
1
  • Number of slices to send:
    Optional 'thank-you' note:
I have been able to upload a jpg image and display it within a jlabel. Somehow, the image doesn't save to the derby database which consists of 3 fields: SLIDEID, SLIDENAME, IMAGEJPG. the first two are string and the last one (IMAGEJPG) is a Blob. I have tried various tutorials with no effect. Just need the jpg image saved to the derby database. This is part of my original progam for developing an interactive tasks slide images with step by step directives. The program does load the images with no problem --just doesn't insert it into the database.

 
 
Saloon Keeper
Posts: 10797
86
  • Number of slices to send:
    Optional 'thank-you' note:
What tells you that it's not getting inserted into the database?

An imageicon is not a jpg image.
 
Carey Brown
Saloon Keeper
Posts: 10797
86
 
Carey Brown
Saloon Keeper
Posts: 10797
86
 
Carey Brown
Saloon Keeper
Posts: 10797
86
  • Number of slices to send:
    Optional 'thank-you' note:
You are calling executeUpdate() twice.
 
Marshal
Posts: 28262
95
  • 1
  • Number of slices to send:
    Optional 'thank-you' note:
I don't understand the purpose of the ObjectOutputStream. Even if it does manage to contain the bytes of the image correctly, it's also going to contain a bunch of Java serialization information. I would look instead for something which will just contain the bytes of the image. (The bytes which you read earlier from the file, in other words.)
 
Marshal
Posts: 4533
572
  • Number of slices to send:
    Optional 'thank-you' note:
I don't know about the Swing side of things, but you can take a look at the saveImageToDatabase and getImageFromDatabaseAsByteArray methods for examples to put and get images with the database.  I don't think you were far off, but you took a wrong turn with ObjectOutputStream.
 
Marshal
Posts: 79423
377
  • Number of slices to send:
    Optional 'thank-you' note:

Ron McLeod wrote:I don't know about the Swing side of things . . .

The file chooser is used correctly, but I would use the option pane differently. There are more details in the Java® Tutorials.
 
Campbell Ritchie
Marshal
Posts: 79423
377
  • Number of slices to send:
    Optional 'thank-you' note:
By the way: why are you using an accessory Component to the file chooser? I think that is probably unnecessary.
 
Saloon Keeper
Posts: 27868
196
  • Number of slices to send:
    Optional 'thank-you' note:

Paul Clapham wrote:I don't understand the purpose of the ObjectOutputStream. Even if it does manage to contain the bytes of the image correctly, it's also going to contain a bunch of Java serialization information. I would look instead for something which will just contain the bytes of the image. (The bytes which you read earlier from the file, in other words.)


Don't ever save data in java Serialization format. It is not formally defined, and odds are very high that one version of a JVM will not be able to read serialized objects saved by another one.

Java serialization is best used only internal to a single app. It has been used for Remote Method Invocation (RMI) data transmission, but that's how I learned (the hard way) about what happens when 2 different JVMs try to serialize to each other.

Use ByteArrayOutputStream instead.
 
Bill Melendez
Ranch Hand
Posts: 74
1
  • Number of slices to send:
    Optional 'thank-you' note:
@Carey: I can open the database within Netbeans and see what fields were filled.
@Ron: Thank you for your code input. Obviously you know what you're talking about. However, I need to be able to work within the constraints of a GUI based (Netbeans) desktop solution -which makes it hard to transpose your code to fit. I have a jbutton for loading the image (which works fine) and a jbutton for saving the image (which doesn't work). The saving the image jbutton method uses the displayed JLABEL image for saving to the derby database Blob. The program is a utility tool for me to get the images to database and will be essential for other non-coding individuals to use for making changes to the photo images of the products and install instructions. Hope that helps better clarify the goal behind this program.  
 
Carey Brown
Saloon Keeper
Posts: 10797
86
  • Number of slices to send:
    Optional 'thank-you' note:
@Bill, in your 1st post it doesn't work because you define a "blob" object and then you serialize the ImageIcon but you never put the serialized data inside the "blob".

But, as mentioned, your approach is not a good one because serialization has issues reading objects serialized with older versions of Java (today's current version is tomorrow's older version). You don't say what the end use of the edited image is. If you are going to use a 3rd party program to print it (for example) you'd need a real JPG file which an ImageIcon is not. You'd be better off reading and writing byte arrays of a JPG file using a Blob.
 
Paul Clapham
Marshal
Posts: 28262
95
  • Number of slices to send:
    Optional 'thank-you' note:

Bill Melendez wrote:I have a jbutton for loading the image (which works fine) and a jbutton for saving the image (which doesn't work).



You're thinking of the "image" as something your eyes see on a screen. Which is one way to look at it (pun not intended). But it makes little sense to save what your eyes are seeing. Instead you need to save the internal representation of the image, which is simply an array of bytes. At least, that's what those other individuals would need when editing the images; a serialized Java object would be useless to them.

So "loading the image" constitutes reading the array of bytes from a file and converting it into what your eyes see on the screen. "Saving the image" constitutes saving the array of bytes.

Right now you "load the image" all in one step and as a result you lose track of the array of bytes, so it isn't available to be saved. Ron's suggestion was to read the array of bytes again when you want to save the image. I don't see what's the problem with that but if you want another solution, then change your "loading the image" code to read the contents of the file into an array of bytes (which can then be used when you save the image) and then use those bytes to produce the Java object.
 
Campbell Ritchie
Marshal
Posts: 79423
377
  • Number of slices to send:
    Optional 'thank-you' note:

Paul Clapham wrote:. . . it makes little sense to save what your eyes are seeing. . . .

Since what you see is implemented by impulses in the brain, it is something dynamic that cannot be saved in that format. That is completely different from a digital represntation.
 
Paul Clapham
Marshal
Posts: 28262
95
  • Number of slices to send:
    Optional 'thank-you' note:

Campbell Ritchie wrote:

Paul Clapham wrote:. . . it makes little sense to save what your eyes are seeing. . . .

Since what you see is implemented by impulses in the brain, it is something dynamic that cannot be saved in that format. That is completely different from a digital represntation.


Okay, it makes little sense to save the coloured pixels which your eyes are seeing then.
 
Bill Melendez
Ranch Hand
Posts: 74
1
  • Number of slices to send:
    Optional 'thank-you' note:
@Paul: Thanks for your input. Yes, I plan to save the image from the directory in which the jpg image resides. What I meant was that the image being displayed would be what is being saved into the database --just will be using the same source (image file) from which the image was posted to the JLabel. Sorry for the confusion. The LOAD and the SAVE functions need to be in separate methods since an individual jbutton selects each. There is no editing of the image (I use Photoshop for that). Again my humble thanks for all you guy's inputs.
 
Paul Clapham
Marshal
Posts: 28262
95
  • Number of slices to send:
    Optional 'thank-you' note:

Bill Melendez wrote:@Paul: Thanks for your input...



So you're okay now? (I mean, you mentioned that things would be done in different methods but hopefully you don't consider that a barrier.)
 
Tim Holloway
Saloon Keeper
Posts: 27868
196
  • Number of slices to send:
    Optional 'thank-you' note:
Just as an observation, Java doesn't generally deal with images as raw data. Mostly it deals with images as "image data" objects and uses ImageIO to handle stuff like getting images into and out of byte sequences.
 
Carey Brown
Saloon Keeper
Posts: 10797
86
  • Number of slices to send:
    Optional 'thank-you' note:
@Bill, I'm feeling a bit dense here, I'm hard pressed to suggest code design without totally understanding how the app is to be used. I'm coming away with two possible scenarios:
  • The app is an index to JPG files and stores a thumbnail of the file in a database. When the user goes to edit the image it retrieves the original JPG file from disk and opens it in Photoshop.
  • The app loads and stores JPG files in a database so that a user can browse and select an image for editing. When selected, the JPG file will be retrieved from the database, written to disk, and then Photoshop will be started to edit it.
  •  
    Tim Holloway
    Saloon Keeper
    Posts: 27868
    196
    • Number of slices to send:
      Optional 'thank-you' note:
    Then this might help:

    https://gogs.mousetech.com/mtsinc7/gourmetj-springboot/src/branch/main/src/main/java/com/mousetech/gourmetj/springweb/PictureController.java

    The gourmetj webapp allows the (authorized!) user to upload an image and creates a thumbnail of it. The PictureController is responsible for most of that stuff, which is why I'm supplying a direct link to it.

    This is a JPA webapp, and it has been run using both SQLite and MySQL database backends.
     
    Bill Melendez
    Ranch Hand
    Posts: 74
    1
    • Number of slices to send:
      Optional 'thank-you' note:
    @Carey:  "The app loads and stores JPG files in a database so that a user can browse and select an image for editing. When selected, the JPG file will be retrieved from the database, written to disk, and then Photoshop will be started to edit it."

    You're close. The app does two things:(1) It loads the jpg image to a JLabel from a directory folder using the JFileChooser --no editing, just displays only. (2) The app Saves the jpg image being viewed to the derby database. It doesn't use the Jlabel display. Instead it gets the the same jpg image using the same location file it used to display the image. There is no Photoshop connection nor is Photoshop in any way linked to the java app -strictly java app only.

    The problem I'm having is getting Derby to accept the jpg image into the Blob field. It does this by using an action jbutton for "saving" to derby. The JButton calls the save image method.

    The only connection to the "load jpg image function" is the image's location in the directory folder (file path) that the JFileChooser gets when loading the image to JLabel.
     
    Ron McLeod
    Marshal
    Posts: 4533
    572
    • Number of slices to send:
      Optional 'thank-you' note:
    Bill - what version of Java are you working with?
     
    Bill Melendez
    Ranch Hand
    Posts: 74
    1
    • Number of slices to send:
      Optional 'thank-you' note:
    Zule OpenJDK 17 with Netbeans 12.5
     
    Bill Melendez
    Ranch Hand
    Posts: 74
    1
    • Number of slices to send:
      Optional 'thank-you' note:
    @Ron: The DB connection and save/insert function work okay for any "string entry" but when the Blob part is included within the PreparedStatement, nothing gets saved to the database.  
     
    Paul Clapham
    Marshal
    Posts: 28262
    95
    • Number of slices to send:
      Optional 'thank-you' note:
    Nothing gets saved to the database? Well, Carey did remark about this problematic code a long time ago:

    To be clear, if adding to the database results in an SQLException, then this code is run. You really ought to at least write something to a place where you can see it.
     
    Bill Melendez
    Ranch Hand
    Posts: 74
    1
    • Number of slices to send:
      Optional 'thank-you' note:
    @Paul: Thank you for your input. The string fields do get saved to the database but only if the Blob field doesn't exist in the database. I deleted the Blob field and all reference to it to confirm that the connection was working. It work fine with the first two entries --which are string entries. When the Blob field was added back on, nothing was saved. My conclusion is that the saving to Blob PreparedStatement is incorrectly coded. So the program goes straight to end of code or to the catch line without running the Blob lines. Even though the lines of code for the Blob are just a few, if they don't execute and the jpg image isn't saved, then the code lines are not effective in accomplishing my objective.
     
    Tim Holloway
    Saloon Keeper
    Posts: 27868
    196
    • Number of slices to send:
      Optional 'thank-you' note:
    I think that this is what you need, then (from the link given above)

    "Recipe" is a JPA Entity and the "thumb" and "image" fields are defined as byte[]. So basically, you'd do the same thing with raw JDBC except that instead of having the scaled and unscaled images as object properties, you'd just make them local variables. The parameter "bs" is a byte[] that in the webapp came in as part of the upload, but for a Swing app that was selecting a local file, you'd simply construct a byte array by opening the input file as a bytestream and reading it into RAM. Or cut out the middleman and change the "bs" parameter to pass in the "istream" directly.

    Since I'm storing both image and thumnail as BLOBs I have a special utility method that I can call to ensure that the stored image isn't too large and scales it down to no more than 512×512, but that's not essential.
     
    Paul Clapham
    Marshal
    Posts: 28262
    95
    • Number of slices to send:
      Optional 'thank-you' note:
    All of that (Bill's last post) could be correct. So may I assume that you added some code to tell you what exception was thrown? The idea of "going straight to end of code" is not realistic. An exception will be thrown. Tell us what it says.
     
    Tim Holloway
    Saloon Keeper
    Posts: 27868
    196
    • Number of slices to send:
      Optional 'thank-you' note:

    Paul Clapham wrote:All of that (Bill's last post) could be correct. So may I assume that you added some code to tell you what exception was thrown? The idea of "going straight to end of code" is not realistic. An exception will be thrown. Tell us what it says.


    Or to put it another way, can we assume that you didn't want me to hunt you down and commit violent acts if I ever had to maintain/debug this code bbecause you are simply eating the exceptions silently without logging, reporting, or otherwise handling them?
     
    Carey Brown
    Saloon Keeper
    Posts: 10797
    86
    • Number of slices to send:
      Optional 'thank-you' note:

    Carey Brown wrote:@Bill, in your 1st post it doesn't work because you define a "blob" object and then you serialize the ImageIcon but you never put the serialized data inside the "blob".

     
    Carey Brown
    Saloon Keeper
    Posts: 10797
    86
    • Number of slices to send:
      Optional 'thank-you' note:
    Here's an example of inserting a blob containing a binary file.
     
    Bill Melendez
    Ranch Hand
    Posts: 74
    1
    • Number of slices to send:
      Optional 'thank-you' note:
    @all  -- it seems to me it would easier if I posted the java files so everyone can see where the issues are. see attached.
     
    Bill Melendez
    Ranch Hand
    Posts: 74
    1
    • 1
    • Number of slices to send:
      Optional 'thank-you' note:
    @Carey --Worked perfectly. THANK YOU! I just used the portion that handled the Blob. Now I'm more knowledgeable on Blob to Derby insertion. I know everyone contributed and I am most grateful to all. Again Thank you!
     
    Consider Paul's rocket mass heater.
    reply
      Bookmark Topic Watch Topic
    • New Topic
    vceplus-200-125    | boson-200-125    | training-cissp    | actualtests-cissp    | techexams-cissp    | gratisexams-300-075    | pearsonitcertification-210-260    | examsboost-210-260    | examsforall-210-260    | dumps4free-210-260    | reddit-210-260    | cisexams-352-001    | itexamfox-352-001    | passguaranteed-352-001    | passeasily-352-001    | freeccnastudyguide-200-120    | gocertify-200-120    | passcerty-200-120    | certifyguide-70-980    | dumpscollection-70-980    | examcollection-70-534    | cbtnuggets-210-065    | examfiles-400-051    | passitdump-400-051    | pearsonitcertification-70-462    | anderseide-70-347    | thomas-70-533    | research-1V0-605    | topix-102-400    | certdepot-EX200    | pearsonit-640-916    | itproguru-70-533    | reddit-100-105    | channel9-70-346    | anderseide-70-346    | theiia-IIA-CIA-PART3    | certificationHP-hp0-s41    | pearsonitcertification-640-916    | anderMicrosoft-70-534    | cathMicrosoft-70-462    | examcollection-cca-500    | techexams-gcih    | mslearn-70-346    | measureup-70-486    | pass4sure-hp0-s41    | iiba-640-916    | itsecurity-sscp    | cbtnuggets-300-320    | blogged-70-486    | pass4sure-IIA-CIA-PART1    | cbtnuggets-100-101    | developerhandbook-70-486    | lpicisco-101    | mylearn-1V0-605    | tomsitpro-cism    | gnosis-101    | channel9Mic-70-534    | ipass-IIA-CIA-PART1    | forcerts-70-417    | tests-sy0-401    | ipasstheciaexam-IIA-CIA-PART3    | mostcisco-300-135    | buildazure-70-533    | cloudera-cca-500    | pdf4cert-2v0-621    | f5cisco-101    | gocertify-1z0-062    | quora-640-916    | micrcosoft-70-480    | brain2pass-70-417    | examcompass-sy0-401    | global-EX200    | iassc-ICGB    | vceplus-300-115    | quizlet-810-403    | cbtnuggets-70-697    | educationOracle-1Z0-434    | channel9-70-534    | officialcerts-400-051    | examsboost-IIA-CIA-PART1    | networktut-300-135    | teststarter-300-206    | pluralsight-70-486    | coding-70-486    | freeccna-100-101    | digitaltut-300-101    | iiba-CBAP    | virtuallymikebrown-640-916    | isaca-cism    | whizlabs-pmp    | techexams-70-980    | ciscopress-300-115    | techtarget-cism    | pearsonitcertification-300-070    | testking-2v0-621    | isacaNew-cism    | simplilearn-pmi-rmp    | simplilearn-pmp    | educationOracle-1z0-809    | education-1z0-809    | teachertube-1Z0-434    | villanovau-CBAP    | quora-300-206    | certifyguide-300-208    | cbtnuggets-100-105    | flydumps-70-417    | gratisexams-1V0-605    | ituonline-1z0-062    | techexams-cas-002    | simplilearn-70-534    | pluralsight-70-697    | theiia-IIA-CIA-PART1    | itexamtips-400-051    | pearsonitcertification-EX200    | pluralsight-70-480    | learn-hp0-s42    | giac-gpen    | mindhub-102-400    | coursesmsu-CBAP    | examsforall-2v0-621    | developerhandbook-70-487    | root-EX200    | coderanch-1z0-809    | getfreedumps-1z0-062    | comptia-cas-002    | quora-1z0-809    | boson-300-135    | killtest-2v0-621    | learncia-IIA-CIA-PART3    | computer-gcih    | universitycloudera-cca-500    | itexamrun-70-410    | certificationHPv2-hp0-s41    | certskills-100-105    | skipitnow-70-417    | gocertify-sy0-401    | prep4sure-70-417    | simplilearn-cisa    |
    http://www.pmsas.pr.gov.br/wp-content/    | http://www.pmsas.pr.gov.br/wp-content/    |