如何从JSP中检索数据和显示?

时间:2017-01-27 17:33:32

标签: jsp

我正在尝试检索数据,然后将其显示在表中,但即使dao正常工作,我也会得到空表。我不知道问题出在哪里。这就是我的代码中的一切。请帮助我,因为我必须明天将这些代码提交给我的医生

ManagerSearch.jsp:

 <%@page contentType="text/html" pageEncoding="UTF-8"%>

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>All Data Search</title>
    </head>
    <body>
        <div style="background-image: url(derma1.jpg); height: 900px; width: 1905px;
             font-size: 1em;  color: #1572c1; font-family: Verdana, Arial, Sans-Serif;
             text-align: center; ">
            <table border="1" cellpadding="10">
                <tr>
                    <th>ID</th>
                    <th>Name</th>
                    <th>Birth Date</th>
                    <th>Place Of Birth</th>
                    <th>Gender</th>
                    <th>Nationality</th>
                    <th>Current Address</th>
                    <th>City Address</th>
                    <th>Marital Status</th>
                    <th>Offspring</th>
                    <th>Age Of Youngest Offspring</th>
                    <th>Work Nature</th>
                    <th>Phone Number</th>
                    <th>E-Mail</th>
                </tr>
                 <c:forEach items="${AllDataSearch}" var="p">
                    <tr>

                        <td>${p.iD}</td>
                        <td>${p.patientName}</td>
                        <td>${p.birthDate}</td>
                        <td>${p.placeOfBirth}</td>
                        <td>${p.gender}</td>
                        <td>${p.nationality}</td>
                        <td>${p.currentAddress}</td>
                        <td>${p.citytAdress}</td>
                        <td>${p.maritalStatus}</td>
                        <td>${p.offspring}</td>
                        <td>${p.ageOfYoungestOffspring}</td>
                        <td>${p.workNature}</td>
                        <td>${p.phoneNumber}</td>
                        <td>${p.eMail}</td>
                    </tr>
                </c:forEach>
            </table>
        </div>
    </body>
</html>  

HTML:

<!DOCTYPE html>
<html>
    <head>
        <title>Dermatology Clinic</title>
        <link rel='stylesheet' type='text/css' href='deleteForm.css'/>
    </head>
    <body>
<div id="container">
      <div class="header">
  <img src="logo1.png" alt="logo" />
</div>
             </div>
<div id="container1">
&nbsp;
&nbsp;
&nbsp;
&nbsp;
&nbsp;
&nbsp;
&nbsp;
&nbsp;
&nbsp;
&nbsp;
&nbsp;
&nbsp;
&nbsp;
&nbsp;
&nbsp;
&nbsp;
&nbsp;
&nbsp;
&nbsp;
&nbsp;
&nbsp;
&nbsp;
&nbsp;
&nbsp;
&nbsp;
&nbsp;
&nbsp;
&nbsp;
&nbsp;
&nbsp;
&nbsp;
&nbsp;
&nbsp;
&nbsp;
&nbsp;
&nbsp;
&nbsp;
&nbsp;
<a href="AddNew.html">
   <div class="dropdown">
        <button class="dropbtn">Add a New Patient</button>
    </div>
  </a>
<a href="Delete.html">
<div class="dropdown">
         <button class="dropbtn">Delete a Patient</button>
        </div>
     </a>

<a href="Update.html">
  <div class="dropdown">
        <button class="dropbtn">Update a Patient</button>
        </div>
                  </a>

<a href="Search.html">
  <div class="dropdown">
        <button class="dropbtn">Search For a Patient</button>
        </div>
  </a>
        </div>  
        <div style="background-image: url(derma.jpg); height:714px; width: 1905px;">
            <form action="JSP/ManagerSearch.jsp">
                <p></p>
                <label>
                    ID
                    <input type="text" placeholder="Enter ID" name="ID">
                </label>
                <p></p>
                <input type="submit" value="Search">
            </form>
</div>

      </body>

</html>

DAO:

package DAO;

import Connection.MysqlConnection;
import Tables.PersonalData;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

public class PersonalDataDao {

    public int InsertPersonalData(PersonalData personaldata) throws SQLException, ClassNotFoundException {
        int affected = 0;
        try {


            PreparedStatement ps = MysqlConnection.getPreparedStatement("insert into personal_data values(?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
            ps.setString(1, personaldata.getiD());
            ps.setString(2, personaldata.getPatientName());
            ps.setString(3, personaldata.getBirthDate());
            ps.setString(4, personaldata.getPlaceOfBirth());
            ps.setString(5, personaldata.getGender());
            ps.setString(6, personaldata.getNationality());
            ps.setString(7, personaldata.getCurrentAddress());
            ps.setString(8, personaldata.getCityAddress());
            ps.setString(9, personaldata.getMaritalStatus());
            ps.setString(10, personaldata.getOffspring());
            ps.setString(11, personaldata.getAgeOfYoungestOffspring());
            ps.setString(12, personaldata.getWorkNature());
            ps.setString(13, personaldata.getPhoneNumber());
            ps.setString(14, personaldata.geteMail());
            ps.executeUpdate();
        } catch (ClassNotFoundException | SQLException ex) {
            Logger.getLogger(PersonalDataDao.class.getName()).log(Level.SEVERE, ex.getMessage());
            throw ex;
        }

        return affected;

    }



    public void delete(String id) {
        try {
            String sql = "DELETE FROM personal_data WHERE iD = ?";
            PreparedStatement ps = MysqlConnection.getPreparedStatement(sql);
            ps.setString(1, id);
            ps.executeUpdate();
        } catch (ClassNotFoundException | SQLException ex) {
            Logger.getLogger(PersonalDataDao.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static List<PersonalData> getById(String id) {
        List<PersonalData> ls = new LinkedList<PersonalData>();
        String sql = "select * from personal_data where id = " + id;
        try {
            ResultSet rs = MysqlConnection.getPreparedStatement(sql).executeQuery();
            while (rs.next()) {
                PersonalData n = new PersonalData(rs.getString(1), rs.getString(2), rs.getString(3), rs.getString(4), rs.getString(5), rs.getString(6), rs.getString(7), rs.getString(8), rs.getString(9), rs.getString(10), rs.getString(11), rs.getString(12), rs.getString(13), rs.getString(14));
                ls.add(n);
            }
        } catch (ClassNotFoundException | SQLException ex) {
            Logger.getLogger(PersonalDataDao.class.getName()).log(Level.SEVERE, null, ex);
        }

        return ls;
    }

    public int edit(String iD, String patientName, String birthDate, String placeOfBirth, String gender, String nationality, String currentAddress, String cityAddress, String maritalStatus, String offspring, String ageOfYoungestOffspring, String workNature, String phoneNumber, String eMail) throws SQLException, ClassNotFoundException {
        int affected = 0;

        try {
            String sql = "update personal_data SET Patient_Name = ?, Birth_Date = ?, Place_Of_Birth = ?, Gender = ?, Nationality = ?, Current_Address = ?, City_Address = ?, Marital_Status = ?, Offspring = ?, Age_Of_Youngest_Offspring = ?, Work_Nature = ?, Phone_Number = ?, E_mail = ?" + " where ID = ?";
            PreparedStatement ps = MysqlConnection.getPreparedStatement(sql);
            ps.setString(1, patientName);
            ps.setString(2, birthDate);
            ps.setString(3, placeOfBirth);
            ps.setString(4, gender);
            ps.setString(5, nationality);
            ps.setString(6, currentAddress);
            ps.setString(7, cityAddress);
            ps.setString(8, maritalStatus);
            ps.setString(9, offspring);
            ps.setString(10, ageOfYoungestOffspring);
            ps.setString(11, workNature);
            ps.setString(12, phoneNumber);
            ps.setString(13, eMail);
            ps.setString(14, iD);

            ps.executeUpdate();
        } catch (ClassNotFoundException | SQLException ex) {
            Logger.getLogger(PersonalDataDao.class.getName()).log(Level.SEVERE, null, ex);
        }
       return affected;

    }

    public static List<PersonalData> getAll() {

        List<PersonalData> ls = new LinkedList<>();

        try {
            ResultSet rs = MysqlConnection.getPreparedStatement("select * from personal_data").executeQuery();
            while (rs.next()) {
                PersonalData n = new PersonalData(rs.getString(1), rs.getString(2), rs.getString(3), rs.getString(4), rs.getString(5), rs.getString(6), rs.getString(7), rs.getString(8), rs.getString(9), rs.getString(10), rs.getString(11), rs.getString(12), rs.getString(13), rs.getString(14));
                ls.add(n);
            }
        } catch (ClassNotFoundException | SQLException ex) {
            Logger.getLogger(PersonalDataDao.class.getName()).log(Level.SEVERE, null, ex);

        }
        return ls;
    }
}

Search.java:

package servlet;

import DAO.PersonalDataDao;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class Search extends HttpServlet {


    protected void processRequest(HttpServletRequest request, HttpServletResponse response) {
        String id = request.getParameter("ID");
        request.setAttribute("getById", DAO.PersonalDataDao.getById(id));
        RequestDispatcher rd = request.getRequestDispatcher("AllData.jsp");
        try {
            rd.forward(request, response);
        } catch (ServletException | IOException ex) {
            Logger.getLogger(Update.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

0 个答案:

没有答案