• 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 an image in to database

 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hai All,

i want to insert a picture into the database. But i can't use PrepareStatement since my query is dynamic. Can any one help me to do the above using Statement , im using MySQL as database

Thanks & Regards
Anuja K
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm still having trouble with the fact that you can't use a PreparedStatement because your insert is "dynamic"... Can you give a concrete example? Perhaps there is a way to use PreparedStatement after all...
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Anuja,
I agree with Jeff that you can probably use a PreparedStatement. It may be a PreparedStatement that is used exactly once if you really have no common structure.

Take a look at BLOBs for the picture.
 
anuja Edathu
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I do have a master table in which some fields are optional including the picture , a user can enter certain details or prefer not to. So my query will be dynamically generated one according to the user inputs. from what i understood PreparedStatements are pre-compiled . what i am currently doing is inserting the all other details using a dynamically generated query and taking the auoto generated id from the database then update the particular column .I want to avoid this repeated access to database for accessing the same table .

here is the code im working with .please correct me if i am doing it wrongly


String query = "INSERT INTO DETAILS_MASTER(ADDRESS_1,ADDRESS_2,ADDRESS_3";
String queryTail = ")VALUES('"
+ address1
+ "','"
+ address2
+ "','"
+ address3
+ "'" ;
if(email!=""){
query = query+ ",E-MAIL";
queryTail = queryTail + ","+ email;
}

query = query+ queryTail + ")";

Statement stmt = con.createStatement();
int insertCount = stmt.executeUpdate(query);
query = "SELECT MAX(D_ID) FROM DETAILS_MASTER";
String DID = null;
ResultSet rs = stmt.executeQuery(query);
while (rs.next())
DID = Integer.toString(rs.getInt(1));
if(photoName!=null){
PreparedStatement ps = con.prepareStatement("UPDATE DETAILS_MASTER SET PICTURE = ? where D_ID =? " );

File image = new File( photoName);
FileInputStream fis = new FileInputStream( image ); ps.setBinaryStream( 1, fis, ( int )image.length() );
ps.setInt(2,Integer.parseInt(DID));
int count = ps.executeUpdate() ;
ps.close();
}


Thanks and Regards
Anuja K
 
Jeanne Boyarsky
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That could still be a prepared statement. The driver would cache it as two statements - one with the e-mail and one without. But the database would cache the execution plan. There is still a benefit to using a prepared statement with the code you posted.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic