Annotation Based Web Application in Hibernate

Program 2

package beans;
import javax.persistence.*;

@Entity
@Table(name = "emp_info",schema = "annotweb")
public class Employee 
{
        @Id
        @GeneratedValue
        private int eid;
        @Column(name = "empname" ,length = 50,nullable = false)
        private String ename;
        @Column(name = "empsalary" ,nullable = false)
        private int esalary;
        @Column(name = "empdept" ,length = 50,nullable = false)
        private String edept;

    public Employee() {
    }

    public int getEid() {
        return eid;
    }

    public void setEid(int eid) {
        this.eid = eid;
    }

    public String getEname() {
        return ename;
    }

    public void setEname(String ename) {
        this.ename = ename;
    }

    public int getEsalary() {
        return esalary;
    }

    public void setEsalary(int esalary) {
        this.esalary = esalary;
    }

    public String getEdept() {
        return edept;
    }

    public void setEdept(String edept) {
        this.edept = edept;
    }
        
        
}

Program 3

<!DOCTYPE hibernate-configuration PUBLIC
  "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
  "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
   <session-factory>
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>  
        <property name="connection.url">jdbc:mysql://localhost/annotweb</property>
        <property name="connection.username">root</property>
        <property name="connection.password">root@data</property>
        <property name="connection.pool_size">10</property>
         <property name="hbm2ddl.auto">update</property>
        <property name="show_sql">true</property>
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
        <mapping class="beans.Employee"/>
    </session-factory>
</hibernate-configuration>

Program 4

<%-- 
    Document   : InsertData
    Created on : Dec 16, 2023, 5:08:16 PM
    Author     : admin
--%>

<%@page import="beans.Employee"%>
<%@page import="org.hibernate.Session"%>
<%@page import="org.hibernate.SessionFactory"%>
<%@page import="org.hibernate.cfg.Configuration"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
       <% 
           int esal;
           String ename,edept;
          
           ename=request.getParameter("txtname");
           esal=Integer.parseInt(request.getParameter("txtsalary"));
           edept=request.getParameter("txtdept");
           
           Configuration cfg=new Configuration();
           cfg.configure("resources/hibernate.cfg.xml");
           SessionFactory sf=cfg.buildSessionFactory();
           Session session1=sf.openSession();
           Employee E=new Employee();
          
           E.setEname(ename);
           E.setEsalary(esal);
           E.setEdept(edept);
           session1.save(E);
           session1.beginTransaction().commit();
           session1.close();
           sf.close();
           out.println("<h1>Record Save with Annotation Application</h1>");
           
       %>
    </body>
</html>

 

courses

TechVidvan Team

TechVidvan Team provides high-quality content & courses on AI, ML, Data Science, Data Engineering, Data Analytics, programming, Python, DSA, Android, Flutter, full stack web dev, MERN, and many latest technology.

Leave a Reply

Your email address will not be published. Required fields are marked *