0% found this document useful (0 votes)
150 views20 pages

Create - JSF: AIM: Inventory Application Using JSF and Java Beans

This document contains code for an inventory application created using JSF and Java beans. It includes the code for create.jsf, show.jsf, edit.jsf pages and DetailList.java bean class to add, display and edit inventory items in a database.

Uploaded by

Tabby S
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
150 views20 pages

Create - JSF: AIM: Inventory Application Using JSF and Java Beans

This document contains code for an inventory application created using JSF and Java beans. It includes the code for create.jsf, show.jsf, edit.jsf pages and DetailList.java bean class to add, display and edit inventory items in a database.

Uploaded by

Tabby S
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 20

Name: Atir Shaikh

UID: 19BIT043 Date: 14th September 2021

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

<h:outputText value="Enter Product Id" class="control-label col-sm-2"/>


</h:outputLabel>
<div class="col-sm-4">

<h:inputText id="TextID" value="#{items.itemId}"/>


</div>
<br/>
</div>

<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">

<h:inputText id="TextPrice" value="#{items.itemRate}"/>


</div>
<br/>
</div>
Name: Atir Shaikh
UID: 19BIT043 Date: 14th September 2021

<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:dataTable value="#{jbean.getAllItems()}" var="u" border="1" class="table table-


striped table-hover table-bordered">

<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:commandButton id="cmddelete" value="Delete"


action="#{jbean.deleteItem(u.itemId)}" class="btn btn-danger ml-2"/>
</h:column>

</h:dataTable>
</h:form>

</h:body>
</html>

Edit.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>Update Item</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>

</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">

<h:inputText id="TextId" value="#{editItem.itemId}"/>


</div>
<br/>
</div>

<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">

<h:inputText id="TextQty" value="#{editItem.itemQuantity}"/>


<br/>
</div>
</div>

<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

<h:inputText id="TextPrice" value="#{editItem.itemRate}"/>


<br/>
</div>
</div>

<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")

public class DetailList {

private Map<String, Object> sessionMap =


FacesContext.getCurrentInstance().getExternalContext().getSessionMap();

public List<ItemDetails> getAllItems() throws ClassNotFoundException {


List<ItemDetails> items = new ArrayList<ItemDetails>();
int result = 0;
String driverName = "com.mysql.cj.jdbc.Driver";

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;
}

public String editItem(int id) throws ClassNotFoundException {


int result = 0;
ItemDetails item = new ItemDetails();
String driverName = "com.mysql.cj.jdbc.Driver";

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;

try (Connection con = DriverManager.getConnection(conString, name, password)) {

Statement stmt = con.createStatement();


rs = stmt.executeQuery("SELECT * FROM ej_ca2.inventory WHERE id = " + (id));

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();

System.out.println("Edit Item done!");

} catch (Exception e) {
System.out.println("Exception is" + e);
Name: Atir Shaikh
UID: 19BIT043 Date: 14th September 2021

e.printStackTrace();
}

return "Edit";

public String update(ItemDetails item) throws ClassNotFoundException {

String driverName = "com.mysql.cj.jdbc.Driver";

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";

try (Connection con = DriverManager.getConnection(conString, name, password)) {


int result;

//String rs=("UPDATE ej_ca2.inventory SET item=?, quantity=?, rate=? WHERE id=?");


PreparedStatement stmt = con.prepareStatement("UPDATE ej_ca2.inventory SET
item=?, quantity=?, rate=? WHERE id=?");

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";
}

public String deleteItem(int id) throws ClassNotFoundException{

String driverName = "com.mysql.cj.jdbc.Driver";

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";

try (Connection con = DriverManager.getConnection(conString, name, password)) {


int result;

//String rs=("UPDATE ej_ca2.inventory SET item=?, quantity=?, rate=? WHERE id=?");


PreparedStatement stmt = con.prepareStatement("DELETE FROM ej_ca2.inventory
where id = "+ id);
stmt.executeUpdate();

con.close();

} catch (Exception e) {
System.out.println("Exception is" + e);
e.printStackTrace();
}

return "show";
}

public String add() throws ClassNotFoundException {


// DetailList dl = new DetailList();
ItemDetails item = new ItemDetails();
int result = 0;
String driverName = "com.mysql.cj.jdbc.Driver";
Name: Atir Shaikh
UID: 19BIT043 Date: 14th September 2021

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";

try (Connection con = DriverManager.getConnection(conString, name, password)) {


PreparedStatement st = con.prepareStatement("INSERT INTO ej_ca2.inventory
VALUES(?, ?, ?, ?)");
st.setInt(1, Integer.valueOf(item.getItemId()));
st.setString(2, item.getItemName());
st.setInt(3, Integer.valueOf(item.getItemQuantity()));
st.setInt(4, Integer.valueOf(item.getItemRate()));
result = st.executeUpdate();

} catch (Exception e) {
System.out.println("Exception is" + e);
e.printStackTrace();
}

// System.out.println(this.itemName + " " + this.itemQuantity + " " + this.itemRate);


if (result == 1) {
System.out.println("Product Inserted!");
}

return "Success";
}

ItemDetail.java (Model)

/*

* 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 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

*/

public class ItemDetails {

private String itemName;

private int itemQuantity;

private int itemRate;

private int itemId;

//List allitems = new ArrayList<>();

public String getItemName() {

return this.itemName;

public void setItemName(String name) {


Name: Atir Shaikh
UID: 19BIT043 Date: 14th September 2021

this.itemName = name;

public int getItemQuantity() {

return this.itemQuantity;

public void setItemQuantity(int qnty) {

this.itemQuantity = qnty;

public int getItemRate() {

return this.itemRate;

public void setItemRate(int rate) {

this.itemRate = rate;

public int getItemId() {

return this.itemId;

public void setItemId(int id) {

this.itemId = id;

public String edit() throws ClassNotFoundException {

int result = 0;
Name: Atir Shaikh
UID: 19BIT043 Date: 14th September 2021

String driverName = "com.mysql.cj.jdbc.Driver";

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";

try (Connection con = DriverManager.getConnection(conString, name, password)) {

} catch (Exception e) {

System.out.println("Exception is" + e);

return null;

public String add() throws ClassNotFoundException {

// DetailList dl = new DetailList();

int result = 0;

String driverName = "com.mysql.cj.jdbc.Driver";

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";

try (Connection con = DriverManager.getConnection(conString, name, password)) {


Name: Atir Shaikh
UID: 19BIT043 Date: 14th September 2021

PreparedStatement st = con.prepareStatement("INSERT INTO ej_ca2.inventory


VALUES(?, ?, ?, ?)");

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("Exception is" + e);

// System.out.println("Product inserted");

// System.out.println(this.itemName + " " + this.itemQuantity + " " + this.itemRate);

if (result == 1) {

System.out.println("Product Inserted!");

if (this.itemQuantity > 2000) {

return "Fail";

return "show";

Output:
Create New Item:
Name: Atir Shaikh
UID: 19BIT043 Date: 14th September 2021

View all items in the database:


Name: Atir Shaikh
UID: 19BIT043 Date: 14th September 2021

Edit a particular item from the list:


Name: Atir Shaikh
UID: 19BIT043 Date: 14th September 2021

Update an item:

Delete particular item from list:


Name: Atir Shaikh
UID: 19BIT043 Date: 14th September 2021

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy