Customer.java

package bean;

import java.sql.*;

import java.util.*;

import org.hibernate.cfg.*;

import org.hibernate.*;

publicclass Customer {

private String customerID;

private String name;

private String address;

private String phoneNo;

private String mailId;

private String searchName;

public String getSearchName() {

returnsearchName;

}

publicvoid setSearchName(String searchName) {

this.searchName = searchName;

}

public String getCustomerID() {

returncustomerID;

}

publicvoid setCustomerID(String customerID) {

this.customerID = customerID;

}

public String getName() {

returnname;

}

publicvoid setName(String name) {

this.name = name;

}

public String getAddress() {

returnaddress;

}

publicvoid setAddress(String address) {

this.address = address;

}

public String getPhoneNo() {

returnphoneNo;

}

publicvoid setPhoneNo(String phoneNo) {

this.phoneNo = phoneNo;

}

public String getMailId() {

returnmailId;

}

publicvoid setMailId(String mailId) {

this.mailId = mailId;

}

public String addAction() throws SQLException {

try

{

SessionFactory sf = new Configuration().configure().buildSessionFactory();

Session s = sf.openSession();

Customer sub = new Customer();

String dbcustid=this.customerID;

String dbcustname=this.name;

String dbcustadd=this.address;

String dbphoneNo=this.phoneNo;

String dbcustmailid=this.mailId;

sub.setCustomerID(dbcustid);

sub.setName(dbcustname);

sub.setAddress(dbcustadd);

sub.setPhoneNo(dbphoneNo);

sub.setMailId(dbcustmailid);

s.beginTransaction();

s.save(sub);

System.out.println("inserted in SearchCustomer Table(Hibernate)--");

s.getTransaction().commit();

s.close();

sf.close();

}catch(Exception ex){

System.out.println("Exception in addAction:-"+ex.getMessage());

}

return"insert";

}

public List<Customer> getAllCustomer()

{

List<Customer> custInfoAll = new ArrayList<Customer>();

try {

SessionFactory sf = new Configuration().configure().buildSessionFactory();

Session s = sf.openSession();

s.beginTransaction();

custInfoAll = s.createQuery("from Customer order by name desc").list();

s.getTransaction().commit();

System.out.println("Display values");

s.close();

sf.close();

} catch (Exception e) {

System.out.println("Exception in getAllCustomer::"+e.getMessage());

}

return custInfoAll;

}

public List<Customer> getCustomerDetail()

{

List<Customer> custInfo = new ArrayList<Customer>();

try {

SessionFactory sf = new Configuration().configure().buildSessionFactory();

Session s = sf.openSession();

s.beginTransaction();

String hql = "FROM Customer WHERE name ='"+searchName+"'";

Query query = s.createQuery(hql);

custInfo= query.list();

s.getTransaction().commit();

System.out.println("display search data with arg");

s.close();

sf.close();

} catch (Exception e) {

System.out.println("Exception in getCustomerDetail::"+e.getMessage());

}

return custInfo;

}

public String ResultCustomer()

{

getCustomerDetail();

return"result";

}

public String addCustomer() throws SQLException {

try

{

System.out.println("in add customer");

}catch(Exception ex){

System.out.println("Exception in addCustomer:-"+ex.getMessage());

}

return"add";

}

}

index.jsp

<!DOCTYPEHTMLPUBLIC"-//W3C//DTD HTML 4.01 Transitional//EN"

"

html

head

</head

body

<% response.sendRedirect("./faces/search.jsp"); %>

</body

</html

insert.jsp

<%@pagelanguage="java"contentType="text/html; charset=ISO-8859-1"pageEncoding="ISO-8859-1"%>

<%@taglibprefix="f" uri="%>

<%@taglibprefix="h" uri="%>

<!DOCTYPEhtmlPUBLIC"-//W3C//DTD HTML 4.01 Transitional//EN""

html

head

metahttp-equiv="Content-Type"content="text/html; charset=ISO-8859-1"

titleInsert</title

linkrel="stylesheet"type="text/css"href="css/table-style.css"/>

</head

body

f:view

h:formid="insert"

tableclass="tbl"cellpadding="0"cellspacing="0"border="0"

trtdb

h:outputLabelvalue="Customer-ID::"/</b

</tdtd

h:messageid="customerIDMessage"for="CustomerId"/>

h:inputTextid="CustomerId"value="#{customer.customerID}"required="true"/</td</tr

trtdb

h:outputLabelvalue="Customer-Name::"/</b</tdtd

h:messageid="CustomerNameMessage"for="CustomerName"/>

h:inputTextid="CustomerName"value="#{customer.name}"required="true"/>

</td</tr

trtdb

h:outputLabelvalue="Customer-Address::"/</b</tdtd

h:messageid="CustomerAddressMessage"for="CustomerAddress"/>

h:inputTextid="CustomerAddress"value="#{customer.address}"required="true"/>

</td</tr

trtdb

h:outputLabelvalue="Customer-PhoneNo::"/</b</tdtd

h:messageid="CustomerPhoneNoMessage"for="CustomerPhoneNo"/>

h:inputTextid="CustomerPhoneNo"value="#{customer.phoneNo}"maxlength="10"required="true"/>

</td</tr

trtdb

h:outputLabelvalue="Customer-MailId::"/</b</tdtd

h:messageid="CustomerMailIdMessage"for="CustomerMailId"/>

h:inputTextid="CustomerMailId"value="#{customer.mailId}"required="true"/>

</td</tr

</table

h:commandButtonaction="#{customer.addAction}"value="Add Customer"/>

h:commandButtonvalue="reset"type="reset"/>

h:outputLinkvalue="search.jsp"

Back To Search Page

</h:outputLink

</h:form

</f:view

</body

</html

result.jsp

<%@pagelanguage="java"contentType="text/html; charset=ISO-8859-1"pageEncoding="ISO-8859-1"%>

<%@taglibprefix="f" uri="%>

<%@taglibprefix="h" uri="%>

<!DOCTYPEhtmlPUBLIC"-//W3C//DTD HTML 4.01 Transitional//EN""

html

head

metahttp-equiv="Content-Type"content="text/html; charset=ISO-8859-1"

titleResult</title

linkrel="stylesheet"type="text/css"href="css/table-style.css"/>

</head

body

f:view

h:formid="searchCustomer"

h:dataTablevalue="#{customer.customerDetail}"var="srch"border="1"

styleClass="order-table"

headerClass="order-table-header"

rowClasses="order-table-odd-row,order-table-even-row"

h:column

f:facetname="header"

h:outputTextvalue="Customer ID"/>

</f:facet

h:outputTextvalue="#{srch.customerID}"

</h:outputText

</h:column

h:column

f:facetname="header"

h:outputTextvalue="Customer Name"/>

</f:facet

h:outputTextvalue="#{srch.name}"

</h:outputText

</h:column

h:column

f:facetname="header"

h:outputTextvalue="Customer Address"/>

</f:facet

h:outputTextvalue="#{srch.address}"

</h:outputText

</h:column

h:column

f:facetname="header"

h:outputTextvalue="Customer Phone No"/>

</f:facet

h:outputTextvalue="#{srch.phoneNo}"

</h:outputText

</h:column

h:column

f:facetname="header"

h:outputTextvalue="Customer Mail-Id"/>

</f:facet

h:outputTextvalue="#{srch.mailId}"

</h:outputText

</h:column

</h:dataTable

h:outputLinkvalue="search.jsp"

Search Again

</h:outputLink

</h:form

</f:view

</body

</html

search.jsp

<%@pagelanguage="java"contentType="text/html; charset=ISO-8859-1"pageEncoding="ISO-8859-1"%>

<%@taglibprefix="f" uri="%>

<%@taglibprefix="h" uri="%>

<!DOCTYPEhtmlPUBLIC"-//W3C//DTD HTML 4.01 Transitional//EN""

html

head

metahttp-equiv="Content-Type"content="text/html; charset=ISO-8859-1"

titleSearch</title

linkrel="stylesheet"type="text/css"href="css/table-style.css"/>

</head

body

f:view

h:formid="Search"

h:commandButtonvalue="Add Customer"action="#{customer.addCustomer}"immediate="true"/>

table

trtdb

h:outputLabelvalue="CustomerName::-"/</b

h:messageid="CustomerNameMessage"for="CustomerName"/>

h:inputTextid="CustomerName"value="#{customer.searchName}"required="true"/>

</td</tr

</table

h:commandButtonvalue="Search"action="#{customer.ResultCustomer}"/>

h:commandButtonvalue="reset"type="reset"/>

h:dataTablevalue="#{customer.allCustomer}"var="allresult"border="1"styleClass="order-table"

headerClass="order-table-header"

rowClasses="order-table-odd-row,order-table-even-row"

h:column

f:facetname="header"

h:outputTextvalue="Customer ID"/>

</f:facet

h:outputTextvalue="#{allresult.customerID}"

</h:outputText

</h:column

h:column

f:facetname="header"

h:outputTextvalue="Name"/>

</f:facet

h:outputTextvalue="#{allresult.name}"

</h:outputText

</h:column

h:column

f:facetname="header"

h:outputTextvalue="Address"/>

</f:facet

h:outputTextvalue="#{allresult.address}"

</h:outputText

</h:column

h:column

f:facetname="header"

h:outputTextvalue="Phone No"/>

</f:facet

h:outputTextvalue="#{allresult.phoneNo}"

</h:outputText

</h:column

h:column

f:facetname="header"

h:outputTextvalue="Mail ID"/>

</f:facet

h:outputTextvalue="#{allresult.mailId}"

</h:outputText

</h:column

</h:dataTable

</h:form

</f:view

</body

</html

hibernate.cfg.xml

<?xmlversion="1.0"encoding="UTF-8"?>

<!DOCTYPEhibernate-configurationPUBLIC"-//Hibernate/Hibernate Configuration DTD 3.0//EN"

"

hibernate-configuration

session-factoryname=""

propertyname="hibernate.connection.driver_class"com.mysql.jdbc.Driver</property

propertyname="hibernate.connection.password"root</property

propertyname="hibernate.connection.url"jdbc:mysql://localhost:3306/test</property

propertyname="hibernate.connection.username"root</property

propertyname="hibernate.dialect"org.hibernate.dialect.MySQLDialect</property

propertyname="connection.pool_size"10</property

propertyname="cache.provider_class"org.hibernate.cache.NoCacheProvider</property

propertyname="hbm2ddl.auto"update</property

mappingresource="bean/Customer.hbm.xml"/>

</session-factory

</hibernate-configuration

Customer.hbm.xml

<?xmlversion="1.0"?>

<!DOCTYPEhibernate-mappingPUBLIC"-//Hibernate/Hibernate Mapping DTD 3.0//EN"

"

<!-- Generated Apr 1, 2012 1:38:22 AM by Hibernate Tools 3.4.0.CR1 -->

hibernate-mapping

classname="bean.Customer"table="SearchCustomer"

idname="customerID"type="java.lang.String"

columnname="CUSTOMERID"/>

generatorclass="assigned"/>

</id

propertyname="name"type="java.lang.String"

columnname="NAME"/>

</property

propertyname="address"type="java.lang.String"

columnname="ADDRESS"/>

</property

propertyname="phoneNo"type="java.lang.String"

columnname="PHONENO"/>

</property

propertyname="mailId"type="java.lang.String"

columnname="MAILID"/>

</property

</class

</hibernate-mapping

faces-config.xml

<?xmlversion="1.0"encoding="UTF-8"?>

faces-configversion="2.0"xmlns="

xmlns:xi="

xmlns:xsi="

managed-bean

managed-bean-namecustomer</managed-bean-name

managed-bean-classbean.Customer</managed-bean-class

managed-bean-scoperequest</managed-bean-scope

</managed-bean

navigation-rule

from-view-id/search.jsp</from-view-id

navigation-case

from-action#{customer.addCustomer}</from-action

from-outcomeadd</from-outcome

to-view-id/insert.jsp</to-view-id

</navigation-case

navigation-case

from-outcomeresult</from-outcome

to-view-id/result.jsp</to-view-id

</navigation-case

</navigation-rule

navigation-rule

from-view-id/insert.jsp</from-view-id

navigation-case

from-action#{customer.addAction}</from-action

from-outcomeinsert</from-outcome

to-view-id/search.jsp</to-view-id

</navigation-case

</navigation-rule

</faces-config

web.xml

<?xmlversion="1.0"encoding="UTF-8"?>

web-appxmlns:xsi="

display-nameSearch-Reasult-Add</display-name

welcome-file-list

welcome-fileindex.html</welcome-file

welcome-fileindex.htm</welcome-file

welcome-fileindex.jsp</welcome-file

welcome-filedefault.html</welcome-file

welcome-filedefault.htm</welcome-file

welcome-filedefault.jsp</welcome-file

</welcome-file-list

servlet

servlet-nameFaces Servlet</servlet-name

servlet-classjavax.faces.webapp.FacesServlet</servlet-class

load-on-startup1</load-on-startup

</servlet

servlet-mapping

servlet-nameFaces Servlet</servlet-name

url-pattern/faces/*</url-pattern

</servlet-mapping

context-param

param-namejavax.servlet.jsp.jstl.fmt.localizationContext</param-name

param-valueresources.application</param-value

</context-param

context-param

descriptionState saving method: 'client' or 'server' (=default). See JSF Specification 2.5.2</description

param-namejavax.faces.STATE_SAVING_METHOD</param-name

param-valueclient</param-value

</context-param

context-param

description

This parameter tells MyFaces if javascript code should be allowed in

the rendered HTML output.

If javascript is allowed, command_link anchors will have javascript code

that submits the corresponding form.

If javascript is not allowed, the state saving info and nested parameters

will be added as url parameters.

Default is 'true'</description

param-nameorg.apache.myfaces.ALLOW_JAVASCRIPT</param-name

param-valuetrue</param-value

</context-param

context-param

description

If true, rendered HTML code will be formatted, so that it is 'human-readable'

i.e. additional line separators and whitespace will be written, that do not

influence the HTML code.

Default is 'true'</description

param-nameorg.apache.myfaces.PRETTY_HTML</param-name

param-valuetrue</param-value

</context-param

context-param

param-nameorg.apache.myfaces.DETECT_JAVASCRIPT</param-name

param-valuefalse</param-value

</context-param

context-param

description

If true, a javascript function will be rendered that is able to restore the

former vertical scroll on every request. Convenient feature if you have pages

with long lists and you do not want the browser page to always jump to the top

if you trigger a link or button action that stays on the same page.

Default is 'false'

</description

param-nameorg.apache.myfaces.AUTO_SCROLL</param-name

param-valuetrue</param-value

</context-param

listener

listener-classorg.apache.myfaces.webapp.StartupServletContextListener</listener-class

</listener

</web-app

table-style.css

.order-table{

border-collapse:collapse;

}

.order-table-header{

text-align:center;

background:nonerepeatscroll00#E5E5E5;

border-bottom:1pxsolid#BBBBBB;

padding:10px;

}

.order-table-odd-row{

text-align:center;

background:nonerepeatscroll00#F9F9F9;

border-top:4pxsolid#BBBBBB;

}

.order-table-even-row{

text-align:center;

background:nonerepeatscroll00#F9F9F9;

border-top:4pxsolid#BBBBBB;

}

body{

background-color:#faebea;

}

table.tbl{

width:100%;

border-collapse:collapse;

border-bottom:1pxsolid#000;

}

table.tblth,

table.tbltd{

padding:2px;

text-align:left;

border:1pxsolid#BBBBBB;;

border-bottom:0;

}