samedi 17 avril 2021

Insertion of data to database mysql not working in jsp?

What i am trying to accomplish is to take form data and insert into the database (localhost) XAMPP, However the code isnt behaving the way i want it to , and i cant find any error or mistake .

The jsp Code:

<form method="POST" action="<%=request.getContextPath()%>/InsertPatientSickness/Save">
              
                             <c:choose>
                              <c:when test="${empty list}">
                          <h1 style="margin-top: 25px; text-align: center">No Patient records were found</h1>
                              </c:when>
                          <c:otherwise>
                        <table class="styled-table" >
                            <thead>
                                <tr>
                                    <th>PatientID</th>
                                    <th>Sickness Name</th>  
                                    <th>Symptoms </th>
                                    <th>Prescriptions</th>
                                    <th>Special Request</th>
                                </tr>
                            </thead>
                            <c:forEach items="${list}" var="record">
                            <tbody>  
                                <tr class="active-row">
                                    <td><input class="form-control" data-val="true"  id="PatientId"  maxlength="200" name="patientid" type="text" value="${record.patient_Id}"></td>
                                    <td><input class="form-control" data-val="true"  id="SicknessName" maxlength="200" name="SicknessName" type="text" value=""></td>
                                    <td><textarea class="form-control" cols="15" id=""  maxlength="1000" name="Symptoms" rows="2"></textarea></td>            
                                    <td><textarea class="form-control" cols="15"  id="Prescription" maxlength="1000" name="Prescription" rows="2"></textarea></td>
                                    <td><textarea class="form-control" cols="15" id="SpecialRequest" maxlength="1000" name="SpecialRequest" rows="2"></textarea></td>
                                </tr>
                            </tbody>  
                            </c:forEach>
                        </c:otherwise>
                          </c:choose>
                        </table>
                        <div style="text-align: center;">
                            <button  class="btn-save" ><i class="fas fa-save ir"></i>Save</button>
                            <input type="reset" class="btn-Reset" >
                            <a style="" href="<%=request.getContextPath()%>/InsertPatientSickness/Cancel"><button class="btn-cancel"><i class="fa fa-ban ir"></i>Cancel</button>  </a>
                        </div>
                    </form>
                </div>

The servlet:

     @Override
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
          
       if (request.getServletPath().contains("Save")) {
                //Calling doPost to reterive form data
                int patientid =Integer.parseInt(request.getParameter("patientid"));//Taking doctor inserting id
                String sickness=request.getParameter("SicknessName");
                String Symptoms=request.getParameter("Symptoms");
                String Prescriptions=request.getParameter("Prescription");
                String Request=request.getParameter("SpecialRequest");
              
                StaffManager.getInstance().addNewPatientRecord(patientid,sickness,Symptoms,Prescriptions,Request);
                
                /*The updated data send to records*/
                List list = new ArrayList<>();//Creating a list to hold data
                try {
                    list = StaffManager.getInstance().getPatientInfo(patientid);//MVC method implementation for Searching patient
                } catch (SQLException ex) {
                    Logger.getLogger(Dreport.class.getName()).log(Level.SEVERE, null, ex);
                }
                request.setAttribute("list", list);
                request.getRequestDispatcher("/records.jsp").forward(request, response);
            }
}

The staff manager code:

public void addNewPatientRecord(int patientid, String sickness, String Symptoms, String Prescriptions, String Request) {
      dao.AddNewPatientRecord(patientid,sickness,Symptoms,Prescriptions,Request);
    }

The Patient Dao Code:

 public void AddNewPatientRecord(int patientid, String sickness, String Symptoms,
            String Prescriptions, String Request) {
        String Insert;
        Connection conn=null;
        try
        {    
            //Opening database for connection
            conn =DriverManager.getConnection("jdbc:mysql://localhost:3306/hms", "root", "");
          
                Insert="INSERT INTO patient(user_symptoms, user_sickeness, user_specail_request ,user_prescriptions,user_id) VALUES (?,?,?,?) ";
                PreparedStatement pstmt = conn.prepareStatement(Insert);
                pstmt.setString(1, Symptoms);
                pstmt.setString(2,sickness);
                pstmt.setString(3,Request);
                pstmt.setString(4, Prescriptions);
                pstmt.setInt(5, patientid);
                pstmt.executeUpdate();
                pstmt.close();
            
        }
        catch(SQLException e)
        {
     
            e.printStackTrace();
        }
        finally
        {
            try 
            {
                conn.close();
            } 
            catch (SQLException ex) 
            {
               
            }
        }     
    }

I'm still new into java web development , so please excuse for not always following the best practices here . And would really appreciate help to make this work , thank you for all in advance.




Aucun commentaire:

Enregistrer un commentaire