Save Image Path And Data in Database through Hibernate.

index.jsp


<%@page import="java.util.Iterator"%>
<%@page import="java.util.List"%>
<%@page import="org.hibernate.SessionFactory"%>
<%@page import="org.hibernate.cfg.Configuration"%>
<%@page import="EMPLOYEE.*"%>
<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
    <head>
        <title>Payroll management System</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
        <link rel="stylesheet" href="jquery-ui.css" />
  <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
  <script src="jquery-1.9.1.js"></script>
  <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
  <script src="jquery-ui.js"></script>
  <script>
   
      $(document).ready(function() {
          $( "#accordion" ).accordion({
active:true,
   
     collapsible:true,
});
         $( "#accordion1" ).accordion({
active:true,
   
     collapsible:true,
});
        $( "#start" ).datepicker({ dateFormat: 'yy-mm-dd' });
        $( "#join" ).datepicker({ dateFormat: 'yy-mm-dd' });
        $( "#com" ).datepicker({ dateFormat: 'yy-mm-dd' });
      });
      </script>
    </head>
    <body>
        <div id="accordion">
    <center><h4>Employee Registration</h4></center>
    <center>
        <form action="save.insert" method="POST" enctype="multipart/form-data">
        <table>
            <tr>
            <td>NAME</td>
            <td><input type="text" name="name"></td>
         
         
            <td>LAST-NAME</td>
            <td><input type="text" name="surname"></td>
            </tr>
            <tr>
            <td>FATHER-NAME</td>
            <td><input type="text" name="fname"></td>
         
         
            <td>MOTHER-NAME</td>
            <td><input type="text" name="mname"></td>
            </tr>
            <tr>
            <td>BIRTH-DATE</td>
            <td><input type="text" name="birthdate" id="start"></td>
         
         
            <td>SEX</td>
                    <td><select name="sex">
                            <option name="sex">
                                MALE
                            </option>
                            <option name="sex">
                                FEMALE
                            </option>
                </select></td>
            </tr>
            <tr>
            <td>MARTIAL-STATUS</td>
            <td><input type="text" name="marst"></td>
         
         
            <td>NATIONALITY</td>
            <td><input type="text" name="nation"></td>
            </tr>
            <tr>
            <td>ADDRESS</td>
            <td><input type="text" name="address"></td>
         
         
            <td>MOBILE</td>
            <td><input type="text" name="mobile"></td>
            </tr>
            <tr>
            <td>MAIL-ID</td>
            <td><input type="text" name="mail"></td>
         
         
            <td>CONTRACT-TYPE</td>
             <td><select name="ct">
                            <option name="ct">
                                FULL-TIME
                            </option>
                            <option name="ct">
                                TENURE
                            </option>
                </select></td>
            </tr>
            <tr>
            <td>JOINING-DATE</td>
            <td><input type="text" name="join" id="join"></td>
            <td>COMPLETION-DATE</td>
            <td><input type="text" name="com" id="com"></td>
            </tr>
            <tr>
                <td>PASSPORT SIZE-PHOTO</td>
            <td><input type="file" name="file1"></td>
            <td><input type="submit" value="SAVE"></td>
            <td><input type="reset" value="RESET"></td>
            </tr>
        </table>
    </form>
    </center>
        </div>
        <div id="accordion1">
            <center><h4>Employees Lists</h4></center>
        <%
      EmployeeDao ee=new EmployeeDao();
      List list=ee.View();
      Iterator<Employee> itr=list.iterator();
      %>
            <center>
            <table style="width:100px;" border="2">
                <th>EMP_NAME</th>
                <th>EMP_NATION</th><th>EMP_ADDRESS</th><th>EMP_MOBILE</th><th>EMP_MAILID</th>
                <th>EMP-CONTRACT-TYPE</th><th>EMP-JOIN-DATE</th><th>COMPLETION-DATE</th><th>PHOTO</th>
             
            <%
      while(itr.hasNext())
      {
          Employee e=itr.next();
       
          %>
           <tr>
          <td><%=e.getName()%></td>
          <td><%=e.getNation()%></td>
          <td><%=e.getAddress()%></td>
          <td><%=e.getMobile()%></td>
          <td><%=e.getMailid()%></td>
          <td><%=e.getContract()%></td>
          <td><%= e.getJoindate()%></td>
          <td><%=e.getComdate()%></td>
          <td><img src="<%= e.getPhoto()%>" width='100px' height='100px'></td>
          </tr>
          <%
      }
        %>
             
            </table>
            </center>
        </div>
    </body>
</html>




save.java



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package Controller;

import EMPLOYEE.Employee;
import EMPLOYEE.EmployeeDao;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import java.util.Hashtable;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javazoom.upload.MultipartFormDataRequest;
import javazoom.upload.UploadBean;
import javazoom.upload.UploadFile;

/**
 *
 * @author NAVJYOTI
 */
@WebServlet(name = "save", urlPatterns = {"/save.insert"})
public class save extends HttpServlet {
private String FullPath=null;
Hashtable ht=null;
UploadFile file=null;
String name1=null;
    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
       PrintWriter out = response.getWriter(); 
                   
           String appPath = request.getServletContext().getRealPath("");
           String savePath = appPath + File.separator + "upload\\";
//           out.print(savePath);
          try
          {
           MultipartFormDataRequest mfdr=new MultipartFormDataRequest(request);
           UploadBean upb=new UploadBean();
           upb.setFolderstore(savePath);
           upb.setOverwrite(false);
           ht=mfdr.getFiles();
           Enumeration files=ht.elements();
           file=(UploadFile)files.nextElement();
           String name=file.getFileName();
           out.print(name);
           FullPath=savePath.concat(name);
           upb.store(mfdr);
         out.print(FullPath);
              name1=mfdr.getParameter("name");
           String surname=mfdr.getParameter("surname");
           String fname=mfdr.getParameter("fname");
           String mname=mfdr.getParameter("mname");
           String birthdate=mfdr.getParameter("birthdate");
           String sex=mfdr.getParameter("sex");
           String marst=mfdr.getParameter("marst");
           String nation=mfdr.getParameter("nation");
           String address=mfdr.getParameter("address");
           String mobile=mfdr.getParameter("mobile");
           String mail=mfdr.getParameter("mail");
           String ct=mfdr.getParameter("ct");
           String join=mfdr.getParameter("join");
           String com=mfdr.getParameter("com");
           //String com=request.getParameter("com");
           Employee e=new Employee();
           System.out.println("Name1-"+name1);
           e.setName(name1);
           e.setLname(surname);
            System.out.println("surname-"+surname);
           e.setFname(fname);
            System.out.println("fname-"+fname);
           e.setMname(mname);
           System.out.println("mname-"+mname);
           e.setBirthdate(birthdate);
           System.out.println("birthdate-"+birthdate);
           e.setSex(sex);
           System.out.println("sex-"+sex);
           e.setMaritialstatus(marst);
           System.out.println("marst-"+marst);
           e.setNation(nation);
           System.out.println("nation-"+nation);
           e.setAddress(address);
           e.setMobile(mobile);
           e.setMailid(mail);
           e.setContract(ct);
           e.setJoindate(join);
           e.setComdate(com);
           e.setPhoto(FullPath);
           EmployeeDao ee=new EmployeeDao();
           ee.add(e);

           
        }
          catch(Exception e)
          {
              System.out.println(e);
          }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}



EmployeeDao.java

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package EMPLOYEE;

import static EMPLOYEE.HibernateUtil.closeSession;
import static EMPLOYEE.HibernateUtil.getSession;
import static EMPLOYEE.HibernateUtil.getSessionFactory;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author NAVJYOTI
 */
public class EmployeeDao {
    
    
    public void add(Employee e) {
        Session session= getSession();
        Transaction t = session.beginTransaction();
       session.persist(e);
        t.commit();
        closeSession();
    }
    
    public List View() {
        Session session= getSession();
        Transaction t = session.beginTransaction();
      Query q= session.createQuery("from Employee");
      List<Employee> list= q.list();
        t.commit();
        closeSession();
        return list;
    }
}


Employee.java
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package EMPLOYEE;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

/**
 *
 * @author NAVJYOTI
 */

public class Employee {
   
    private int id;
   
    private String name;
    
    private String lname;
    
    private String fname;
    
    private String mname;
    
    private String birthdate;
    
    private String sex;
    
    private String maritialstatus;
    
    private String nation;
    
    private String address;
    
    private String mobile;
    
    private String mailid;
   
    private String contract;
    
    private String joindate;
    
    private String comdate;
    
    private String photo;

    public Employee() {
        
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getLname() {
        return lname;
    }

    public void setLname(String lname) {
        this.lname = lname;
    }

    public String getFname() {
        return fname;
    }

    public void setFname(String fname) {
        this.fname = fname;
    }

    public String getMname() {
        return mname;
    }

    public void setMname(String mname) {
        this.mname = mname;
    }

    public String getBirthdate() {
        return birthdate;
    }

    public void setBirthdate(String birthdate) {
        this.birthdate = birthdate;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getMaritialstatus() {
        return maritialstatus;
    }

    public void setMaritialstatus(String maritialstatus) {
        this.maritialstatus = maritialstatus;
    }

    public String getNation() {
        return nation;
    }

    public void setNation(String nation) {
        this.nation = nation;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getMobile() {
        return mobile;
    }

    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    public String getMailid() {
        return mailid;
    }

    public void setMailid(String mailid) {
        this.mailid = mailid;
    }

    public String getContract() {
        return contract;
    }

    public void setContract(String contract) {
        this.contract = contract;
    }

    public String getJoindate() {
        return joindate;
    }

    public void setJoindate(String joindate) {
        this.joindate = joindate;
    }

    public String getComdate() {
        return comdate;
    }

    public void setComdate(String comdate) {
        this.comdate = comdate;
    }

    public Employee(int id, String name, String lname, String fname, String mname, String birthdate, String sex, String maritialstatus, String nation, String address, String mobile, String mailid, String contract, String joindate, String comdate, String photo) {
        this.id = id;
        this.name = name;
        this.lname = lname;
        this.fname = fname;
        this.mname = mname;
        this.birthdate = birthdate;
        this.sex = sex;
        this.maritialstatus = maritialstatus;
        this.nation = nation;
        this.address = address;
        this.mobile = mobile;
        this.mailid = mailid;
        this.contract = contract;
        this.joindate = joindate;
        this.comdate = comdate;
        this.photo = photo;
    }

    public String getPhoto() {
        return photo;
    }

    public void setPhoto(String photo) {
        this.photo = photo;
    }
    
}





Popular posts from this blog

Simple Sign up design. Android.

Cart page design in android.

Set Date on jDateChooser and retrieve record from database.