Skip to content
Permalink
Browse files
almost done
  • Loading branch information
attikoo committed Aug 19, 2022
1 parent 011de03 commit b2c44dc8fd3359a00580774d466a3b4e2ce7a6b0
Show file tree
Hide file tree
Showing 50 changed files with 983 additions and 85 deletions.
@@ -1,75 +1,23 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/JSP_Servlet/Servlet.java to edit this template
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package com.ecommerce.connection;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

public class DbConnection {


public static Connection connection;

public static Connection getConnection(){

try{

Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://localhost:3368/ecommerce", "root","root");

}catch(Exception e){
e.printStackTrace();

}
return (connection);
}

public static void CloseConnection(){
if(connection !=null){
try{
connection.close();
connection =null;
}catch(SQLException ex){
ex.printStackTrace();
}
}
}
private static Connection connection = null;

public static ResultSet getResultFromSqlQuery(String SqlQueryString){

ResultSet rs = null;
try{
if (connection == null){
getConnection();
}

rs = connection.createStatement().executeQuery(SqlQueryString);

}catch(Exception ex){
ex.printStackTrace();
public static Connection getConnection() throws ClassNotFoundException, SQLException{
if(connection ==null){
Class.forName("com.mysql.cj.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/ecommerce","root","rootroot");
System.out.print("connected");

}
return rs;
}

public static int insertUpdateFromSqlQuery(String SqlQueryString){
int i = 2;
try{
if(connection ==null){
getConnection();
}

i = connection.createStatement().executeUpdate(SqlQueryString);
}catch(Exception ex){
ex.printStackTrace();

}
return i;


return connection;
}
}
}
@@ -0,0 +1,107 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package com.ecommerce.dao;

import com.ecommerce.loginpack.model.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.*;


/**
*
* @author oladimeji
*/
public class ProductDao {
private Connection con;
private String query;
private PreparedStatement pst;
private ResultSet rs;

public ProductDao(Connection con) {
this.con = con;
}

public List<Product> getAllProducts(){
List<Product> products = new ArrayList<Product>();
try {
query = "select * from products";
pst = this.con.prepareStatement(query);
rs = pst.executeQuery();
while(rs.next()){
Product row = new Product();
row.setProductid(rs.getInt("productid"));
row.setName(rs.getString("name"));
row.setPrice(rs.getDouble("price"));
row.setCategory(rs.getString("category"));
row.setImage(rs.getString("image"));

products.add(row);
}


}catch(Exception e){
e.printStackTrace();
}

return products;

}

public List<Cart> getCartProducts(ArrayList<Cart> cartList){
List<Cart> products = new ArrayList<Cart>();

try{
if(cartList.size()>0){
for(Cart item:cartList){
query = "select * from products where productid=?";
pst = this.con.prepareStatement(query);
pst.setInt(1,item.getProductid());
rs = pst.executeQuery();
while(rs.next()){
Cart row = new Cart();
row.setProductid(rs.getInt("productid"));
row.setName(rs.getString("name"));
row.setCategory(rs.getString("category"));
row.setPrice(rs.getDouble("price")*item.getQuantity());
row.setQuantity(item.getQuantity());
products.add(row);
}
}
}
}
catch(Exception e){
System.out.print(e.getMessage());
}
return products;
}
//creating an array list for all the products
public double getTotalCartPrice(ArrayList<Cart> cartList){
double sum =0;

try{
if(cartList.size()>0){
for(Cart item:cartList){
query = "select price from products where productid=?";
pst = this.con.prepareStatement(query);
pst.setInt(1, item.getProductid());
rs = pst.executeQuery();
//adding the total price
while(rs.next()){
sum+=rs.getDouble("price")*item.getQuantity();
}

}
}

}
catch(Exception e){
e.printStackTrace();
}
return sum;

}
}
@@ -0,0 +1,51 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package com.ecommerce.dao;

import com.ecommerce.loginpack.model.UserModelClass;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

/**
*
* @author oladimeji
*/
public class UserDao {
private Connection con;
private String query;
private PreparedStatement pst;
private ResultSet rs;

public UserDao(Connection con){
this.con = con;

}

public UserModelClass userLogin(String username, String password){
UserModelClass user = null;
try{
query = "select * from users where username=? and password=?";
pst = this.con.prepareStatement(query);
pst.setString(1, username);
pst.setString(2, password);
rs = pst.executeQuery();

if(rs.next()){
user = new UserModelClass();
user.setId(rs.getInt("userid"));
user.setUsername(rs.getString("username"));

}
}catch(Exception e){
e.printStackTrace();
System.out.print(e.getMessage());

}
return user;

}

}
@@ -0,0 +1,75 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/JSP_Servlet/Servlet.java to edit this template
*/
package com.ecommerce.login;

import com.ecommerce.loginpack.model.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
*
* @author oladimeji
*/
@WebServlet(name = "AddCartServlet", urlPatterns = {"/add-to-cart"})
public class AddCartServlet extends HttpServlet {

/**
*
* @param request
* @param response
* @throws ServletException
* @throws IOException
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");

try ( PrintWriter out = response.getWriter()) {
ArrayList<Cart> cartList = new ArrayList<>();

int id = Integer.parseInt(request.getParameter("id"));
Cart cm = new Cart();
cm.setProductid(id);
cm.setQuantity(1);

HttpSession session = request.getSession();
ArrayList<Cart> cart_list = (ArrayList<Cart>) session.getAttribute("cart-list");
//session created and product added to the list
if (cart_list == null) {
cartList.add(cm);
session.setAttribute("cart-list", cartList);
response.sendRedirect("index.jsp");

} else {
cartList = cart_list;
boolean exist = false;
// if the product is in the cart exist,display already exist message
for (Cart c : cart_list) {
if (c.getProductid() == id) {
exist = true;
out.println("<h3 style='color:crimson; text-align:center'>Item already exist in Cart.<a href='cart.jsp'>Go to Cart</a></h3>");

}

}
// product being added to the cart list
if (!exist) {
cartList.add(cm);
response.sendRedirect("index.jsp");
}
}
}

}

}
@@ -0,0 +1,67 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/JSP_Servlet/Servlet.java to edit this template
*/
package com.ecommerce.login;

import com.ecommerce.connection.DbConnection;
import com.ecommerce.dao.UserDao;
import com.ecommerce.loginpack.model.UserModelClass;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
*
* @author oladimeji
*/
@WebServlet(name = "LoginServlet", urlPatterns = {"/user-login"})
public class LoginServlet extends HttpServlet {


@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

response.sendRedirect("login.jsp");

}


@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try(PrintWriter out = response.getWriter()){
String username = request.getParameter("lusername");
String password = request.getParameter("lpassword");

try {
UserDao udao = new UserDao(DbConnection.getConnection());
UserModelClass user = udao.userLogin(username, password);

if(user !=null){
request.getSession().setAttribute("'auth", user);
response.sendRedirect("index.jsp");
}else{
out.print("user login failed");

}
} catch (ClassNotFoundException | SQLException ex) {
Logger.getLogger(LoginServlet.class.getName()).log(Level.SEVERE, null, ex);
}



}

}

}

0 comments on commit b2c44dc

Please sign in to comment.