diff --git a/RentItAll/.classpath b/RentItAll/.classpath index a5f4a7e..46cb932 100644 --- a/RentItAll/.classpath +++ b/RentItAll/.classpath @@ -3,9 +3,15 @@ + + + + + + diff --git a/RentItAll/WebContent/ItemForm.jsp b/RentItAll/WebContent/ItemForm.jsp new file mode 100644 index 0000000..ec7a68f --- /dev/null +++ b/RentItAll/WebContent/ItemForm.jsp @@ -0,0 +1,96 @@ +<%@ page language="java" contentType="text/html; charset=UTF-8" + pageEncoding="UTF-8"%> +<%@ taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %> + + + + + + + Rent It All Items + + + +

Items Management

+

+ Add New Item +     + List All Items + +

+ +
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+

+ + Edit Item + + + Add New Item + +

+
Name: + +
Category: + +
Price: + +
Duration: + +
Security Deposit: + +
+ +
+
+
+ + \ No newline at end of file diff --git a/RentItAll/WebContent/ItemList.jsp b/RentItAll/WebContent/ItemList.jsp new file mode 100644 index 0000000..64560ad --- /dev/null +++ b/RentItAll/WebContent/ItemList.jsp @@ -0,0 +1,56 @@ +<%@ page language="java" contentType="text/html; charset=UTF-8" + pageEncoding="UTF-8"%> +<%@ taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %> + + + + + + + Rent It All Items + + + + +

ITEMS FOR RENTING

+

+ Add New Item +     + List All Items +

+ +
+ + + + + + + + + + + + + + + + + + + + + + + +
+ List of Items +
IDNameCategoryPriceDurationSecurity Deposit Actions
+ Edit +      + Delete +
+
+ + + \ No newline at end of file diff --git a/RentItAll/WebContent/WEB-INF/lib/jstl-impl-1.2.jar b/RentItAll/WebContent/WEB-INF/lib/jstl-impl-1.2.jar new file mode 100644 index 0000000..110ca51 Binary files /dev/null and b/RentItAll/WebContent/WEB-INF/lib/jstl-impl-1.2.jar differ diff --git a/RentItAll/WebContent/WEB-INF/lib/jstl-standard.jar b/RentItAll/WebContent/WEB-INF/lib/jstl-standard.jar new file mode 100644 index 0000000..c74dbad Binary files /dev/null and b/RentItAll/WebContent/WEB-INF/lib/jstl-standard.jar differ diff --git a/RentItAll/WebContent/WEB-INF/lib/jstl.jar b/RentItAll/WebContent/WEB-INF/lib/jstl.jar new file mode 100644 index 0000000..a151faf Binary files /dev/null and b/RentItAll/WebContent/WEB-INF/lib/jstl.jar differ diff --git a/RentItAll/WebContent/WEB-INF/web.xml b/RentItAll/WebContent/WEB-INF/web.xml index b75860e..9cb3e73 100644 --- a/RentItAll/WebContent/WEB-INF/web.xml +++ b/RentItAll/WebContent/WEB-INF/web.xml @@ -1,6 +1,8 @@ + RentItAll + index.html index.htm @@ -9,4 +11,30 @@ default.htm default.jsp + + + jdbcURL + jdbc:mysql://localhost:3306/RentItAll + + + + jdbcUsername + root + + + + jdbcPassword + P@ssw0rd + + + + ControllerServlet + net.codejava.javaee.RentItAll.ControllerServlet + + + + ControllerServlet + / + + \ No newline at end of file diff --git a/RentItAll/build/classes/Item.class b/RentItAll/build/classes/Item.class deleted file mode 100644 index d12f1dd..0000000 Binary files a/RentItAll/build/classes/Item.class and /dev/null differ diff --git a/RentItAll/build/classes/ItemDBC.class b/RentItAll/build/classes/ItemDBC.class deleted file mode 100644 index e950a71..0000000 Binary files a/RentItAll/build/classes/ItemDBC.class and /dev/null differ diff --git a/RentItAll/build/classes/items/ControllerServlet.class b/RentItAll/build/classes/items/ControllerServlet.class new file mode 100644 index 0000000..23b7fda Binary files /dev/null and b/RentItAll/build/classes/items/ControllerServlet.class differ diff --git a/RentItAll/build/classes/items/Item.class b/RentItAll/build/classes/items/Item.class new file mode 100644 index 0000000..368b985 Binary files /dev/null and b/RentItAll/build/classes/items/Item.class differ diff --git a/RentItAll/build/classes/items/ItemDBC.class b/RentItAll/build/classes/items/ItemDBC.class new file mode 100644 index 0000000..75719c1 Binary files /dev/null and b/RentItAll/build/classes/items/ItemDBC.class differ diff --git a/RentItAll/src/items/ControllerServlet.java b/RentItAll/src/items/ControllerServlet.java new file mode 100644 index 0000000..0e153f3 --- /dev/null +++ b/RentItAll/src/items/ControllerServlet.java @@ -0,0 +1,120 @@ +package items; + +import java.io.IOException; +import java.sql.SQLException; +import java.util.List; + +import javax.servlet.GenericServlet; +import javax.servlet.RequestDispatcher; +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + + +public class ControllerServlet extends HttpServlet { + private static final long serialVersionUID = 1L; + private ItemDBC itemDBC; + + public void init() { + String jdbcURL = getServletContext().getInitParameter("jdbcURL"); + String jdbcUsername = getServletContext().getInitParameter("jdbcUsername"); + String jdbcPassword = getServletContext().getInitParameter("jdbcPassword"); + + itemDBC = new ItemDBC(jdbcURL, jdbcUsername, jdbcPassword); + } + + protected void doPost(HttpServletRequest request, HttpServletResponse response) + throws ServletException, IOException { + doGet(request, response); + } + + protected void doGet(HttpServletRequest request, HttpServletResponse response) + throws ServletException, IOException { + String action = request.getServletPath(); + + try { + switch (action) { + case "/new": + showNewForm(request, response); + break; + case "/insert": + insertItem(request, response); + break; + case "/delete": + deleteItem(request, response); + break; + case "/edit": + showEditForm(request, response); + break; + case "/update": + updateItem(request, response); + break; + default: + listItem(request, response); + break; + } + } catch (SQLException ex) { + throw new ServletException(ex); + } + } + + private void listItem(HttpServletRequest request, HttpServletResponse response) + throws SQLException, IOException, ServletException { + List listItem = ItemDBC.listAllItems(); + request.setAttribute("listItem", listItem); + RequestDispatcher dispatcher = request.getRequestDispatcher("ItemList.jsp"); + dispatcher.forward(request, response); + } + + private void showNewForm(HttpServletRequest request, HttpServletResponse response) + throws ServletException, IOException { + RequestDispatcher dispatcher = request.getRequestDispatcher("ItemForm.jsp"); + dispatcher.forward(request, response); + } + + private void showEditForm(HttpServletRequest request, HttpServletResponse response) + throws SQLException, ServletException, IOException { + int id = Integer.parseInt(request.getParameter("ID")); + Item existingItem = ItemDBC.getItem(id); + RequestDispatcher dispatcher = request.getRequestDispatcher("ItemForm.jsp"); + request.setAttribute("item", existingItem); + dispatcher.forward(request, response); + } + + private void insertItem(HttpServletRequest request, HttpServletResponse response) + throws SQLException, IOException { + String name = request.getParameter("Name"); + String category = request.getParameter("category"); + int price = Integer.parseInt(request.getParameter("PricePerDay")); + int duration = Integer.parseInt(request.getParameter("Duration")); + int deposit = Integer.parseInt(request.getParameter("SecurityDepo")); + + Item newItem = new Item(name, category, price, duration, deposit); + ItemDBC.insertItem(newItem); + response.sendRedirect("list"); + } + + private void updateItem(HttpServletRequest request, HttpServletResponse response) + throws SQLException, IOException { + int id = Integer.parseInt(request.getParameter("ID")); + String name = request.getParameter("Name"); + String category = request.getParameter("Category"); + int price = Integer.parseInt(request.getParameter("PricePerDay")); + int duration = Integer.parseInt(request.getParameter("Duration")); + int deposit = Integer.parseInt(request.getParameter("SecurityDepo")); + + Item item = new Item(id, name, category, price, duration, deposit); + ItemDBC.updateItem(item); + response.sendRedirect("list"); + } + + private void deleteItem(HttpServletRequest request, HttpServletResponse response) + throws SQLException, IOException { + int id = Integer.parseInt(request.getParameter("ID")); + + Item item = new Item(id); + ItemDBC.deleteItem(item); + response.sendRedirect("list"); + } +} diff --git a/RentItAll/src/Item.java b/RentItAll/src/items/Item.java similarity index 80% rename from RentItAll/src/Item.java rename to RentItAll/src/items/Item.java index 9771e22..63d03a1 100644 --- a/RentItAll/src/Item.java +++ b/RentItAll/src/items/Item.java @@ -1,10 +1,12 @@ - +package items; +//model class for item table public class Item { protected int id; protected String name; protected String category; protected int price; protected int duration; + protected int deposit; public Item() { @@ -14,20 +16,22 @@ public Item(int id) { this.id = id; } - public Item(int id, String name, String category, int price, int duration) { + public Item(int id, String name, String category, int price, int duration, int deposit) { this.name = name; this.category = category; this.price = price; this.duration = duration; this.id = id; + this.deposit = deposit; } - public Item(String name, String category, int price, int duration) { + public Item(String name, String category, int price, int duration, int deposit) { this.name = name; this.category = category; this.price = price; this.duration = duration; + this.deposit = deposit; } public int getID() { @@ -69,6 +73,14 @@ public int getDuration() { public void setDuration(int duration) { this.duration = duration; } + + public int getDeposit() { + return price; + } + + public void setDeposit(int price) { + this.price = price; + } } diff --git a/RentItAll/src/ItemDBC.java b/RentItAll/src/items/ItemDBC.java similarity index 76% rename from RentItAll/src/ItemDBC.java rename to RentItAll/src/items/ItemDBC.java index a663c01..d2317dc 100644 --- a/RentItAll/src/ItemDBC.java +++ b/RentItAll/src/items/ItemDBC.java @@ -1,3 +1,4 @@ +package items; import java.sql.Connection; import java.sql.SQLException; import java.sql.DriverManager; @@ -7,11 +8,12 @@ import java.sql.Statement; import java.sql.ResultSet; +//data base management class for items table public class ItemDBC { - private String jdbcURL; - private String jdbcUsername; - private String jdbcPassword; - private Connection jdbcConnection; + private static String jdbcURL; + private static String jdbcUsername; + private static String jdbcPassword; + private static Connection jdbcConnection; public ItemDBC(String jdbcURL, String jdbcUsername, String jdbcPassword) { this.jdbcURL = jdbcURL; @@ -19,26 +21,25 @@ public ItemDBC(String jdbcURL, String jdbcUsername, String jdbcPassword) { this.jdbcPassword = jdbcPassword; } - protected void connect() throws SQLException { + protected static void connect() throws SQLException { if (jdbcConnection == null || jdbcConnection.isClosed()) { try { Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { throw new SQLException(e); } - jdbcConnection = DriverManager.getConnection( - jdbcURL, jdbcUsername, jdbcPassword); + jdbcConnection = DriverManager.getConnection(jdbcURL, jdbcUsername, jdbcPassword); } } - protected void disconnect() throws SQLException { + protected static void disconnect() throws SQLException { if (jdbcConnection != null && !jdbcConnection.isClosed()) { jdbcConnection.close(); } } - public boolean insertItem(Item item) throws SQLException { - String sql = "INSERT INTO ITEMS (ID, Name, Category, PricePerDay, Duration) VALUES (?, ?, ?, ?, ?)"; + public static boolean insertItem(Item item) throws SQLException { + String sql = "INSERT INTO ITEMS (ID, Name, Category, PricePerDay, Duration, SecurityDepo) VALUES (?, ?, ?, ?, ?, ?)"; connect(); PreparedStatement statement = jdbcConnection.prepareStatement(sql); @@ -47,6 +48,7 @@ public boolean insertItem(Item item) throws SQLException { statement.setString(3, item.getCategory()); statement.setInt(4, item.getPrice()); statement.setInt(5, item.getDuration()); + statement.setInt(6, item.getDeposit()); boolean rowInserted = statement.executeUpdate() > 0; statement.close(); @@ -54,7 +56,7 @@ public boolean insertItem(Item item) throws SQLException { return rowInserted; } - public List listAllItems() throws SQLException { + public static List listAllItems() throws SQLException { List listItem = new ArrayList<>(); String sql = "SELECT * FROM ITEMS"; @@ -70,8 +72,9 @@ public List listAllItems() throws SQLException { String category = resultSet.getString("Category"); int price = resultSet.getInt("PricePerDay"); int duration = resultSet.getInt("Duration"); + int deposit = resultSet.getInt("SecurityDepo"); - Item item = new Item(id, name, category, price, duration); + Item item = new Item(id, name, category, price, duration, deposit); listItem.add(item); } @@ -83,7 +86,7 @@ public List listAllItems() throws SQLException { return listItem; } - public boolean deleteItem(Item item) throws SQLException { + public static boolean deleteItem(Item item) throws SQLException { String sql = "DELETE FROM ITEMS where ID = ?"; connect(); @@ -97,8 +100,8 @@ public boolean deleteItem(Item item) throws SQLException { return rowDeleted; } - public boolean updateItem(Item item) throws SQLException { - String sql = "UPDATE ITEMS SET Name = ?, Category = ?, PricePerDay = ?, Duration = ?"; + public static boolean updateItem(Item item) throws SQLException { + String sql = "UPDATE ITEMS SET Name = ?, Category = ?, PricePerDay = ?, Duration = ?, SecurityDepo = ?"; sql += " WHERE ID = ?"; connect(); @@ -107,7 +110,8 @@ public boolean updateItem(Item item) throws SQLException { statement.setString(2, item.getCategory()); statement.setInt(3, item.getPrice()); statement.setInt(4, item.getDuration()); - statement.setInt(5, item.getID()); + statement.setInt(5, item.getDeposit()); + statement.setInt(6, item.getID()); boolean rowUpdated = statement.executeUpdate() > 0; statement.close(); @@ -115,7 +119,7 @@ public boolean updateItem(Item item) throws SQLException { return rowUpdated; } - public Item getItem(int id) throws SQLException { + public static Item getItem(int id) throws SQLException { Item item = null; String sql = "SELECT * FROM ITEMS WHERE ID = ?"; @@ -131,8 +135,9 @@ public Item getItem(int id) throws SQLException { String category = resultSet.getString("Category"); int price = resultSet.getInt("PricePerDay"); int duration = resultSet.getInt("Duration"); + int deposit = resultSet.getInt("SecurityDepo"); - item = new Item(id, name, category, price, duration); + item = new Item(id, name, category, price, duration, deposit); } resultSet.close();