Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

inserting an image in to database

 
anuja Edathu
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • 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
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • 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...
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34178
340
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • 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
  • 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
Marshal
Posts: 34178
340
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic