Refresh Your Java - Before Java Interview
We are Agile, believe in less Documentation - Only Quick notes (Java Interview Questions) of Java/J2ee Read more....
Store Attachment as BLOB:
This Example shows
how to read an attachemt (File) by using Struts FormFile
and store that as a Blob.
Struts Action Class:
import
org.apache.struts.upload.FormFile;
import
jakarta.servlet.http.HttpServletRequest;
import
jakarta.servlet.http.HttpServletResponse;
import
org.apache.commons.logging.Log;
import
org.apache.commons.logging.LogFactory;
import
org.apache.struts.action.ActionForm;
import
org.apache.struts.action.ActionForward;
import
org.apache.struts.action.ActionMapping;
import
org.apache.struts.upload.FormFile;
class
AttachementSaver extends ButtonDispatchAction {
public void
readFormFile(ActionMapping mapping, ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {
FormFile fileAttached = (FormFile)
PropertyUtils.getSimpleProperty(form,
"attachment" + i);
if(fileAttached != null &&
fileAttached.getFileSize()>0)
{
AttachmentServices.updateAttachmentClob(fileAttached,
id);
}
}
}
AttachmentServices:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import
java.io.InputStream;
import
java.io.InputStreamReader;
import
java.io.OutputStream;
import
java.io.OutputStreamWriter;
import
java.math.BigDecimal;
import
java.net.MalformedURLException;
import
java.sql.PreparedStatement;
import
java.sql.SQLException;
import
java.util.Date;
import
java.util.Iterator;
import
java.util.List;
import java.util.Set;
public class AttachmentServices () {
//This method need to be modified once
ATTACHMENT column changed to blob
//1. ATTACHMENT DEFAULT EMPTY_BLOB()
- CREATE THE TABLE DEFAULT TO EMPTY_BLOB
//2. SELECT ATTACHMENT FROM TABLE_ATTACHMENT
FOR UPDATE
//3. Open the recordet like
rs.getBlob("ATTACHMENT").setBytes(fileAttached.getFileData());
//4. Step3 will store the bytes to ATTACHMENT
column...
public static
void updateAttachmentClob(FormFile fileAttached,Double
id) throws DataAccessException {
try {
Session session
= HibernateConfig.getSession();
String filetype
= null;
if(fileAttached.getFileName() != null &&
fileAttached.getFileName().indexOf(".")>0)
filetype =
fileAttached.getFileName().substring(fileAttached.getFileName().indexOf(".")+1);
String hql =
"update TABLE_ATTACHMENT set ATTACHMENT
=?,ATTACHED_FILE=?, TYPE=? where OBJID =
?";
PreparedStatement ps =
session.connection().prepareStatement(hql);
ps.setString(1, new
String(fileAttached.getFileData()));
ps.setString(2,
fileAttached.getFileName());
ps.setString(3,
filetype);
ps.setBigDecimal(4, attachment.getObjid());
int rowCount =
ps.executeUpdate();
}
catch(IOException
e)
{
log.error("unhandled file excetpion.",e);
e.printStackTrace();
}
catch(RuntimeException
re) {
} catch
(SQLException e) {
e.printStackTrace();
}
}
}
This Portal is intended to put all Java/J2ee related topics at one single place for quick referance, not only Technical , but also the Project Management Related thing such as Development Process methodoogies build process, unit testing etc.,
This Portal has More than 500 Java Interview Questions (also could be Considered as Quick Notes) very neatly separated topic by topic with simple diagrams which makes you easily understandable. Importantly these are from our Realtime expericance.