Results 1 to 8 of 8
  1. #1
    LAVANYAPESALA is offline Member
    Join Date
    Nov 2013
    Posts
    3
    Rep Power
    0

    Default how to upload excel sheet data into database using servlet

    how to upload excel sheet data into database.

    This is below code is there.This is code not inserting Dateofbirth column and YearOfPassing and Aggregate columns not inserting.instead of displaying 0 value.Where i did mistake.or any one have example code send me.


    Java Code:
    import java.io.BufferedInputStream;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.ArrayList;
    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.poi.hssf.usermodel.HSSFRow;
    import org.apache.poi.hssf.usermodel.HSSFSheet;
    import org.apache.poi.hssf.usermodel.HSSFWorkbook;
    import org.apache.poi.poifs.filesystem.POIFSFileSystem;
    
    
     
    public class FileUploadHandler extends HttpServlet {
    	private static final long serialVersionUID = 1L;
      
        @SuppressWarnings("unused")
    	@Override
        protected void doPost(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
          
        	List<RegistrationUser> usersList = new ArrayList<RegistrationUser>();
    		String filename = request.getParameter("fileName");
    		if(filename == null || filename.length() == 0){
    			request.setAttribute("error_msg", "Invalid file.Please select file.");
    			request.getRequestDispatcher("fileUpload.jsp").include(request, response);
    			return;
    		}
    		try {
    			// Read excel file and store email ids in mailIdsArray
    			InputStream input = new BufferedInputStream(new FileInputStream(filename));
    			POIFSFileSystem fs = new POIFSFileSystem(input);
    			HSSFWorkbook wb = new HSSFWorkbook(fs);
    			HSSFSheet sheet = wb.getSheetAt(0);
    
    			Iterator rows = sheet.rowIterator();
    			while (rows.hasNext()) {
    				RegistrationUser user= new RegistrationUser();
    				HSSFRow row = (HSSFRow) rows.next();
    				
    				
    				
    				if(row.getCell(0) != null && !row.getCell(0).equals("First Name")){
    					user.setFirstName(row.getCell(0).toString());
    					user.setLastName(row.getCell(1).toString());
    					user.setAddress(row.getCell(2).toString());
    					user.setDateOfBirth(row.getCell(3).toString());
    					user.setPhoneNumber(row.getCell(4).hashCode());
    					user.setEmailId(row.getCell(5).toString());
    					user.setGender(row.getCell(6).toString());
    					user.setMaritalStatus(row.getCell(7).toString());
    					user.setHighestDegree(row.getCell(8).toString());
    					user.setAggregate(row.getCell(9).hashCode());
    					user.setYearOfPassing(row.getCell(10).hashCode());
    					user.setCollegeName(row.getCell(11).toString());
    					user.setUniversityName(row.getCell(12).toString());
    					user.setTotalExperiences(row.getCell(13).hashCode());
    					user.setCurrentCompany(row.getCell(14).toString());
    					user.setDesignation(row.getCell(15).toString());
    					user.setReferredBy(row.getCell(16).toString());
    					
    				}
    				
    				
    				usersList.add(user);
    			}
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    Last edited by Tolls; 11-22-2013 at 01:04 PM.

  2. #2
    LAVANYAPESALA is offline Member
    Join Date
    Nov 2013
    Posts
    3
    Rep Power
    0

    Default Re: how to upload excel sheet data into database using servlet

    Above code i need to store in oracle database.

  3. #3
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,346
    Rep Power
    6

    Default Re: how to upload excel sheet data into database using servlet

    Uh, why are you using hashCode() ?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  4. #4
    LAVANYAPESALA is offline Member
    Join Date
    Nov 2013
    Posts
    3
    Rep Power
    0

    Default Re: how to upload excel sheet data into database using servlet

    excel sheet name i didn't specify.because i need to upload multipul excelsheets.please any one help me.

  5. #5
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,346
    Rep Power
    6

    Default Re: how to upload excel sheet data into database using servlet

    Quote Originally Posted by LAVANYAPESALA View Post
    excel sheet name i didn't specify.because i need to upload multipul excelsheets.please any one help me.
    That is not an answer to my question. Why are you trying to store the hashCode() of specific fields? Let me make it a little more clear for you: the hasCode() is NOT the value of the field.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,225
    Rep Power
    20

    Default Re: how to upload excel sheet data into database using servlet

    When posting code please wrap it in [code] tags [/code] so it retains its formatting.
    I've edited your post to do this just this once.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    ankita_01 is offline Member
    Join Date
    Oct 2014
    Posts
    1
    Rep Power
    0

    Default Re: how to upload excel sheet data into database using servlet

    This code is not working for me. It gives me exception connection reset. What should I do? Pls help ASAP

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,225
    Rep Power
    20

    Default Re: how to upload excel sheet data into database using servlet

    Show us the exception and stack trace.
    Include the code if you have made any changes.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Upload excel data to access database
    By ravikumar.achi in forum New To Java
    Replies: 20
    Last Post: 01-19-2012, 12:30 PM
  2. Replies: 3
    Last Post: 01-24-2011, 07:37 AM
  3. Disable Excel Sheet's Macros through Java Servlet
    By anmat24 in forum Java Servlet
    Replies: 0
    Last Post: 12-13-2010, 12:18 PM
  4. Hi i need a code for excel sheet upload to database
    By lavanya82 in forum JavaServer Pages (JSP) and JSTL
    Replies: 15
    Last Post: 07-28-2009, 05:15 PM
  5. Save data from Excel sheet to servlet
    By chandu.v in forum Java Servlet
    Replies: 2
    Last Post: 04-10-2008, 02:51 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •