aspose file tools*
The moose likes JDBC and the fly likes inserting an image in to database Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "inserting an image in to database" Watch "inserting an image in to database" New topic
Author

inserting an image in to database

anuja Edathu
Ranch Hand

Joined: Sep 09, 2005
Posts: 84
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

Joined: Sep 16, 2005
Posts: 1780
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...


There is no emoticon for what I am feeling!
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30917
    
158

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.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
anuja Edathu
Ranch Hand

Joined: Sep 09, 2005
Posts: 84
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

Joined: May 26, 2003
Posts: 30917
    
158

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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: inserting an image in to database