Index.jsp:
we
can get the records Using
Jsp technology and Hibernate Daba
base.Here ,weare developing the index.jsp file .It is forwarded the request
to another jsp program.
<a href="success.jsp">Search</a>
Success.jsp:
This page is display the
results as per the pagination concept.
<%@page
import="org.hibernate.criterion.Projections"%>
<%@page import="java.util.Iterator"%>
<%@page import="java.util.List"%>
<%@page import="org.hibernate.Criteria"%>
<%@page import="a.b.Product"%>
<%@page import="org.hibernate.Session"%>
<%@page
import="org.hibernate.cfg.Configuration"%>
<%@page import="org.hibernate.SessionFactory"%>
<%
SessionFactory factory =
new Configuration().configure().buildSessionFactory();
int pageIndex = 0;
int totalNumberOfRecords = 0;
int numberOfRecordsPerPage = 4;
String sPageIndex = request.getParameter("pageIndex");
if(sPageIndex ==null)
{
pageIndex = 1;
}else
{
pageIndex = Integer.parseInt(sPageIndex);
}
Session ses = factory.openSession();
int s = (pageIndex*numberOfRecordsPerPage) -
numberOfRecordsPerPage;
Criteria crit = ses.createCriteria(Product.class);
crit.setFirstResult(s);
crit.setMaxResults(numberOfRecordsPerPage);
List l = crit.list();
Iterator it = l.iterator();
out.println("<table border=1>");
out.println("<tr>");
out.println("<th>PID</th><th>PNAME</th><th>PRICE</th>");
out.println("</tr>");
while(it.hasNext())
{
Product p = (Product)it.next();
out.println("<tr>");
out.println("<td>"+p.getPid()+"</td>");
out.println("<td>"+p.getPname()+"</td>");
out.println("<td>"+p.getPrice()+"</td>");
out.println("</tr>");
}
out.println("<table>");
Criteria crit1 = ses.createCriteria(Product.class);
crit1.setProjection(Projections.rowCount());
List l1=crit1.list();
Iterator it1 = l1.iterator();
if(it1.hasNext())
{
Object o=it1.next();
totalNumberOfRecords = Integer.parseInt(o.toString());
}
int noOfPages = totalNumberOfRecords/numberOfRecordsPerPage;
if(totalNumberOfRecords > (noOfPages *
numberOfRecordsPerPage))
{
noOfPages = noOfPages + 1;
}
for(int i=1;i<=noOfPages;i++)
{
String myurl = "success.jsp?pageIndex="+i;
out.println("<a
href="+myurl+">"+i+"</a>");
}
%>
hibernate.cfg.xml
This Configuration
file provides the information about Database interaction code.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property
name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property
name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property
name="hibernate.connection.url">jdbc:mysql://localhost:3306/ashok</property>
<property
name="hibernate.connection.username">root</property>
<mapping
resource="a/b/Product.hbm.xml"/>
</session-factory>
</hibernate-configuration>
hibernate.reveng.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-reverse-engineering PUBLIC
"-//Hibernate/Hibernate Reverse Engineering DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-reverse-engineering-3.0.dtd">
<hibernate-reverse-engineering>
<schema-selection
match-catalog="ashok"/>
<table-filter
match-name="product"/>
</hibernate-reverse-engineering>
Product.java
Here we are
providing setter getter methods for that properties.
package a.b;
// Generated Oct 12, 2013 9:59:39 AM by Hibernate Tools 3.2.1.GA
/**
* Product generated by
hbm2java
*/
public class Product
implements java.io.Serializable {
private int pid;
private String pname;
private Integer
price;
public Product() {
}
public Product(int
pid) {
this.pid = pid;
}
public Product(int
pid, String pname, Integer price) {
this.pid = pid;
this.pname = pname;
this.price = price;
}
public int getPid() {
return this.pid;
}
public void setPid(int
pid) {
this.pid = pid;
}
public String
getPname() {
return this.pname;
}
public void
setPname(String pname) {
this.pname =
pname;
}
public Integer
getPrice() {
return this.price;
}
public void
setPrice(Integer price) {
this.price =
price;
}
}
Product.hbm.xml
This Hibernate mapping file is map the Pojo class properties and
database table columns and Pojo class was mapped with database table name.
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Oct 12, 2013 9:59:44 AM by Hibernate Tools
3.2.1.GA -->
<hibernate-mapping>
<class
name="a.b.Product" table="product"
catalog="ashok">
<id
name="pid" type="int">
<column
name="pid" />
<generator
class="assigned" />
</id>
<property
name="pname" type="string">
<column
name="pname" length="23" />
</property>
<property
name="price" type="java.lang.Integer">
<column
name="price" />
</property>
</class>
</hibernate-mapping>
OutPut:
If you want o click a '2' we can get 15-20 records(based on pid)
Thanks, this is generally helpful.
ReplyDeleteStill, I followed step-by-step your method in this Core Java online course
learn Java online