Hi Julian,
I have changed the code as below and it worked!!!
-------------------------- saving image to database -----------------------
private void savePayload(String filename,BufferedInputStream is) throws java.io.IOException{
int c;
PushbackInputStream input = new PushbackInputStream(is,128);
ByteArrayOutputStream blob = new ByteArrayOutputStream();
while ( (c=read(input,boundary)) >= 0 )blob.write( c );
saveBlob(filename,blob.toByteArray());
blob.close();
}
private void saveBlob(String filename,byte[] out){
Connection con = null;
PreparedStatement pstmt = null;
String sqlCmd = "INSERT INTO tblContent (FileName,BinaryData) VALUES(?,?)";
try {
Class.forName(jdbcDriver);
con = DriverManager.getConnection(dbUrl);
pstmt = con.prepareStatement(sqlCmd);
pstmt.setString(1, filename);
pstmt.setBytes(2, out);
pstmt.executeUpdate();
con.close();
}
catch(ClassNotFoundException e){
e.printStackTrace();
}
catch(SQLException e){
e.printStackTrace();
}
}
--------------------retrieving from database ---------------------------
public byte[] getBlob(int id){
String sqlQuery = "SELECT BINARYDATA FROM tblContent WHERE ContentID = ?;";
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
Blob blob = null;
byte[] bytes = null;
String description = "";
try {
Class.forName(jdbcDriver);
con = DriverManager.getConnection(dbUrl);
pstmt = con.prepareStatement(sqlQuery);
pstmt.setInt(1,id);
rs = pstmt.executeQuery();
ResultSetMetaData md = rs.getMetaData();
while (rs.next()) {
blob = rs.getBlob(1);
}
bytes = blob.getBytes( 1, (int)(blob.length()));
con.close();
}
catch(ClassNotFoundException e){
e.printStackTrace();
}
catch(SQLException e){
e.printStackTrace();
}
return bytes;
}
-----------------------------------------------------------------
You are right. I did not save image properly. Thank you very much!
Honour