Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Subhikshaa23 committed Nov 21, 2023
0 parents commit 0d18a3a
Show file tree
Hide file tree
Showing 14 changed files with 586 additions and 0 deletions.
Empty file added Schema
Empty file.
Binary file added WEB-INF/classes/DbCon.class
Binary file not shown.
129 changes: 129 additions & 0 deletions WEB-INF/classes/DbCon.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
// package dbconnection;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ResourceBundle;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.annotation.WebServlet;

//jdbc packages
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.PreparedStatement;

@WebServlet("/DbCon")

public class DbCon extends HttpServlet {

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
{
//db connection
final String URL = "jdbc:mysql://localhost:3306/";
final String DB = "stockdb";
final String DRIVER = "com.mysql.cj.jdbc.Driver";
final String USER = "root";
final String PASSWORD = "Subhi#23";

response.setContentType("text/html");
PrintWriter out = response.getWriter();

String username = request.getParameter("username");
String password = request.getParameter("password");

Connection conn = null;
ResultSet rs = null;
Statement stmt = null;
try {
Class.forName(DRIVER);
conn = DriverManager.getConnection(URL+DB, USER, PASSWORD);
stmt = conn.createStatement();

String sql = "SELECT * FROM login WHERE Username='" + username + "' AND Password='" + password + "'";
rs = stmt.executeQuery(sql);

if (rs.next()) {
sql = "SELECT * FROM stock";
PreparedStatement pstmt = conn.prepareStatement(sql);
ResultSet rs1 = pstmt.executeQuery();

out.println("<link rel=\"stylesheet\" href=\"style.css\">");
out.println("<style>");
out.println("td, th{padding: 10px 15px;}");
out.println("table{margin: auto auto;}");
out.println("body{text-align: center; padding: auto 50px}");
out.println(".options{display: inline-block; width: 30%; float: right;}");
out.println(".container{ width: 50%; margin: 50px auto;}");
out.println("</style>");

out.println("<div class=\"container\">");
out.println("<h1 style=\"text-align:center\";>AVAILABLE STOCK</h1>");
out.println("</div>");

out.print("<div class=\"options\">");
out.println("<form method=\"post\" id=\"add\" action=\"insert.html\">");
out.println("<input type=\"submit\" class=\"btns\" value=\"ADD \">");
out.println("</form>");

out.println("<form method=\"post\" id=\"edit\"action=\"edit.html\">");
out.println("<input type=\"submit\" class=\"btns\" value=\"EDIT \">");
out.println("</form>");

out.println("<form method=\"post\" id=\"delete\" action=\"Delete\">");
out.println("<input type=\"text\" name=\"deleteVal\">");
out.println("<input type=\"submit\" class=\"btns\" value=\"DELETE\">");
out.println("<br>");
out.println("</form>");
out.print("</div>");



out.print("<div class=\"table-container\">");
out.println("<table border=\"1\">");
out.println("<tr>");
out.println("<th>PRODUCT ID</th>");
out.println("<th>PRODUCT NAME</th>");
out.println("<th>QUANTITY</th>");
out.println("<th>PRICE</th>");
out.println("</tr>");

while (rs1.next()) {
out.println("<tr>");

out.println("<td>" + rs1.getInt("Product_Id") + "</td>");
out.println("<td>" + rs1.getString("Product_Name") + "</td>");
out.println("<td>" + rs1.getString("Product_Quantity") + "</td>");
out.println("<td>" + rs1.getString("Product_Price") + "</td>");

out.println("</tr>");
}

out.print("</table>");
out.print("</div>");
}

else {
response.sendRedirect("index.html");
}
}

catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}

finally {
try {
if (rs != null) rs.close();
if (stmt != null) stmt.close();
if (conn != null) conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
Binary file added WEB-INF/classes/Delete.class
Binary file not shown.
78 changes: 78 additions & 0 deletions WEB-INF/classes/Delete.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// package dbconnection;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ResourceBundle;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.annotation.WebServlet;

//jdbc packages
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.PreparedStatement;

@WebServlet("/Delete")

public class Delete extends HttpServlet {

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
{
//db connection
final String URL = "jdbc:mysql://localhost:3306/";
final String DB = "stockdb";
final String DRIVER = "com.mysql.cj.jdbc.Driver";
final String USER = "root";
final String PASSWORD = "Subhi#23";

response.setContentType("text/html");
PrintWriter out = response.getWriter();

Integer recordIdToDelete = Integer.parseInt(request.getParameter("deleteVal"));

Connection conn = null;

try {
Class.forName(DRIVER);
conn = DriverManager.getConnection(URL+DB, USER, PASSWORD);

String sql = "DELETE FROM stock WHERE Product_Id = ?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, recordIdToDelete);

int rowsAffected = pstmt.executeUpdate();

if (rowsAffected > 0)
{
out.println("<link rel=\"stylesheet\" href=\"style.css\">");
out.print("Record has been deleted successfully");

} else {
out.print("Enter valid ID");

}

}
catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
out.print(e);
}

finally {
try {
// if (rs != null) rs.close();
// if (pstmt != null) pstmt.close();
if (conn != null) conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}


}
}
Binary file added WEB-INF/classes/Edit.class
Binary file not shown.
86 changes: 86 additions & 0 deletions WEB-INF/classes/Edit.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// package dbconnection;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ResourceBundle;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.annotation.WebServlet;

//jdbc packages
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.PreparedStatement;

@WebServlet("/Edit")

public class Edit extends HttpServlet {

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
{
//db connection
final String URL = "jdbc:mysql://localhost:3306/";
final String DB = "stockdb";
final String DRIVER = "com.mysql.cj.jdbc.Driver";
final String USER = "root";
final String PASSWORD = "Subhi#23";

response.setContentType("text/html");
PrintWriter out = response.getWriter();

String pname = request.getParameter("pname");
String qty = request.getParameter("qty");
String price = request.getParameter("price");
String recordToEdit = request.getParameter("editVal");


Connection conn = null;

try {
// out.print("Success5");
Class.forName(DRIVER);
conn = DriverManager.getConnection(URL+DB, USER, PASSWORD);

String sql = "UPDATE stock\r\n" + //
"SET Product_Name = ?, Product_Quantity = ?, Product_Price = ?\r\n" + //
"WHERE Product_Id = ?;";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, pname);
pstmt.setString(2, qty);
pstmt.setString(3, price);
pstmt.setString(4, recordToEdit);


int rowsAffected = pstmt.executeUpdate();

if(rowsAffected > 0)
{
response.sendRedirect("edit.html#successMessage1");

} else {
response.sendRedirect("edit.html");
}

}

catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
out.print(e);
}

finally {
try {
// if (rs != null) rs.close();
// if (stmt != null) stmt.close();
if (conn != null) conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
Binary file added WEB-INF/classes/Insert.class
Binary file not shown.
81 changes: 81 additions & 0 deletions WEB-INF/classes/Insert.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// package dbconnection;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ResourceBundle;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.annotation.WebServlet;

//jdbc packages
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.PreparedStatement;

@WebServlet("/Insert")

public class Insert extends HttpServlet {

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
{
//db connection
final String URL = "jdbc:mysql://localhost:3306/";
final String DB = "stockdb";
final String DRIVER = "com.mysql.cj.jdbc.Driver";
final String USER = "root";
final String PASSWORD = "Subhi#23";

response.setContentType("text/html");
PrintWriter out = response.getWriter();

String pname = request.getParameter("pname");
String qty = request.getParameter("qty");
String price = request.getParameter("price");


Connection conn = null;

try {
Class.forName(DRIVER);
conn = DriverManager.getConnection(URL+DB, USER, PASSWORD);


String sql = "INSERT INTO stock (Product_Name, Product_Quantity, Product_Price) VALUES (?, ?, ?)";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, pname);
pstmt.setString(2, qty);
pstmt.setString(3, price);


int rowsAffected = pstmt.executeUpdate();

if (rowsAffected > 0)
{
response.sendRedirect("insert.html#successMessage");

} else {
response.sendRedirect("insert.html");
}

}

catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}

finally {
try {
// if (rs != null) rs.close();
// if (stmt != null) stmt.close();
if (conn != null) conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
Loading

0 comments on commit 0d18a3a

Please sign in to comment.