Press enter to see results or esc to cancel.


JSF MySQL CRUD Create Read Update Delete Project-JSF Tutorial Part 10

This tutorial shows a simple Create Read Update Delete project in Java Server Faces JSF. Mysql is used as database here and the IDE used is NetBeans

  1. Project structure in NetBeans

  2. Mysql Database Data

  3. DB_connection.java
    package com.chillyfacts.com;
    import java.sql.Connection;
    import java.sql.DriverManager;
    public class DB_connection {
        public static void main(String[] args) throws Exception{
            DB_connection obj_DB_connection=new DB_connection();
            System.out.println(obj_DB_connection.get_connection());
        }
        public Connection get_connection() throws Exception{
            Connection connection=null;
            try {
            Class.forName("com.mysql.jdbc.Driver");
            connection=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","root");
            } catch (Exception e) {
                System.out.println(e);
            }
            return connection;
        }
    }
    
  4. Category.java
    package com.chillyfacts.com;
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.Statement;
    import java.util.ArrayList;
    import java.util.Map;
    import javax.faces.bean.ManagedBean;
    import javax.faces.bean.RequestScoped;
    import javax.faces.context.FacesContext;
    @ManagedBean
    @RequestScoped
    public class Category {
        private String category_name;
     private String sl_no;
        public void setSl_no(String sl_no) {
            this.sl_no = sl_no;
        }
        public String getSl_no() {
            return sl_no;
        }
        public void setCategory_name(String category_name) {
            this.category_name = category_name;
        }
        public String getCategory_name() {
            return category_name;
        }
    
    
    private Map<String,Object> sessionMap = FacesContext.getCurrentInstance().getExternalContext().getSessionMap(); 
    
    public String edit_Category(){
         FacesContext fc = FacesContext.getCurrentInstance();
         Map<String,String> params = fc.getExternalContext().getRequestParameterMap();
         String field_sl_no= params.get("action");
         try {
              DB_connection obj_DB_connection=new DB_connection();
              Connection connection=obj_DB_connection.get_connection();
              Statement st=connection.createStatement();
              ResultSet rs=st.executeQuery("select * from categories where sl_no="+field_sl_no);
              Category obj_Category=new Category();
              rs.next();
              obj_Category.setCategory_name(rs.getString("category_name"));
              obj_Category.setSl_no(rs.getString("sl_no"));
              sessionMap.put("editcategory", obj_Category);  
          } catch (Exception e) {
                System.out.println(e);
          }
         return "/edit.xhtml?faces-redirect=true";   
    }
    
    
    
    public String delete_Category(){
          FacesContext fc = FacesContext.getCurrentInstance();
          Map<String,String> params = fc.getExternalContext().getRequestParameterMap();
          String field_sl_no= params.get("action");
          try {
             DB_connection obj_DB_connection=new DB_connection();
             Connection connection=obj_DB_connection.get_connection();
           PreparedStatement ps=connection.prepareStatement("delete from categories where sl_no=?");
             ps.setString(1, field_sl_no);
             System.out.println(ps);
             ps.executeUpdate();
            } catch (Exception e) {
             System.out.println(e);
            }
           return "/index.xhtml?faces-redirect=true";   
    }
    
    
    
    public String update_category(){
            FacesContext fc = FacesContext.getCurrentInstance();
            Map<String,String> params = fc.getExternalContext().getRequestParameterMap();
    	String	update_sl_no= params.get("update_sl_no");
            try {
                DB_connection obj_DB_connection=new DB_connection();
                Connection connection=obj_DB_connection.get_connection();
          PreparedStatement ps=connection.prepareStatement("update categories set category_name=? where sl_no=?");
                ps.setString(1, category_name);
                ps.setString(2, update_sl_no);
                System.out.println(ps);
                ps.executeUpdate();
            } catch (Exception e) {
                System.out.println(e);
            }
           return "/index.xhtml?faces-redirect=true";   
    }
    
    
    
    public ArrayList getGet_all_category() throws Exception{
            ArrayList list_of_categories=new ArrayList();
                 Connection connection=null;
            try {
                DB_connection obj_DB_connection=new DB_connection();
                connection=obj_DB_connection.get_connection();
                Statement st=connection.createStatement();
                ResultSet rs=st.executeQuery("select * from categories");
                while(rs.next()){
                    Category obj_Category=new Category();
                    obj_Category.setCategory_name(rs.getString("category_name"));
                    obj_Category.setSl_no(rs.getString("sl_no"));
                    list_of_categories.add(obj_Category);
                }
            } catch (Exception e) {
                System.out.println(e);
            }finally{
                if(connection!=null){
                    connection.close();
                }
            }
            return list_of_categories;
    }
    
    
    
    public void add_Category(){
            try {
                Connection connection=null;
                DB_connection obj_DB_connection=new DB_connection();
                connection=obj_DB_connection.get_connection();
    PreparedStatement ps=connection.prepareStatement("insert into categories(category_name) value('"+category_name+"')");
                ps.executeUpdate();
            } catch (Exception e) {
                System.out.println(e);
            }
        }
        public Category() {
        }
    }
    
  5. index.xhtml
    <?xml version='1.0' encoding='UTF-8' ?>
    <!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:f="http://java.sun.com/jsf/core"
      xmlns:h="http://xmlns.jcp.org/jsf/html">
      <h:head>
        <title>Facelet Title</title>
      </h:head>
      <h:body>
      <center>
        <h2>Categories</h2>
        <h:form>
          Enter Category : <h:inputText value="#{category.category_name}"></h:inputText>
          <h:commandButton value="Add" action="#{category.add_Category}"></h:commandButton>
        </h:form>
        <h:form>
        <h:dataTable value="#{category.get_all_category}" var="cat">
        <h:column>
          <f:facet name="header">Sl NO</f:facet>
          <h:outputText value="#{cat.sl_no}"></h:outputText>
        </h:column>
        <h:column>
          <f:facet name="header">Category</f:facet>
          <h:outputText value="#{cat.category_name}" ></h:outputText>
        </h:column>
        <h:column>
          <f:facet name="header">Edit</f:facet>
          <h:commandButton value="Edit" action="#{category.edit_Category}">
            <f:param name="action" value="#{cat.sl_no}" />
          </h:commandButton>
        </h:column>
        <h:column>
          <f:facet name="header">Delete</f:facet>
          <h:commandButton value="Delete" action="#{category.delete_Category}">
            <f:param name="action" value="#{cat.sl_no}" />
          </h:commandButton>
        </h:column>
        </h:dataTable>
      </h:form>
      </center>
    </h:body>
    </html>
    
  6. edit.xhtml
    <?xml version='1.0' encoding='UTF-8' ?>
    <!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:f="http://java.sun.com/jsf/core"
    xmlns:h="http://xmlns.jcp.org/jsf/html">
    <h:head>
    <title>Facelet Title</title>
    </h:head>
      <h:body>
       <center>
       <h:form>
        <h1>Edit Category</h1>
         Enter Category : <h:inputText value="#{editcategory.category_name}"></h:inputText>
        <h:commandButton value="Update" action="#{editcategory.update_category}">
         <f:param name="update_sl_no" value="#{editcategory.sl_no}" />
        </h:commandButton>
       </h:form>
      </center>
    </h:body>
    </html>
  7. web.xml
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
         <context-param>
             <param-name>javax.faces.PROJECT_STAGE</param-name>
             <param-value>Development</param-value>
         </context-param>
         <servlet>
             <servlet-name>Faces Servlet</servlet-name>
             <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
             <load-on-startup>1</load-on-startup>
         </servlet>
         <servlet-mapping>
             <servlet-name>Faces Servlet</servlet-name>
             <url-pattern>/faces/*</url-pattern>
         </servlet-mapping>
         <session-config>
             <session-timeout>
                 30
             </session-timeout>
         </session-config>
         <welcome-file-list>
             <welcome-file>faces/index.xhtml</welcome-file>
         </welcome-file-list>
    </web-app>
    
  8. http://localhost:8080/CRUDJSF/faces/index.xhtml

    
    
  9. http://localhost:8080/CRUDJSF/faces/edit.xhtml

  10. Download the project here.

    CRUDJSF.rar