0% found this document useful (0 votes)
34 views

Practical 9c

The document describes developing a Hibernate application to store and retrieve employee details from a MySQL database. It includes creating the database table, developing Java classes for the entity and configuration, and JSP pages for the user interface to insert and display records.

Uploaded by

Zoya
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views

Practical 9c

The document describes developing a Hibernate application to store and retrieve employee details from a MySQL database. It includes creating the database table, developing Java classes for the entity and configuration, and JSP pages for the user interface to insert and display records.

Uploaded by

Zoya
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

***9c.

Develop a Hibernate application to store and retrieve employee details in


MySQL Database.

create database empdb;

create table emptab(empid int PRIMARY KEY AUTO_INCREMENT,


empname varchar(20),
empdesg varchar(20),
empdept varchar(20),
empsal int);

**Now create Index.html file


<html>
<head>
<title>Employee Details data entry form
</title>
</head>
<body><h1>Employee Details data entry form</h1>
<form action="Insert.jsp"><table border="1" >
<tr><td>Enter Employee Name</td>
<td><input type="text" name="txtName"></td></tr>
<tr><td>Enter Employee Designation</td>
<td><input type="text" name="txtDes"></td></tr>
<tr><td>Enter Employee Department</td>
<td><input type="text" name="txtDept"></td></tr>
<tr><td>Enter Employee Salary</td>
<td><input type="text" name="txtSal"></td></tr>
<tr><td><input type="reset"></td>
<td><input type="submit" value="REGISTER"></td></tr>
<h1><a href="fetch.jsp" >Show All Records</a></h1>
</table>
</form>
</body>
</html>

***Add hibernate library file and mysql jar file.


-->Add java Bean class (EmpBean)
EmpBean.java
package mypack;

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

@Entity
@Table(name = "emptab")
public class EmpBean implements java.io.Serializable
{
@Id
@GeneratedValue
@Column(name ="empid")
private String no;
@Column(name = "empname")
private String name;
@Column(name = "empdesg")
private String desg;
@Column(name = "empdept")
private String dept;
@Column(name ="empsal")
private String sal;

public EmpBean()
{ }
public EmpBean(String en,String ed1,String ed2,String es)
{
this.name=en;
this.desg=ed1;
this.dept=ed2;
this.sal=es;
}
public String getNo()
{ return no; }
public String getName()
{ return name;}
public String getDesg()
{ return desg; }
public String getDept()
{ return dept; }
public String getSal()
{ return sal;}
public void setNo(String n)
{ no=n; }

public void setName(String nm)


{ name = nm; }

public void setDesg(String dg)


{ desg = dg; }

public void setDept(String dp)


{ dept = dp; }

public void setSal(String sl)


{ sal = sl; } }

--->Add hibernate configuration file


source package-->right click-->other--->hibernate(from categories)-->
hibernate configuration wizard(file types)
-->hibernate.cfg--->choose database connection with appropriate database name
--->finish
hibernate configuration file will be added in source package
--->add following mapping class within Session Factory
<mapping class="mypack.EmpBean"/>

-->now fetch.jsp file


***fetch.jsp(JSTL coding)

<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>


<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>
<%@page import =" javax.servlet.http.*,javax.servlet.*"
contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="java.io.*,java.util.*,java.sql.*" %>

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<sql:setDataSource var="db" driver="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost/cbs" user="root" password="gnvs"/>
<sql:query dataSource="${db}" var="rs">
select * from emptab;
</sql:query>
<table border="1" width="100%">
<tr>
<th>Employee Id
<th>Name
<th>Designation
<th>Department
<th>Salary
</tr>
<c:forEach var="table" items="${rs.rows}">
<tr>
<td><c:out value="${table.EmpId}"/>
<td><c:out value="${table.EmpName}"/>
<td><c:out value="${table.EmpDesg}"/>
<td><c:out value="${table.EmpDept}"/>
<td><c:out value="${table.EmpSal}"/>
</tr>
</c:forEach>
</table>

</body>
</html>

***Insert.jsp
<%@page import="mypack.EmpBean"%>
<%@page import="org.hibernate.Transaction"%>
<%@page import="org.hibernate.cfg.Configuration"%>
<%@page import="org.hibernate.SessionFactory"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<%!
SessionFactory sf;
org.hibernate.Session hibSession;
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%
sf=new Configuration().configure().buildSessionFactory();
hibSession=sf.openSession();
Transaction tx=null;
EmpBean eb=new EmpBean();
try
{
tx=hibSession.beginTransaction();
eb.setName(request.getParameter("txtName"));
eb.setDesg(request.getParameter("txtDes"));
eb.setDept(request.getParameter("txtDept"));
eb.setSal(request.getParameter("txtSal"));
hibSession.save(eb);
tx.commit();
out.println("<h1>record inserted successfully ");
out.println("<a href='fetch.jsp'>Click here</a>");
}
catch(Exception e)
{
out.println(e);
}
%>
</body>
</html>

You might also like