Create - JSF: AIM: Inventory Application Using JSF and Java Beans
Create - JSF: AIM: Inventory Application Using JSF and Java Beans
CA 2
AIM: Inventory Application using JSF and Java Beans.
Create.jsf
<?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:h="http://xmlns.jcp.org/jsf/html">
<h:head>
<title>Adding product</title>
<link
style="C:\Users\Zod\Documents\NetBeansProjects\ca2\src\main\webapp\style.css"/>
<h:outputStylesheet name="/css/bootstrap.min.css"/>
<link href="css/bootstrap.min.css" rel="stylesheet" type="text/css"/>
<style type="text/css">
table {
border-collapse: collapse;
width: 100%;
}
th, td {
text-align: left;
padding: 8px;
}
tr:nth-child(even){background-color: #f2f2f2}
th {
background-color: #4CAF50;
color: white;
}
</style>
</h:head>
<h:body>
<h:form class="d-flex justify-content-center m-3">
<div class="form-group">
<div class="col-sm-4">
<h2 class="control-label col-sm-2">Add items</h2>
</div>
<br/>
</div>
<div class="form-group">
<h:outputLabel for="TextID" >
Name: Atir Shaikh
UID: 19BIT043 Date: 14th September 2021
<div class="form-group">
<h:outputLabel for="TextName" >
<h:outputText value="Enter Product name" class="control-label col-sm-2"/>
</h:outputLabel>
<div class="col-sm-4">
<h:inputText id="Textname" value="#{items.itemName}"/>
</div>
<br/>
</div>
<div class="form-group">
<h:outputLabel for="TextQty">
<h:outputText value="Enter Product Quantity" class="control-label col-sm-2"/>
</h:outputLabel>
<div class="col-sm-4">
<h:inputText id="TextQty" value="#{items.itemQuantity}"/>
</div>
<br/>
</div>
<div class="form-group">
<h:outputLabel for="TextPrice">
<h:outputText value="Enter Product Price" class="control-label col-sm-2"/>
</h:outputLabel>
<div class="col-sm-4">
<div class="form-group">
<div class="col-sm-4">
<h:commandButton id="cmdInsert" value="Add Item" action="#{items.add()}"
class="btn btn-dark h-100 m-4"/>
</div>
</div>
</h:form>
</h:body>
</html>
Show.jsf
<?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:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:c="http://xmlns.jcp.org/jsp/jstl/core">
<h:head>
<title>Show facelet</title>
<h:outputStylesheet name="/css/bootstrap.min.css"/> .
<link href="css/bootstrap.min.css" rel="stylesheet" type="text/css"/>
<style type="text/css">
table {
border-collapse: collapse;
width: 100%;
}
th, td {
text-align: left;
padding: 8px;
}
tr:nth-child(odd){background-color: #bfd3ff}
th {
background-color: #203d7f;
color: white;
}
</style>
Name: Atir Shaikh
UID: 19BIT043 Date: 14th September 2021
</h:head>
<h:body>
<h:form>
<h1>Inventory System</h1>
<h:column>
<f:facet name="header">
Item ID
</f:facet>
#{u.itemId}
</h:column>
<h:column>
<f:facet name="header">
Item Name
</f:facet>
#{u.itemName}
</h:column>
<h:column>
<f:facet name="header">
Quantity
</f:facet>
#{u.itemQuantity}
</h:column>
<h:column>
<f:facet name="header">
Rate
</f:facet>
#{u.itemRate}
</h:column>
<h:column>
<f:facet name="header">
Edit/Delete
</f:facet>
<h:commandButton id="cmdedit" value="Edit"
action="#{jbean.editItem(u.itemId)}" class="btn btn-dark"/>
Name: Atir Shaikh
UID: 19BIT043 Date: 14th September 2021
</h:dataTable>
</h:form>
</h:body>
</html>
Edit.jsf
tr:nth-child(odd){background-color: #bfd3ff}
th {
background-color: #203d7f;
color: white;
}
</style>
</h:head>
<h:body>
<h:form class="d-flex justify-content-center">
<div class="form-group">
Name: Atir Shaikh
UID: 19BIT043 Date: 14th September 2021
<div class="col-sm-4">
<h2 class="control-label col-sm-2">Edit Items</h2>
</div>
<br/>
</div>
<div class="form-group">
<h:outputLabel for="TextName" >
<h:outputText value="Product Id" class="control-label col-sm-2"/>
</h:outputLabel>
<div class="col-sm-4">
<div class="form-group">
<h:outputLabel for="TextName">
<h:outputText value="Product name" class="control-label col-sm-2"/>
</h:outputLabel>
<div class="col-sm-4">
<h:inputText id="Textname" value="#{editItem.itemName}" />
<br/>
</div>
</div>
<div class="form-group">
<h:outputLabel for="TextQty">
<h:outputText value="Product Quantity" class="control-label col-sm-2"/>
</h:outputLabel>
<div class="col-sm-4">
<div class="form-group">
<h:outputLabel for="TextPrice">
<h:outputText value="Product Price" class="control-label col-sm-2"/>
</h:outputLabel>
<div class="col-sm-4">
Name: Atir Shaikh
UID: 19BIT043 Date: 14th September 2021
<div class="form-group">
<div class="col-sm-4">
<h:commandButton id="cmdUpdate" value="Update Item"
action="#{jbean.update(editItem)}" class="btn btn-dark h-100 m-4"/>
</div>
</div>
</h:form>
</h:body>
</html>
DetailList.java (JavaBean)
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package Beans;
import java.util.List;
import Entity.ItemDetails;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
Name: Atir Shaikh
UID: 19BIT043 Date: 14th September 2021
/**
*
* @author Zod
*/
@ManagedBean(name = "jbean")
Class.forName(driverName);
final String conString = "jdbc:mysql://localhost:3306/ej_ca2?
useTimeZone=true&serverTimezone=UTC&autoReconnect=true&useSSL=false";
String name = "root";
String password = "root";
PreparedStatement p = null;
Connection con = null;
ResultSet rs = null;
try {
con = DriverManager.getConnection(conString, name, password);
final String sql = "SELECT * FROM ej_ca2.inventory";
p = con.prepareStatement(sql);
rs = p.executeQuery();
while (rs.next()) {
ItemDetails item = new ItemDetails();
item.setItemName(rs.getString("item"));
item.setItemQuantity(rs.getInt("quantity"));
item.setItemRate(rs.getInt("rate"));
item.setItemId(rs.getInt("id"));
items.add(item);
} catch (Exception e) {
e.printStackTrace();
Name: Atir Shaikh
UID: 19BIT043 Date: 14th September 2021
} finally {
try {
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return items;
}
Class.forName(driverName);
final String conString = "jdbc:mysql://localhost:3306/ej_ca2?
useTimeZone=true&serverTimezone=UTC&autoReconnect=true&useSSL=false";
String name = "root";
String password = "root";
ResultSet rs;
rs.next();
item.setItemId(rs.getInt("id"));
item.setItemName(rs.getString("item"));
item.setItemQuantity(rs.getInt("quantity"));
item.setItemRate(rs.getInt("rate"));
sessionMap.put("editItem", item);
con.close();
} catch (Exception e) {
System.out.println("Exception is" + e);
Name: Atir Shaikh
UID: 19BIT043 Date: 14th September 2021
e.printStackTrace();
}
return "Edit";
Class.forName(driverName);
final String conString = "jdbc:mysql://localhost:3306/ej_ca2?
useTimeZone=true&serverTimezone=UTC&autoReconnect=true&useSSL=false";
String name = "root";
String password = "root";
stmt.setString(1, item.getItemName());
stmt.setInt(2, item.getItemQuantity());
stmt.setInt(3, item.getItemRate());
stmt.setInt(4, item.getItemId());
try {
result = stmt.executeUpdate();
if (result == 1) {
System.out.println("Updated successfully");
}
} catch (Exception e) {
e.printStackTrace();
}
Name: Atir Shaikh
UID: 19BIT043 Date: 14th September 2021
con.close();
} catch (Exception e) {
System.out.println("Exception is" + e);
e.printStackTrace();
}
return "show";
}
Class.forName(driverName);
final String conString = "jdbc:mysql://localhost:3306/ej_ca2?
useTimeZone=true&serverTimezone=UTC&autoReconnect=true&useSSL=false";
String name = "root";
String password = "root";
con.close();
} catch (Exception e) {
System.out.println("Exception is" + e);
e.printStackTrace();
}
return "show";
}
Class.forName(driverName);
final String conString = "jdbc:mysql://localhost:3306/ej_ca2?
useTimeZone=true&serverTimezone=UTC&autoReconnect=true&useSSL=false";
String name = "root";
String password = "root";
} catch (Exception e) {
System.out.println("Exception is" + e);
e.printStackTrace();
}
return "Success";
}
ItemDetail.java (Model)
/*
*/
package Entity;
import Beans.DetailList;
Name: Atir Shaikh
UID: 19BIT043 Date: 14th September 2021
import javax.faces.bean.ManagedBean;
import Entity.ItemDetails;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.RequestDispatcher;
@ManagedBean(name = "items")
/**
* @author Zod
*/
return this.itemName;
this.itemName = name;
return this.itemQuantity;
this.itemQuantity = qnty;
return this.itemRate;
this.itemRate = rate;
return this.itemId;
this.itemId = id;
int result = 0;
Name: Atir Shaikh
UID: 19BIT043 Date: 14th September 2021
Class.forName(driverName);
} catch (Exception e) {
return null;
int result = 0;
Class.forName(driverName);
st.setInt(1, Integer.valueOf(this.getItemId()));
st.setString(2, this.getItemName());
st.setInt(3, Integer.valueOf(this.getItemQuantity()));
st.setInt(4, Integer.valueOf(this.getItemRate()));
result = st.executeUpdate();
} catch (Exception e) {
// System.out.println("Product inserted");
if (result == 1) {
System.out.println("Product Inserted!");
return "Fail";
return "show";
Output:
Create New Item:
Name: Atir Shaikh
UID: 19BIT043 Date: 14th September 2021
Update an item: