Skip to content

Commit

Permalink
lab sheet 4.2/4.3
Browse files Browse the repository at this point in the history
  • Loading branch information
Izu99 committed Apr 11, 2021
0 parents commit 734ba8b
Show file tree
Hide file tree
Showing 18 changed files with 370 additions and 0 deletions.
21 changes: 21 additions & 0 deletions labsheet4.2/.classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-13">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
<attribute name="org.eclipse.jst.component.dependency" value="/WEB-INF/lib"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>
2 changes: 2 additions & 0 deletions labsheet4.2/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/build/
/target/
37 changes: 37 additions & 0 deletions labsheet4.2/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>labsheet4.2</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.wst.common.project.facet.core.builder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.wst.validation.validationbuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
<nature>org.eclipse.jem.workbench.JavaEMFNature</nature>
<nature>org.eclipse.wst.common.modulecore.ModuleCoreNature</nature>
<nature>org.eclipse.wst.common.project.facet.core.nature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.wst.jsdt.core.jsNature</nature>
</natures>
</projectDescription>
12 changes: 12 additions & 0 deletions labsheet4.2/.settings/.jsdtscope
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry excluding="**/bower_components/*|**/node_modules/*|**/*.min.js" kind="src" path="WebContent"/>
<classpathentry kind="con" path="org.eclipse.wst.jsdt.launching.JRE_CONTAINER"/>
<classpathentry kind="con" path="org.eclipse.wst.jsdt.launching.WebProject">
<attributes>
<attribute name="hide" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.wst.jsdt.launching.baseBrowserLibrary"/>
<classpathentry kind="output" path=""/>
</classpath>
11 changes: 11 additions & 0 deletions labsheet4.2/.settings/org.eclipse.jdt.core.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=13
org.eclipse.jdt.core.compiler.compliance=13
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning
org.eclipse.jdt.core.compiler.release=enabled
org.eclipse.jdt.core.compiler.source=13
4 changes: 4 additions & 0 deletions labsheet4.2/.settings/org.eclipse.m2e.core.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
activeProfiles=
eclipse.preferences.version=1
resolveWorkspaceProjects=true
version=1
15 changes: 15 additions & 0 deletions labsheet4.2/.settings/org.eclipse.wst.common.component
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?><project-modules id="moduleCoreId" project-version="1.5.0">

<wb-module deploy-name="lab4-0.0.1-SNAPSHOT">
<wb-resource deploy-path="/" source-path="/target/m2e-wtp/web-resources"/>

<wb-resource deploy-path="/" source-path="/WebContent" tag="defaultRootSource"/>

<wb-resource deploy-path="/WEB-INF/classes" source-path="/src"/>

<property name="java-output-path" value="/item/build/classes"/>
<property name="context-root" value="lab4"/>

</wb-module>

</project-modules>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<faceted-project>
<fixed facet="wst.jsdt.web"/>
<fixed facet="java"/>
<fixed facet="jst.web"/>
<installed facet="java" version="13"/>
<installed facet="jst.web" version="3.0"/>
<installed facet="wst.jsdt.web" version="1.0"/>
</faceted-project>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.eclipse.wst.jsdt.launching.baseBrowserLibrary
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Window
2 changes: 2 additions & 0 deletions labsheet4.2/.settings/org.eclipse.wst.validation.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
disabled=06target
eclipse.preferences.version=1
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/META-INF/
3 changes: 3 additions & 0 deletions labsheet4.2/WebContent/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Manifest-Version: 1.0
Class-Path:

7 changes: 7 additions & 0 deletions labsheet4.2/WebContent/Views/bootstrap.min.css

Large diffs are not rendered by default.

21 changes: 21 additions & 0 deletions labsheet4.2/WebContent/itemps.jsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<%@page import="java.sql.DriverManager" %>
<%@page import="java.sql.Connection" %>
<%@page import="com.item" %>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
if (request.getParameter("itemCode") != null)
{
item itemObj = new item();
itemObj.connect();//For testing the connect method
}
%>
</body>
</html>
59 changes: 59 additions & 0 deletions labsheet4.2/WebContent/items.jsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<%@page import="com.Item"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%
//Insert item---------------------------------
if (request.getParameter("itemCode") != null) {
Item itemObj = new Item();
String stsMsg = itemObj.insertItem(request.getParameter("itemCode"), request.getParameter("itemName"),
request.getParameter("itemPrice"), request.getParameter("itemDesc"));
session.setAttribute("statusMsg", stsMsg);
}
//Delete item----------------------------------
if (request.getParameter("itemID") != null) {
Item itemObj = new Item();
String stsMsg = itemObj.deleteItem(request.getParameter("itemID"));
session.setAttribute("statusMsg", stsMsg);
}
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<link rel="stylesheet" href="Views/bootstrap.min.css">

<title>Items Management</title>
</head>
<body>

<div class="container">
<div class="row">
<div class="col">
<h1>Items Management</h1>
<form method="post" action="items.jsp">

Item code: <input name="itemCode" type="text" class="form-control"><br>
Item name: <input name="itemName" type="text" class="form-control"><br>
Item price: <input name="itemPrice" type="text" class="form-control"><br>
Item description: <input name="itemDesc" type="text" class="form-control"><br>
<input name="btnSubmit" type="submit" value="Save" class="btn btn-outline-success"> <br><br>

<div class="alert alert-info">
<%
out.print(session.getAttribute("statusMsg"));
%>
</div>
</form>
<br>
<%
Item itemObj = new Item();
out.print(itemObj.readItems());
%>

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

</body>
</html>

33 changes: 33 additions & 0 deletions labsheet4.2/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.PAF</groupId>
<artifactId>lab4</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<release>13</release>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.3</version>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.23</version>
</dependency>
</dependencies>
</project>
131 changes: 131 additions & 0 deletions labsheet4.2/src/com/Item.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
package com;

import java.sql.*;

public class Item {

public Connection connect() {
Connection con = null;

try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/items", "root", "root");
// For testing
System.out.print("Successfully connected");
} catch (Exception e) {
e.printStackTrace();
}

return con;
}

public String insertItem(String code, String name, String price, String desc) {
String output = "";
try {
Connection con = connect();
if (con == null) {
return "Error while connecting to the database";
}
// create a prepared statement
String query = " insert into items(itemID,itemCode,itemName,itemPrice,itemDesc)"
+ " values (?, ?, ?, ?, ?)";
PreparedStatement preparedStmt = con.prepareStatement(query);
// binding values
preparedStmt.setInt(1, 0);
preparedStmt.setString(2, code);
preparedStmt.setString(3, name);
preparedStmt.setDouble(4, Double.parseDouble(price));
preparedStmt.setString(5, desc);
// execute the statement
preparedStmt.execute();
con.close();
output = "Inserted successfully";
} catch (Exception e) {
output = "Error while inserting";
System.err.println(e.getMessage());
}
return output;
}

public String deleteItem(String itemID)
{
String output = "";
try
{
Connection con = connect();
if (con == null)
{
return "Error while connecting to the database for deleting.";
}
// create a prepared statement
String query = "delete from items where itemID=?";
PreparedStatement preparedStmt = con.prepareStatement(query);
// binding values
preparedStmt.setInt(1, Integer.parseInt(itemID));

// execute the statement
preparedStmt.execute();
con.close();
output = "Deleted successfully";
}
catch (Exception e)
{
output = "Error while deleting the item.";
System.err.println(e.getMessage());
}
return output;
}

public String readItems()
{
String output = "";
try
{
Connection con = connect();
if (con == null)
{
return "Error while connecting to the database for reading.";
}
// Prepare the html table to be displayed
output = "<table border='1' table class=\"table table-dark table-hover\"><tr><th>Item Code</th>"
+"<th>Item Name</th><th>Item Price</th>"
+ "<th>Item Description</th>"
+ "<th>Update</th><th>Remove</th></tr>";
String query = "select * from items";
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);
// iterate through the rows in the result set
while (rs.next())
{
String itemID = Integer.toString(rs.getInt("itemID"));
String itemCode = rs.getString("itemCode");
String itemName = rs.getString("itemName");
String itemPrice = Double.toString(rs.getDouble("itemPrice"));
String itemDesc = rs.getString("itemDesc");
// Add a row into the html table
output += "<tr><td>" + itemCode + "</td>";
output += "<td>" + itemName + "</td>";
output += "<td>" + itemPrice + "</td>";
output += "<td>" + itemDesc + "</td>";
// buttons
output += "<td><input name='btnUpdate' "
+ "<input name='btnUpdate' type='submit' value='Update' class='btn btn-primary'> </td>"
+ "<td><form method='post' action='items.jsp'>"
+ "<input name='btnRemove' type='submit' value='Remove' class='btn btn-danger'>"
+ "<input name='itemID' type='hidden' "
+ " value='" + itemID + "'>" + "</form></td></tr>";
}
con.close();
// Complete the html table
output += "</table>";
}
catch (Exception e)
{
output = "Error while reading the items.";
System.err.println(e.getMessage());
}
return output;
}
}


0 comments on commit 734ba8b

Please sign in to comment.