大家好,窝又来了,今天我们接着上回的继续完善我们的后续,来巩固我们的知识
目录
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(){
//无参构造方法
}
今天的进度差不多就先到此结束啦,喜欢的可以留下宝贵的评论,掰掰咯