read data from excel file and update database using jsp
Hi, I am using a MySQL database for scientific research analysis. My team members sends research data in excel file format and I need to update MySQL tables with new experimental data. How can I upload excel file and update database using JSP ?
Thanks in Advance
import java.io.File; import java.io.IOException; import java.util.Date; import java.util.Iterator; import java.util.List;
import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem; import org.apache.commons.fileupload.FileItemFactory; import org.apache.commons.fileupload.FileUploadException; import org.apache.commons.fileupload.disk.DiskFileItemFactory; import org.apache.commons.fileupload.servlet.ServletFileUpload; import org.apache.commons.io.FilenameUtils;
public class Upload extends HttpServlet { private static final long serialVersionUID = 1L;
String saveFile="G:/uploaded/"; protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try{ boolean isMultipart=ServletFileUpload.isMultipartContent(request); if(!isMultipart){ }else{ FileItemFactory item=new DiskFileItemFactory(); ServletFileUpload upload=new ServletFileUpload(item); List items=null; try{ items=upload.parseRequest(request); }catch(FileUploadException e){ e.getMessage(); } Iterator itr=items.iterator(); while(itr.hasNext()){ FileItem itemname=(FileItem) itr.next(); if(itemname.isFormField()){ }else{ String filename=itemname.getName(); String filename1=FilenameUtils.getName(filename); File f=new File(filename1); itemname.write(f); } } } }catch(Exception e){ } }
}
import java.io.File; import java.io.IOException; import java.util.Date; import java.util.Iterator; import java.util.List;
import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem; import org.apache.commons.fileupload.FileItemFactory; import org.apache.commons.fileupload.FileUploadException; import org.apache.commons.fileupload.disk.DiskFileItemFactory; import org.apache.commons.fileupload.servlet.ServletFileUpload; import org.apache.commons.io.FilenameUtils;
public class Upload extends HttpServlet { private static final long serialVersionUID = 1L;
String saveFile="G:/uploaded/"; protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try{ boolean isMultipart=ServletFileUpload.isMultipartContent(request); if(!isMultipart){ }else{ FileItemFactory item=new DiskFileItemFactory(); ServletFileUpload upload=new ServletFileUpload(item); List items=null; try{ items=upload.parseRequest(request); }catch(FileUploadException e){ e.getMessage(); } Iterator itr=items.iterator(); while(itr.hasNext()){ FileItem itemname=(FileItem) itr.next(); if(itemname.isFormField()){ }else{ String filename=itemname.getName(); String filename1=FilenameUtils.getName(filename); File f=new File(filename1); itemname.write(f); } } } }catch(Exception e){ } }
}
Ads