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

Hibernate and Spring

The document describes an example Hibernate application that allows inserting and listing product records from a database table. It includes an HTML form, Hibernate configuration files, a Product POJO class, and two servlets - one for inserting records and one for listing all records. The insertion servlet takes product details from the form, saves a new Product object to the database, and commits the transaction. The listing servlet queries the database for all Product objects, iterates through them, and outputs the details to an HTML table.

Uploaded by

Ina Shivdasani
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
106 views

Hibernate and Spring

The document describes an example Hibernate application that allows inserting and listing product records from a database table. It includes an HTML form, Hibernate configuration files, a Product POJO class, and two servlets - one for inserting records and one for listing all records. The insertion servlet takes product details from the form, saves a new Product object to the database, and commits the transaction. The listing servlet queries the database for all Product objects, iterates through them, and outputs the details to an HTML table.

Uploaded by

Ina Shivdasani
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 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);} } }

You might also like