Practical 9c
Practical 9c
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; }
<!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>