• 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

Inserting Files to Database.

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there. I have a Java program which allows the user to select a File from there desktop.

I then want to save this in my database, where I have a column which is of type IMAGE.

My query is "INSERT INTO MYTABLE VALUES(199,'This is a file' , " + getByteArray(files.get(0)) + ")";

This does not work, is their a better way to do it?
 
Ranch Hand
Posts: 344
Oracle Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What DB are you using? You'll have to create a java.util.PreparedStatement and use something like setBlob() to set the binary contents for the image column.
 
Owen Martin
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Its a SQL database. Using SQL Server.

Thanks will give it a try. Why does it only work with a prepared statement, do you have an example?
 
Owen Martin
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have got closer, but it does not seem to work.



Any help? Much needed!

The column is of type binary(50) where the stream is supposed to go.
 
Koen Aerts
Ranch Hand
Posts: 344
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is your issue/error... any details? Also, it's probably better to also specify the column names that you want to insert values into. For instance "INSERT INTO CAR (REG_NO, NOTE, IMG, CATEGORY, SOMETHING_ELSE) VALUES(?,?,?,?,?)".
 
Owen Martin
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It was due to not having the column as varbinary(max) apparently. Thanks.
 
Owen Martin
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have one more question didn't think it was worth a thread.

Say I execute an update statement to insert a new row into a table, I immediately need to use the generated id of that row. How do I retrieve it seen as an update doesn't produce a result set?
 
Koen Aerts
Ranch Hand
Posts: 344
Oracle Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Something along the lines like this:
 
reply
    Bookmark Topic Watch Topic
  • New Topic