Search Medicine project in Java using JSP, Servlet, JDBC and MYSQL

Online Search medicine project in java with source code and project report. Search medicine projects in java using JSP, Servlet, JDBC, MYSQL, Tomcat, and maven. it is a web application running on a browser and deployed over the tomcat server.

This is a J2ee, Maven project, and following MVC architecture, So According to the MVC. The View part is developed in HTML, CSS, and Bootstrap. The Controller part is developed into Servlet. The model and business logics develop into Java and JDBC.

It’s a web application running deployed over the tomcat server at localhost.

Search Medicine project in Java

Let’s understand the features and functionality of the application.

When the application launch, the user will find the login and registration option on the home page. where an existing user can logIn and a new user can register using the signup option.

There are two types of users ADMIN and USER/CUSTOMER.

After admin login

  • Admin can ADD/Edit/Delete/Update/View Users.
  • Admin can Add new Medicine into the database.
  • Admin will update the quantity of the medicine.
  • Admin can manage the medicine booking history.

After User Login

  • Users can view the list of available medicine.
  • Users can search for the required medicine by the name of the medicine.
  • Users can book the medicine.
  • Users can manage the booking history.

When the user will booking any medicine the existing quantity will be automatic update and displayed as per the booking. If the user will book more than one medicine then the total price will be calculated automatically from the backend as per the number of medicine bookings. The price of each medicine will be generated randomly.

Tools and Technologies Used

Technology/Domain: Java
Front-End: JSP, Html, CSS, JS, Bootstrap.
Server-side: Servlet.
Back-end: MYSQL.
Server: Tomcat 8.5.

Contact to get the Source Code

Skype Id: jcodebun
Email: jcodebun@gmail.com
WhatsApp: +91 8827363777
Price: 1500 INR

Check more details about the prices and services

Search Medicine project Source code in Java

Here, I am sharing a folder structure of the project and source code for the medicine module. As the application is using MVC architecture so we divide each module of the project into 3 parts Model View and Controller.

Folder and Package Structure

Add medicine From(addmedicine.jsp)

<%@page import="com.Medicine.Controller.AddMedicineCtl"%>
<%@page import="com.Medicine.Utility.ServletUtility"%>
<%@page import="com.Medicine.Utility.DataUtility"%>
<%@page import="com.Medicine.Controller.SMView"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Medicine</title>
</head>
<body>

<%@include file="Header.jsp"%>
 
 <div class="container">
  <br>
  <h6 style="color: red;" class="text-center"><%=ServletUtility.getErrorMessage(request)%></h6>
  <h6 style="color: green;" class="text-center"><%=ServletUtility.getSuccessMessage(request)%></h6>
  
  <form action="<%=SMView.MEDICINE_CTL%>" method="post">

   <div class="row">
    <div class="col-2"></div>
    <div class="col-8">


     <jsp:useBean id="bean" scope="request"
      class="com.Medicine.Bean.MedicineBean" />

     <input type="hidden" name="id" value="<%=bean.getId()%>"> <input
      type="hidden" name="createdBy" value="<%=bean.getCreatedby()%>">
     <input type="hidden" name="modifiedBy"
      value="<%=bean.getModifiedby()%>"> <input type="hidden"
      name="createdDatetime" value="<%=bean.getCreatedatetime()%>">
     <input type="hidden" name="modifiedDateTime"
      value="<%=bean.getModifieddatetime()%>">

<%
      if (bean.getId() > 0) {
     %>
     <h1 class="text-center">Update Medicine</h1>
     <%
      } else {
     %>
     <h1 class="text-center">Add Medicine</h1>
     <%
      }
     %>

<hr>
     <div class="mb-3">
      <label class="form-label">Company Name</label> <input type="text"
       class="form-control" name="cName" placeholder="Enter Company here..."
       value="<%=DataUtility.getStringData(bean.getCompanyName())%>">

      <div class="form-text" style="color: red"><%=ServletUtility.getErrorMessage("cName", request)%></div>
     </div>


     <div class="mb-3">
      <label class="form-label">Medicine Name</label> <input type="text"
       class="form-control" name="mName"
       placeholder="Enter Medicine Name here..."
       value="<%=DataUtility.getStringData(bean.getMedicineName())%>">

      <div class="form-text" style="color: red"><%=ServletUtility.getErrorMessage("mName", request)%></div>
     </div>
     <div class="mb-3">
      <label class="form-label">Quantity</label> <input type="text"
       class="form-control" name="quantity"
       placeholder="Enter Quantity here..."
       value="<%=DataUtility.getStringData(bean.getQuantity())%>">

      <div class="form-text" style="color: red"><%=ServletUtility.getErrorMessage("quantity", request)%></div>
     </div>

<div class="container text-center">
     <%
      if (bean.getId() > 0) {
     %>
     <input type="submit" class="btn btn-primary" name="operation"
      value="<%=AddMedicineCtl.OP_UPDATE%>">
     <%
      } else {
     %>
     <input type="submit" class="btn btn-primary" name="operation"
      value="<%=AddMedicineCtl.OP_SAVE%>">
     <%
      }
     %>
</div>
    </div>
    <div class="col-2"></div>

   </div>
  </form>
 </div>
 <br>
 <%@ include file="Footer.jsp"%>
</body>
</html>

Controller Servlet (AddMedicine.java)

package com.Medicine.Controller;

import java.io.IOException;
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 com.Medicine.Bean.BaseBean;
import com.Medicine.Bean.MedicineBean;
import com.Medicine.Bean.UserBean;
import com.Medicine.Model.MedicineModel;
import com.Medicine.Model.UserModel;
import com.Medicine.Utility.DataUtility;
import com.Medicine.Utility.DataValidator;
import com.Medicine.Utility.PropertyReader;
import com.Medicine.Utility.ServletUtility;

/**
 * Servlet implementation class AddMedicineCtl
 */
@WebServlet(name = "AddMedicineCtl", urlPatterns = "/medicine")
public class AddMedicineCtl extends BaseCtl {
 private static final long serialVersionUID = 1L;
 public static final String OP_UPDATE = "Update";
 public static final String OP_SAVE = "Save";         
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AddMedicineCtl() {
        super();
        // TODO Auto-generated constructor stub
    }

    @Override
   	protected boolean validate(HttpServletRequest request) {
   		System.out.println("in validation");
   		boolean pass = true;

   		if (DataValidator.isNull(request.getParameter("cName"))) {
   			request.setAttribute("cName", PropertyReader.getvalue("error.require", "Company Name"));
   			pass = false;

   		} else if (!DataValidator.isName(request.getParameter("cName"))) {
   			request.setAttribute("cName", PropertyReader.getvalue("error.name", "Company Name"));
   			pass = false;
   		}

   		if (DataValidator.isNull(request.getParameter("mName"))) {
   			request.setAttribute("mName", PropertyReader.getvalue("error.require", "Medicine Name"));
   			pass = false;

   		} else if (!DataValidator.isName(request.getParameter("mName"))) {
   			request.setAttribute("mName", PropertyReader.getvalue("error.name", "Medicine Name"));
   			pass = false;
   		}

   		if (DataValidator.isNull(request.getParameter("quantity"))) {
   request.setAttribute("quantity", PropertyReader.getvalue("error.require", "Quantity"));
   pass = false;

  }
   		return pass;
   	}
       
       protected BaseBean populateBean(HttpServletRequest request) {

   		MedicineBean bean = new MedicineBean();

   		bean.setId(DataUtility.getLong(request.getParameter("id")));
   		bean.setCompanyName(DataUtility.getString(request.getParameter("cName")));
   		bean.setMedicineName(DataUtility.getString(request.getParameter("mName")));
   		bean.setQuantity(DataUtility.getLong(request.getParameter("quantity")));
   		populateDTO(bean, request);
   		return bean;

   	}
    
 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  MedicineModel model = new MedicineModel();
  long id = DataUtility.getLong(request.getParameter("id"));
  if (id > 0) {
   MedicineBean bean = null;
   try {
    bean = model.findByPk(id);
   } catch (Exception e) {
    e.printStackTrace();
   }
   ServletUtility.setbean(bean, request);
  }
  ServletUtility.forward(getView(), request, response);
 }

 /**
  * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
  */
 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  MedicineModel model = new MedicineModel();
  System.out.println("in do post");
  String op = DataUtility.getString(request.getParameter("operation"));
  long id = DataUtility.getLong(request.getParameter("id"));
  MedicineBean bean = new MedicineBean();
  bean = (MedicineBean) populateBean(request);

  if (bean.getId() > 0) {
   System.out.println("in do post2");
   long i = model.Update(bean);
   ServletUtility.setSuccessMessage("Data Updated Successfully", request);
  } else {
   try {
    long pk = model.add(bean);
    ServletUtility.setSuccessMessage("Data Add Successfully", request);
   } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }
  ServletUtility.forward(getView(), request, response);
 }

 @Override
 protected String getView() {
  
  return SMView.MEDICINE_VIEW;
 }

}

Medicine Model(MedicineModel.Java)

package com.Medicine.Model;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import com.Medicine.Bean.MedicineBean;
import com.Medicine.Bean.OrderMedicineBean;
import com.Medicine.Bean.UserBean;
import com.Medicine.Exception.ApplicationException;
import com.Medicine.Utility.JDBCDataSource;

public class MedicineModel {

 public Integer nextpk() throws Exception {

  Connection conn = null;
  int pk = 0;
  try {
   conn = JDBCDataSource.getConnection();
   PreparedStatement ps = conn.prepareStatement("SELECT MAX(ID) FROM medicine");
   ResultSet rs = ps.executeQuery();
   while (rs.next()) {
    pk = rs.getInt(1);
   }
  } catch (SQLException e) {
   e.printStackTrace();
  }
  return pk + 1;
 }
 
 public MedicineBean findByPk(long pk) throws Exception {

  MedicineBean bean = null;
  Connection conn = null;
  try {
   conn = JDBCDataSource.getConnection();
  } catch (SQLException e) {
   e.printStackTrace();
  }
  try {
   PreparedStatement ps = conn.prepareStatement("SELECT * FROM medicine WHERE id=?");
   ps.setLong(1, pk);
   ResultSet rs = ps.executeQuery();
   while (rs.next()) {
    bean = new MedicineBean();
    bean.setId(rs.getLong(1));
    bean.setCompanyName(rs.getString(2));
    bean.setMedicineName(rs.getString(3));
    bean.setQuantity(rs.getLong(4));
   }
   rs.close();
  } catch (SQLException e) {
   e.printStackTrace();
  }
  return bean;
 }
 
 /*
  * public OrderMedicineBean OrderfindByPk(long pk) throws Exception {
  * 
  * OrderMedicineBean bean = null; Connection conn = null; try { conn =
  * JDBCDataSource.getConnection(); } catch (SQLException e) {
  * e.printStackTrace(); } try { PreparedStatement ps =
  * conn.prepareStatement("SELECT * FROM medicine WHERE id=?"); ps.setLong(1,
  * pk); ResultSet rs = ps.executeQuery(); while (rs.next()) { bean = new
  * OrderMedicineBean(); bean.setId(rs.getLong(1));
  * bean.setMedicineName(rs.getString(2)); bean.setMedicineid(rs.getLong(3));
  * bean.setQuantity(rs.getLong(4)); bean.setUserid(rs.getLong(5)); } rs.close();
  * } catch (SQLException e) { e.printStackTrace(); } return bean; }
  */
 public long add(MedicineBean bean) throws Exception {
  System.out.println("in add method");
  Connection conn = null;
  int pk = 0;

  try {

   conn = JDBCDataSource.getConnection();
   pk = nextpk();
   conn.setAutoCommit(false);
   PreparedStatement ps = conn.prepareStatement("INSERT INTO medicine VALUES(?,?,?,?)");
   ps.setLong(1, pk);
   ps.setString(2, bean.getCompanyName());
   ps.setString(3, bean.getMedicineName());
   ps.setLong(4, bean.getQuantity());
   ps.executeUpdate();
   conn.commit();
   ps.close();
  } catch (Exception e) {

   try {
    conn.rollback();
   } catch (Exception e2) {
    e.printStackTrace();
    throw new ApplicationException("Exception : add rollback exception " + e.getMessage());
   }
  } finally {
   JDBCDataSource.closeconnection(conn);
  }
  return pk;
 }
 
 public List list() throws Exception {
  System.out.println("in model list");
  ArrayList list = new ArrayList();
  Connection conn = null;
  conn = JDBCDataSource.getConnection();
  PreparedStatement pstmt = conn.prepareStatement("SELECT * from medicine");
  ResultSet rs = pstmt.executeQuery();
  while (rs.next()) {
   MedicineBean bean = new MedicineBean();
   bean.setId(rs.getLong(1));
   bean.setCompanyName(rs.getString(2));
   bean.setMedicineName(rs.getString(3));
   bean.setQuantity(rs.getLong(4));
   list.add(bean);
  }
  return list;
 }
 
 public List Showlist(long userid) throws Exception {
  System.out.println("in model list");
  ArrayList list = new ArrayList();
  try {
   Connection conn = null;
   conn = JDBCDataSource.getConnection();
   PreparedStatement ps = conn.prepareStatement("SELECT * from userorder where userid=?");
   ps.setLong(1, userid);
   ResultSet rs = ps.executeQuery();
   while (rs.next()) {
    OrderMedicineBean bean = new OrderMedicineBean();
    bean.setId(rs.getLong(1));
    bean.setMedicineName(rs.getString(2));
    bean.setMedicineid(rs.getLong(3));
    bean.setQuantity(rs.getLong(4));
    bean.setUserid(rs.getLong(5));
    bean.setTotalamount(rs.getLong(6));
    bean.setPaymentstatus(rs.getString(7));
    list.add(bean);
   }
  } catch (Exception e) {
   e.printStackTrace();
  }
  return list;
 }
 
 public List orderlist() throws Exception {
  System.out.println("in model list");
  ArrayList list = new ArrayList();
  Connection conn = null;
  conn = JDBCDataSource.getConnection();
  PreparedStatement pstmt = conn.prepareStatement("SELECT * from userorder");
  ResultSet rs = pstmt.executeQuery();
  while (rs.next()) {
   MedicineBean bean = new MedicineBean();
   bean.setId(rs.getLong(1));
   bean.setMedicineName(rs.getString(2));
   bean.setMedicineid(rs.getLong(3));
   bean.setQuantity(rs.getLong(4));
   bean.setUserid(rs.getLong(5));
   list.add(bean);
  }
  return list;
 }
 

 public static long delete(long id) {
  int i = 0;
  try {
   Connection conn = JDBCDataSource.getConnection();
   PreparedStatement stmt = conn.prepareStatement("DELETE from medicine where id=?");
   stmt.setLong(1, id);
   i = stmt.executeUpdate();
  } catch (Exception e) {
   e.printStackTrace();
  }
  return i;
 }
 
 public long Update(MedicineBean bean) {

  System.out.println("in model update method");
  int pk = 0;
  try {
   Connection conn = JDBCDataSource.getConnection();
   PreparedStatement ps = conn.prepareStatement(
     "update medicine set company_name=?, medicine_name=?, quantity=? where id=?");
   ps.setString(1, bean.getCompanyName());
   ps.setString(2, bean.getMedicineName());
   ps.setLong(3, bean.getQuantity());
   ps.setLong(4, bean.getId());
    ps.executeUpdate();

  } catch (Exception e) {
   e.printStackTrace();
  }
  return pk;
 }
 
 public long UpdateQuantity(long quantity, long id) {

  System.out.println("in model update method");
  int pk = 0;
  try {
   Connection conn = JDBCDataSource.getConnection();
   PreparedStatement ps = conn.prepareStatement(
     "update medicine set quantity=? where id=?");
  MedicineBean bean =	findByPk(id);
   ps.setLong(1, bean.getQuantity()-quantity);
   ps.setLong(2, id);
    ps.executeUpdate();

  } catch (Exception e) {
   e.printStackTrace();
  }
  return pk;
 }
 
 public static String UpdatePaymentStatus(long id) {

  System.out.println("in model update method");
  String status = null;
  try {
   Connection conn = JDBCDataSource.getConnection();
   PreparedStatement ps = conn.prepareStatement(
     "update userorder set paymentstatus=? where id=?");
  
   ps.setString(1, "paid");
   ps.setLong(2, id);
    ps.executeUpdate();
    status = "Payemnt Status Updated";
  } catch (Exception e) {
   e.printStackTrace();
  }
  return status;
 }
 
 
 public List search(MedicineBean bean) throws Exception {
  StringBuffer sql = new StringBuffer("SELECT * from medicine WHERE 1=1");
  if (bean != null) {
   if (bean.getId() > 0) {
    sql.append(" AND id = " + bean.getId());
   }
   if (bean.getMedicineName() != null && bean.getMedicineName().length() > 0) {
    sql.append(" AND medicine_name like '" + bean.getMedicineName() + "%'");
   }
  }

  ArrayList list = new ArrayList();
  Connection conn = null;
  try {
   conn = JDBCDataSource.getConnection();
   PreparedStatement pstmt = conn.prepareStatement(sql.toString());
   ResultSet rs = pstmt.executeQuery();
   while (rs.next()) {
    bean = new MedicineBean();
    bean.setId(rs.getLong(1));
    bean.setCompanyName(rs.getString(2));
    bean.setMedicineName(rs.getString(3));
    bean.setQuantity(rs.getLong(4));
    list.add(bean);
   }
   rs.close();
  } catch (Exception e) {
e.printStackTrace();
  } finally {
   JDBCDataSource.closeconnection(conn);
  }

  return list;

 }
 
 public Integer ordernextpk() throws Exception {

  Connection conn = null;
  int pk = 0;
  try {
   conn = JDBCDataSource.getConnection();
   PreparedStatement ps = conn.prepareStatement("SELECT MAX(ID) FROM userorder");
   ResultSet rs = ps.executeQuery();
   while (rs.next()) {
    pk = rs.getInt(1);
   }
  } catch (SQLException e) {
   e.printStackTrace();
  }
  return pk + 1;
 }
 
 public long useradd(MedicineBean bean) throws Exception {
  System.out.println("in add method");
  Connection conn = null;
  int pk = 0;
  Random rand = new Random();
  try {
   conn = JDBCDataSource.getConnection();
   pk = ordernextpk();
   conn.setAutoCommit(false);
   PreparedStatement ps = conn.prepareStatement("INSERT INTO userorder VALUES(?,?,?,?,?,?,?)");
   ps.setLong(1, pk);
   ps.setString(2, bean.getMedicineName());
   ps.setLong(3, bean.getMedicineid());
   ps.setLong(4, bean.getQuantity());
   ps.setLong(5, bean.getUserid());
   ps.setLong(6, bean.getQuantity() * rand.nextInt(100));
   ps.setString(7, "unpaid");
   ps.executeUpdate();
   System.out.println("Medicine Id to Update: "+bean.getId());
   UpdateQuantity(bean.getQuantity(), bean.getId());
   conn.commit();
   ps.close();
  } catch (Exception e) {
               e.printStackTrace();
   try {
    conn.rollback();
   } catch (Exception e2) {
    e.printStackTrace();
    throw new ApplicationException("Exception : add rollback exception " + e.getMessage());
   }
  } finally {
   JDBCDataSource.closeconnection(conn);
  }
  return pk;
 }
 
 
}