JaiswalTraining

Get the online training



Corejava Servlet Jsp  Php  Hibernate  Ajax Web Service   Ejb2.1 Ejb3.0 Struts Struts2  JPA Spring Ibatis   JSF    JSF2.0  CoreJavaDesignPattern    Jquery  Flex J2EE-Design-Patterns  Jboss7  Maven  Contact Me                                                                                                                                                                        
            IGNOU SMU PTU Project                                           Training                                                                                                                              
              

Contact Us  0091- 9210721663         jaiswaltraining@gmail.com




Corejava
Servlet
Jsp
Php
Hibernate
Ajax
Web Service
Ejb2.1
Ejb3.0
Struts  
Struts2
JPA
Spring
Ibatis
JSF
JSF2.0
CoreJavaDesignPattern
Jquery
Flex
J2EE-Design-Patterns
Jboss7
Maven









JSF database Connectivity on Tomcat
Create a table in Oracle with the following code

SQL> create table project(id int primary key,name varchar(12),cost int, duration int);

  • First create HibernateUtil.java  file in src.
  • First Create a Dynamic project with the given name JsfData
  • Add the Tomcat Server7 as Server
  • Add the project facet for jsf2 as used in previous page   link.
  • Add the required jsf library  in the lib folder.
  • Also add the ojdbc5.jar for oracle connectivity.

  • This is the Project Structure.



  • Now create a xhtml file in the WebContent
  • Save it as ProjectForm.xhtml

ProjectForm.xhtml

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:ui="http://java.sun.com/jsf/facelets"
  xmlns:h="http://java.sun.com/jsf/html"
  xmlns:f="http://java.sun.com/jsf/core">
<h:body>
  <h:form>
    <h:panelGrid columns="3" bgcolor="aqua">
      <h:outputText value="Enter project id " />
      <h:inputText value="#{prj.id}" required="true"
        requiredMessage="Id is blank" id="pid" />
      <h:message for="pid" />

      <h:outputText value="Enter project name " />
      <h:inputText value="#{prj.name}" />
      <h:message for="pid" />

      <h:outputText value="Enter project cost " />
      <h:inputText value="#{prj.cost}" />
      <h:message for="pid" />

      <h:outputText value=" Enter
      project duration " />
      <h:inputText value="#{prj.duration}" />
      <h:message for="pid" />

      <h:commandButton value="Add Project Record"
        action="#{prjimpl.insert}" />
      <h:commandButton value="Update Project" action="#{prjimpl.update}" />
      <h:commandButton value="ShowAll Record" action="ProjectShowAll" />
      <h:commandButton value="Search  Project" action="#{prjimpl.search}" />
      <h:commandButton value="Remove  Project" action="#{prjimpl.delete}" />

    </h:panelGrid>
  </h:form>
</h:body>
</html>


  • Now create Project.java  file in src.
  • Add the package name as model.
  • Add the following code as shown below.
Project.java

package model.com;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

@ManagedBean(name = "prj")
@SessionScoped
public class Project {
  private int id;
  private String name;
  private float cost;
  private int duration;

  public int getId() {
    return id;
  }

  public void setId(int id) {
    this.id = id;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public float getCost() {
    return cost;
  }

  public void setCost(float cost) {
    this.cost = cost;
  }

  public int getDuration() {
    return duration;
  }

  public void setDuration(int duration) {
    this.duration = duration;
  }

}

  • Now create ProjectImpl.java  file in src.
  • Add the package name as model.
  • Add the following code as shown below
ProjectImpl.java

ProjectImpl.java
package model.com;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

@ManagedBean(name = "prjimpl")
public class ProjectImpl {
  Connection con;
  ResultSet rs;
  PreparedStatement pst;

  HttpSession session;
  FacesContext ctx;
  Project project;

  public ProjectImpl() throws ClassNotFoundException, SQLException {
    Class.forName("oracle.jdbc.driver.OracleDriver");
    con = DriverManager.getConnection(
        "jdbc:oracle:thin:@127.0.0.1:1521:XE""system""manager");
  }

  public List<Project> getProject1() throws SQLException {
    pst = con.prepareStatement("select * from project");
    rs = pst.executeQuery();
    List<Project> prjList = new ArrayList<Project>();
    Project pr;
    while (rs.next()) {
      pr = new Project();
      pr.setId(rs.getInt(1));
      pr.setName(rs.getString(2));
      pr.setCost(rs.getFloat(3));
      pr.setDuration(rs.getInt(4));
      prjList.add(pr);
    }
    return prjList;
  }

  public String delete() throws SQLException {
    ctx = FacesContext.getCurrentInstance();
    session = (HttpSessionctx.getExternalContext().getSession(false);

    project = (Projectsession.getAttribute("prj");
    pst = con.prepareStatement("select * from project where id=?");
    pst.setInt(1, project.getId());
    rs = pst.executeQuery();

    if (rs.next()) {
      pst = con.prepareStatement("delete from   project where id=?");
      pst.setInt(1, project.getId());

      pst.executeUpdate();
      session.setAttribute("msg""Record deleted successfully with id "
          + project.getId());
      return "ProjectOperation";

    else {
      session.setAttribute("msg""Project Id  not present");
      return "ProjectFail";
    }
  }

  public String insert() throws SQLException {
    ctx = FacesContext.getCurrentInstance();
    session = (HttpSessionctx.getExternalContext().getSession(false);

    project = (Projectsession.getAttribute("prj");
    pst = con.prepareStatement("select * from project where id=?");
    pst.setInt(1, project.getId());
    rs = pst.executeQuery();

    if (rs.next()) {
      session.setAttribute("msg""Project Id already Present");
      return "ProjectFail";
    else {
      pst = con.prepareStatement("insert into project values(?,?,?,?)");
      pst.setInt(1, project.getId());
      pst.setString(2, project.getName());
      pst.setFloat(3, project.getCost());
      pst.setInt(4, project.getDuration());
      pst.executeUpdate();
      session.setAttribute("msg""Record Added");
      return "ProjectOperation";
    }
  }

  public String search() throws SQLException {
    ctx = FacesContext.getCurrentInstance();
    session = (HttpSessionctx.getExternalContext().getSession(false);

    project = (Projectsession.getAttribute("prj");
    pst = con.prepareStatement("select * from project where id=?");
    pst.setInt(1, project.getId());
    rs = pst.executeQuery();

    if (rs.next()) {
      project.setId(rs.getInt(1));
      project.setName(rs.getString(2));
      project.setCost(rs.getFloat(3));
      project.setDuration(rs.getInt(1));
      
      session.setAttribute("prj1", project);
      return "ProjectSearch";
    else {
      session.setAttribute("msg""Project not found with id "+project.getId());
      return "ProjectFail";
    }
  }

  public String update() throws SQLException {
    ctx = FacesContext.getCurrentInstance();
    session = (HttpSessionctx.getExternalContext().getSession(false);

    project = (Projectsession.getAttribute("prj");
    pst = con.prepareStatement("select * from project where id=?");
    pst.setInt(1, project.getId());
    ResultSet rs = pst.executeQuery();

    if (rs.next()) {
      pst = con
          .prepareStatement("update  project set name=?,cost=?,duration=? where id=?");
      pst.setInt(4, project.getId());
      pst.setString(1, project.getName());
      pst.setFloat(2, project.getCost());
      pst.setInt(3, project.getDuration());
      pst.executeUpdate();
      session.setAttribute("msg""Record Updated successfully with id "
          + project.getId());
      return "ProjectOperation";
    else {

      session.setAttribute("msg",
          "Project Id already Present<br>Cannot be updated");
      return "ProjectFail";
    }
  }
}

  • Now create a xhtml file in the WebContent
  • Save it as ProjectForm.xhtml

ProjectShowAll.xhtml

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:ui="http://java.sun.com/jsf/facelets"
  xmlns:h="http://java.sun.com/jsf/html"
  xmlns:f="http://java.sun.com/jsf/core">
<h:body>
  <h1>Project Information</h1>
  <h:dataTable var="pr" value="#{prjimpl.project1}" bgcolor="yellow"
    border="1" width="40%">
    <h:column>
      <f:facet name="header">Id
      </f:facet>
        #{pr.id
    </h:column>

    <h:column>
      <f:facet name="header">name
      </f:facet>
        #{pr.name
    </h:column>

    <h:column>
      <f:facet name="header">Cost
      </f:facet>
        #{pr.cost
    </h:column>
    <h:column>
      <f:facet name="header">Duration
      </f:facet>
        #{pr.duration
    </h:column>

  </h:dataTable>
</h:body>


</html>

  • Now create a xhtml file in the WebContent
  • Save it as ProjectForm.xhtml
ProjectOperation.xhtml

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:ui="http://java.sun.com/jsf/facelets"
  xmlns:h="http://java.sun.com/jsf/html"
  xmlns:f="http://java.sun.com/jsf/core">
<h:body>
  <h1><h:outputText value="#{sessionScope.msg}" /></h1>
  </h:body>
</html>



  • Now create a xhtml file in the WebContent
  • Save it as ProjectForm.xhtml

ProjectSearch.xhtml

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:ui="http://java.sun.com/jsf/facelets"
  xmlns:h="http://java.sun.com/jsf/html"
  xmlns:f="http://java.sun.com/jsf/core">
<h:body>
  <h1>Project Found</h1>

  <ui:repeat value="#{sessionScope.prj1}" var="pr">
      Id is #{pr.id}<br />
  Name Is  #{pr.name}<br />
  Cost is   #{pr.cost}<br />
  Duration is #{pr.duration}
  </ui:repeat>

</h:body>
</html>

  • Now create a xhtml file in the WebContent
  • Save it as ProjectForm.xhtml

ProjectFail.xhtml

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:ui="http://java.sun.com/jsf/facelets"
  xmlns:h="http://java.sun.com/jsf/html"
  xmlns:f="http://java.sun.com/jsf/core">
<h:body>
  <h1>Error page</h1>

  <h2><h:outputText value="#{sessionScope.msg}" /></h2>

</h:body>
</html>

Running the project

  • Rt Click on the ProjectForm.xhtml
  • Select Run As-->Run on Server.
  • Select Tomcat7
  • Now add the required data.
  • Click on Add Project Record



  • It will show Record Added.
  • If any Problem then it will goto ProjectFail.xhtml




  • Add multiple  record.
  • Then click on ShowAll Record.




  • It will display all the records.




  • Come back.
  • Now we want to update record.
  • Add the changed value.
  • Click on Update Project Record.



  • It will update the record.
  • Following message will be displayed.




  • Similarly do for  delete and search.
Download the above Eclipse code
Download the library zip file