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