Java简易健康系统-第二部

 大家好,窝又来了,今天我们接着上回的继续完善我们的后续,来巩固我们的知识

目录

LOginService层

LoginServiceImpl

LoginDao

LoginImpl

2.健康信息管理功能

后端HomeServlet


LOginService层

package cn.hp.Service;

import cn.hp.model.Admin;

public interface LoginService {
     boolean login(String name, String password);
    
}

LoginServiceImpl

package cn.hp.Service.impl;

import cn.hp.Dao.LoginDao;
import cn.hp.Dao.impl.LoginDaoImpl;
import cn.hp.Service.LoginService;
import cn.hp.model.Admin;

/**
 * @author 法外狂徒
 * @year 2022年02月22日13:00
 */
public class LoginServiceImpl implements LoginService {
    @Override
    public boolean login(String name, String password) {
            LoginDao loginDao = new LoginDaoImpl ();
        int  count = loginDao.findByNameAndPass ( name, password );
        if (count > 0 ){
            return true ;
            }else {
                return false;
            }
    }
  

LoginDao

package cn.hp.Dao;

import cn.hp.model.Admin;

/**
 * @author 法外狂徒
 * @year 2022年02月22日13:01
 */
public interface LoginDao {
    int  findByNameAndPass(String name, String password);
   
}

LoginImpl

package cn.hp.Dao.impl;

import cn.hp.Dao.LoginDao;
import cn.hp.model.Admin;

import java.sql.*;

/**
 * @author 法外狂徒
 * @year 2022年02月22日13:07
 */
public class LoginDaoImpl implements LoginDao {
    @Override
    public int findByNameAndPass(String name, String password) {
        //jdbc操作 druid链接池
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        int count = 0;

        try {
            Class.forName ( "com.mysql.jdbc.Driver" );
            connection = DriverManager.getConnection ( "jdbc:mysql://localhost:3306/jiankang?characterEncoding" +
                    "=utf8", "root", "root" );
            String sql = "select * from admin where aname = ? and apwd = ? ";
            preparedStatement = connection.prepareStatement ( sql );
            preparedStatement.setString ( 1, name );
            preparedStatement.setString ( 2, password );
            resultSet = preparedStatement.executeQuery ( );
            while (resultSet.next ( )) {
                count++;
            }
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace ( );
        } finally {
            try {
                if (resultSet != null) {
                    resultSet.close ( );
                }
                if (preparedStatement != null) {
                    preparedStatement.close ( );
                }
                if (connection != null) {
                    connection.close ( );
                }
            } catch (SQLException e) {
                e.printStackTrace ( );
            }
        }
        return count;
    }
}

2.健康信息管理功能

涉及页面一个 ,标题一个 ,一个表格(固定:2行 ,不固定行数未知 7列)

 代码编写: 前端页面 home.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<style>

</style>
<head>
    <title>健康信息列表</title>
    <link rel="stylesheet" href="css/Home.css">
</head>
<body>
<div><h1>健康信息列表</h1></div>
<div>
    <table border="1px" cellspacing="0px">
        <tr>
            <td colspan="7">
                <a href="add.jsp"> 新增</a>
            </td>
        </tr>
        <tr>
            <th>姓名</th>
            <th>身份证</th>
            <th>体温</th>
            <th>身体状况</th>
            <th>预报时间</th>
            <th>填报人</th>
            <th> 操作</th>
        </tr>
        <%--   对后端传递的集合 lis进行遍历--%>
        <c:forEach items="${list}" var="info">
            <tr align="center">
                <td>${info.uname}</td>
                <td>${info.ipcard}</td>
                <td>${info.temp}</td>
                <td>${info.condition}</td>
                <td>${info.ctime}</td>
                <td>${info.aid} </td>
                <td>
                    <a href="/delete?hid=${info.hid}">删除</a>
                    <a href="/update?hid=${info.hid}">修改</a>
                </td>
            </tr>
        </c:forEach>

    </table>
</div>
</body>
</html>

后端HomeServlet

package cn.hp.Servlet;

import cn.hp.Service.HealthInfoService;
import cn.hp.Service.impl.HealthInfoServiceImpl;


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 java.io.IOException;
import java.util.List;

@WebServlet (urlPatterns ="/home")
public class HomeServlet extends HttpServlet {
    //查询全部健康信息
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置编码格式 utf-8
        req.setCharacterEncoding ("utf-8");
        //调用service层 查询全部内容
        HealthInfoService healthInfoService = new HealthInfoServiceImpl ();
        List list =healthInfoService.findAll();
        //将全部信息集合放到域中 请求域中
        req.setAttribute ( "list",list );
        //请求转发到主页
        req.getRequestDispatcher ( "/home.jsp" ).forward ( req,resp );
    }
}

HealthInfoService

package cn.hp.Service;

import cn.hp.Dao.HealthInfoDao;
import cn.hp.model.HealthInfo;

import java.util.List;

public interface HealthInfoService {
    List findAll();
  }

HealthInfoServiceImpl

package cn.hp.Service.impl;

import cn.hp.model.HealthInfo;
import cn.hp.Dao.HealthInfoDao;
import cn.hp.Dao.impl.HealthInfoDaoImpl;
import cn.hp.Service.HealthInfoService;


import java.util.List;



public class HealthInfoServiceImpl implements HealthInfoService {
   HealthInfoDao healthInfoDao =new HealthInfoDaoImpl ();
    @Override
    public List findAll() {
        //查询全部健康信息
        List list = healthInfoDao.findAll ( );
        return list;
    }
}

 HealthInfoDao 

package cn.hp.dao;

import java.util.List;

public interface HealthInfoDao {  

List findAll();

}

HealthInfoDaoImpl

package cn.hp.Dao.impl;

import cn.hp.Service.HealthInfoService;
import cn.hp.model.HealthInfo;
import cn.hp.Dao.HealthInfoDao;


import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class HealthInfoDaoImpl implements HealthInfoDao {
    @Override
    public List findAll() {
        //jdbc操作 druid连接池
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        List list = new ArrayList ( );
        try {
            Class.forName ( "com.mysql.jdbc.Driver" );
            connection = DriverManager.getConnection ( "jdbc:mysql://localhost:3306/health?characterEncoding" +
                    "=utf8", "root", "root" );
            String sql = "select * from healthinfo;  ";
            preparedStatement = connection.prepareStatement ( sql );
            resultSet = preparedStatement.executeQuery ( );
            while (resultSet.next ( )) {
                HealthInfo healthInfo = new HealthInfo ( );
                healthInfo.setAid ( resultSet.getInt ( "aid" ) );
                healthInfo.setHid ( resultSet.getInt ( "hid" ) );
                healthInfo.setUname ( resultSet.getString ( "uname" ) );
                healthInfo.setCondition ( resultSet.getString ( "condition" ) );
                healthInfo.setCtime ( resultSet.getString ( "ctime" ) );
                healthInfo.setIpcard ( resultSet.getString ( "ipcard" ) );
                healthInfo.setTemp ( resultSet.getString ( "temp" ) );
                list.add ( healthInfo );
            }
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace ( );
        } finally {
            try {
                if (resultSet != null) {
                    resultSet.close ( );
                }
                if (preparedStatement != null) {
                    preparedStatement.close ( );
                }
                if (connection != null) {
                    connection.close ( );
                }
            } catch (SQLException e) {
                e.printStackTrace ( );
            }
        }
        return list;
    }

  
}

}

HealthInfo

package cn.hp.model;

public class HealthInfo {

    private int hid ;
    private String uname ;
    private String ipcard ;
    private String temp ;
    private String condition ;
    private String ctime ;
    private int aid ;

    public HealthInfo(int hid, String uname, String ipcard, String temp, String condition, String ctime, int aid) {
        this.hid = hid;
        this.uname = uname;
        this.ipcard = ipcard;
        this.temp = temp;
        this.condition = condition;
        this.ctime = ctime;
        this.aid = aid;
    }

    public  HealthInfo(){
        //无参构造方法
    }

今天的进度差不多就先到此结束啦,喜欢的可以留下宝贵的评论,掰掰咯

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值