You are on page 1of 5

Hibernate Application 1 1.client.html <html> <head> <script> function subm() { f1.action="http://localhost:8080/prodhiber/p1"; f1.submit(); } function listme() { f1.action="http://localhost:8080/prodhiber/p2"; f1.

submit(); } </script> </head> <body> <form name=f1> <h2>Enter Product Details here</h2> <br> <hr> <br> <table> <tr><td>Pno<td><input type=text name="pno"></tr> <tr><td>Product Name<td><input type=text name="pname"> <tr><td><input type=button value="List Products" onclick="listme()"><td><input type=button value="Insert" onclick="subm()"></tr> </table> </form> </body> </html> 2. hibernate.properties hibernate.dialect net.sf.hibernate.dialect.SQLServerDialect hibernate.connection.driver_class sun.jdbc.odbc.JdbcOdbcDriver hibernate.connection.url jdbc:odbc:Mydsn hibernate.connection.username sa hibernate.connection.password hibernate.show_sql true

3. Product.hbm.xml
<?xml version="1.0" encoding="utf-8" ?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd"> <hibernate-mapping> <class name="Product" table="product"> <id name="pno" type="string"

>

<column name="pno" sql-type="text" not-null="true"/> <generator class="assigned"/> </id> <property name="pname"/> </class> </hibernate-mapping>

4. Product pojo import java.io.*; import java.util.*; public class Product { String pno; String pname; public Product() { } public Product(String pno,String pname) { this.pname = pname; this.pno = pno; } public void setPno(String id) { pno = id; } public String getPno(){ return pno; } public void setPname(String pn) {

pname = pn; } public String getPname() { return pname; } } 5. servlet1 import javax.servlet.*; import java.io.*; import net.sf.hibernate.*; import net.sf.hibernate.cfg.*; import java.util.*; public class prodlist extends GenericServlet { static SessionFactory sessionFactory; public void service(ServletRequest req,ServletResponse res) throws ServletException,IOException { PrintWriter out=res.getWriter(); try { Configuration cfg = new Configuration(); cfg.addClass(Product.class); sessionFactory = cfg.buildSessionFactory(); } catch (Exception e) { out.println(e); } try { Session session = sessionFactory.openSession(); out.println("<html>"); out.println("<table border='1'>"); out.println("<tr><td>Pno</td><td>Product Name</td></tr>"); List cds = session.find("from Product"); Iterator iter = cds.iterator(); while (iter.hasNext()) { Product cd = (Product)iter.next(); out.println("<tr><td>"); out.println(cd.getPno());

out.println("</td><td>"); out.println(cd.getPname()); out.println("</td><td>"); out.println("</td></tr>"); session.flush(); session.close(); } } catch (Exception e) {out.println(e);}

} } 6. servlet 2 import javax.servlet.*; import java.io.*; import net.sf.hibernate.*; import net.sf.hibernate.cfg.*; import java.util.*; public class prodserv extends GenericServlet { static SessionFactory sessionFactory; public void service(ServletRequest req,ServletResponse res) throws ServletException,IOException { PrintWriter out=res.getWriter(); String no=req.getParameter("pno"); String pn=req.getParameter("pname"); try { Configuration cfg = new Configuration(); cfg.addClass(Product.class); sessionFactory = cfg.buildSessionFactory(); } catch (Exception e) { out.println(e); } try { Product pd = new Product(no,pn); Session session = sessionFactory.openSession();

Transaction t = session.beginTransaction(); session.save(pd); // t.execute(); t.commit(); session.flush(); out.println("<h2>Record inserted..."); session.close(); } catch (Exception e) {out.println(e);} } }