image & details saved in DB

Image stored data using jsp
----------------------------------
employee_upload_profile_image.html
----------





 

 

       
     
Employee Name:

 


 
 
     
Employee Address:

 



 
 
     
 
 
     
 
 
 
     
Contact Number:

 


 
       
     
Employee Email ID:

 


 

       
     
Employee Image

     
     
 
 


 
               
 



   





====================================
employee_upload_profile_image.jsp
-------------------
 
   <%@ page import="java.util.List" %>
   <%@ page import="java.util.Iterator" %>
   <%@ page import="java.io.*" %>
   <%@ page import="org.apache.commons.fileupload.servlet.ServletFileUpload"%>
   <%@ page import="org.apache.commons.fileupload.disk.DiskFileItemFactory"%>
   <%@ page import="org.apache.commons.fileupload.*"%>
    <%@ page import="java.nio.file.Files"%>
 <%@ page import="java.nio.file.Path"%>
 <%@ page import="java.nio.file.Paths"%>
 <%@ page import="java.sql.*"%>

   <%@ page contentType="text/html;charset=UTF-8" language="java" %>
 

       

Your  Profile has been Uploaded


   <%!
     String emp_name="";
String emp_c_number="";
String emp_emailid="";
     String address1="";
String address2="";
int count1=0,count2=0,count3=0,count4=0,count5=0;
 %>
 <%
 boolean isMultipart = ServletFileUpload.isMultipartContent(request);
 if (!isMultipart) {
 } else {
   FileItemFactory factory = new DiskFileItemFactory();
   ServletFileUpload upload = new ServletFileUpload(factory);
   List items = null;
   try {
   items = upload.parseRequest(request);
   } catch (FileUploadException e) {
   e.printStackTrace();
   }
   Iterator itr = items.iterator();
   while (itr.hasNext())
  {
   FileItem item = (FileItem) itr.next();
   if (item.isFormField())
  {
     String name = item.getFieldName();
 String value = item.getString();
 if(name.equals("emp_name"))
          {
  emp_name=value;
            count1=1;
  }
 if(name.equals("address1"))
                 {
        address1=value;            
                         count2=2;
 }
 if(name.equals("address2"))
                 {
        address2=value;            
                         count5=5;
 }
 if(name.equals("contact_number"))
                 {
        emp_c_number=value;
        count3=3;
 }
           
 if(name.equals("email_id"))
            {
     count4=4;
 emp_emailid=value;
}  


 
   } else
  {
    try {

   String itemName = item.getName();
   File savedFile = new File(config.getServletContext().getRealPath("/")+"image\\"+itemName);
   item.write(savedFile);
 String extension = "";
 String fname="";
int i = itemName.lastIndexOf('.');
if (i >= 0) {
    extension = itemName.substring(i+1);
fname = itemName.substring(0, i);
}
Path path = Paths.get(itemName);

String contentType = Files.probeContentType(path);
Connection connection = null;
        String connectionURL = "jdbc:mysql://localhost:3306/test";
Class.forName("com.mysql.jdbc.Driver").newInstance();
connection = DriverManager.getConnection(connectionURL, "root", "root");
 String sql = "insert into employee(name,email,file_name,file_type,file_extn) values('"+emp_name+"','"+emp_emailid+"', '"+fname+"','"+contentType+"','"+extension+"') ";
  Statement st = connection.createStatement();
     
int s = st.executeUpdate(sql);
         if(s>0){
         out.println("Uploaded successfully !");
       }
      else{
     out.println("Error!");
       }

     %>

width="137"  height="140">
 
   <% if(count1==1)
      out.println("
Name:
"+emp_name);

 if(count2==2)
    out.println("

Addresss:
"+address1);
 if(count5==5)
    out.println("


"+address2);
 if(count3==3)
    out.println("

Contact No
"+emp_c_number);
 if(count4==4)
    out.println("

Email ID
"+emp_emailid);


   } catch (Exception e) {
   e.printStackTrace();
   }
   }
   }
   }
   %>
   

   
=============================
CREATE TABLE `employee` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(45) DEFAULT NULL,
  `email` varchar(45) DEFAULT NULL,
  `file_name` varchar(45) DEFAULT NULL,
  `file_type` varchar(45) DEFAULT NULL,
  `file_extn` varchar(45) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2

Comments