创建MemberTypeDao
类,放在dao
包下:
public class MemberTypeDao {
QueryRunner runner = new QueryRunner();
// 查询所有会员类型
public List<MemberType> getAll() throws SQLException {
Connection conn = DBHelper.getConnection();
String sql = "select * from membertype";
List<MemberType> memberTypes = runner.query(conn, sql, new BeanListHandler<MemberType>(MemberType.class));
conn.close();
return memberTypes;
}
// 根据会员编号查询一个会员信息
public MemberType getById(long id) throws SQLException{
Connection conn = DBHelper.getConnection();
String sql = "select * from membertype where id = ?";
MemberType memberType = runner.query(conn, sql, new BeanHandler<MemberType>(MemberType.class), id);
conn.close();
return memberType;
}
}
创建MemberDao
类,放在dao
包下:
public class MemberDao {
QueryRunner runner = new QueryRunner();
// 添加会员
public int add(String name, String pwd, long typeId, double balance, String tel, String idNumber) throws SQLException {
Connection conn = DBHelper.getConnection();
String sql = "insert into member(`name`, pwd, typeId, balance, regdate, tel, idNumber) values(?, ?, ?, ?, CURRENT_DATE, ?, ?)";
int count = runner.update(conn, sql, name, pwd, typeId, balance, tel, idNumber);
DBHelper.close(conn);
return count;
}
//修改会员
public int modify(long id, String name, String pwd, long typeId, double balance, String tel, String idNumber) throws SQLException {
Connection conn = DBHelper.getConnection();
String sql = "update member set `name`=?, pwd=?, typeId=?, balance=?, tel=?,idNumber=? where id=?";
int count = runner.update(conn, sql, name, pwd, typeId, balance, tel, idNumber, id);
DBHelper.close(conn);
return count;
}
// 删除会员
public int remove(long id) throws SQLException {
Connection conn = DBHelper.getConnection();
String sql = "delete from member where id = ?";
int count = runner.update(conn, sql, id);
DBHelper.close(conn);
return count;
}
// 会员充值
public int modifyBalance(String idNumber, double amount) throws SQLException {
Connection conn = DBHelper.getConnection();
String sql = "update member set balance = balance + ? where idNumber = ?";
int count = runner.update(conn, sql, amount, idNumber);
DBHelper.close(conn);
return count;
}
// 修改押金
public int modifyBalance (long id, double amount) throws SQLException {
Connection conn = DBHelper.getConnection();
String sql = "update member set balance = balance + ? where id = ?";
int count = runner.update(conn, sql, amount, id);
DBHelper.close(conn);
return count;
}
// 查询所有会员
public List<Member> getAll() throws SQLException {
Connection conn = DBHelper.getConnection();
String sql = "select id, `name`, pwd, typeId, balance, regdate, tel, idNumber from member";
List<Member> members = runner.query(conn, sql, new BeanListHandler<Member>(Member.class));
DBHelper.close(conn);
return members;
}
// 根据会员编号差会员信息
public Member getById(long id) throws SQLException {
Connection conn = DBHelper.getConnection();
String sql = "select id, `name`, pwd, typeId, balance, regdate, tel, idNumber from member where id = ?";
Member member = runner.query(conn, sql, new BeanHandler<Member>(Member.class), id);
DBHelper.close(conn);
return member;
}
//根据会员身份证查会员信息
public Member getByIdNumber(String idNumber) throws SQLException {
Connection conn = DBHelper.getConnection();
String sql = "select id, `name`, pwd, typeId, balance, regdate, tel, idNumber, from member where id = ?";
Member member = runner.query(conn, sql, new BeanHandler<Member>(Member.class), idNumber);
DBHelper.close(conn);
return member;
}
//判断会员编号是否存在Record中(作为外键 )
public boolean exits(long id) throws SQLException {
Connection conn = DBHelper.getConnection();
String sql = "select count(id) from record where memberId = ?";
Number number = runner.query(conn, sql, new ScalarHandler<>(), id);
DBHelper.close(conn);
return number.intValue() > 0 ? true : false;
}
}
创建MemberTypeBiz
类,放在biz
包下:
public class MemberTypeBiz {
MemberTypeDao memberTypeDao = new MemberTypeDao();
// 查询所有会员类型
public List<MemberType> getAll() {
try {
return memberTypeDao.getAll();
} catch (SQLException e) {
e.printStackTrace();
return null;
}
}
// 根据会员编号查询一个会员信息
public MemberType getById(long id) {
MemberType memberType = null;
try {
memberType = memberTypeDao.getById(id);
} catch (SQLException e) {
e.printStackTrace();
}
return memberType;
}
}
创建MemberBiz
类,放在biz
包下:
public class MemberBiz {
MemberDao memberDao = new MemberDao();
MemberTypeDao typeDao = new MemberTypeDao();
public int add(String name, String pwd, long typeId, double balance, String tel, String idNumber) {
int count = 0;
try {
count = memberDao.add(name, pwd, typeId, balance, tel, idNumber);
} catch (SQLException e) {
e.printStackTrace();
}
return count;
}
public int modify(long id, String name, String pwd, long typeId, double balance, String tel, String idNumber) {
int count = 0;
try {
count = memberDao.modify(id, name, pwd, typeId, balance, tel, idNumber);
} catch (SQLException e) {
e.printStackTrace();
}
return count;
}
public int remove(long id) throws Exception {
Member member = getById(id);
if (member.getBalance() > 0) {
throw new Exception("此会员消费金额大于0,删除失败");
}
if (memberDao.exits(id)) {
throw new Exception("此会员还有在借阅的书籍,删除失败");
}
int count = 0;
try {
count = memberDao.remove(id);
} catch (SQLException e) {
e.printStackTrace();
}
return count;
}
public int modifyBalance(String idNumber, double amount) {
int count = 0;
try {
count = memberDao.modifyBalance(idNumber, amount);
} catch (SQLException e) {
e.printStackTrace();
}
return count;
}
public List<Member> getAll() {
MemberTypeDao typeDao = new MemberTypeDao();
List<Member> members = null;
try {
members = memberDao.getAll();
for (Member member: members) {
MemberType type = typeDao.getById(member.getTypeId());
member.setType(type);
}
} catch (SQLException e) {
e.printStackTrace();
}
return members;
}
public Member getById(long id) {
Member member = null;
try {
member = memberDao.getById(id);
MemberType memberType = typeDao.getById(member.getTypeId());
member.setType(memberType);
} catch (SQLException e) {
e.printStackTrace();
}
return member;
}
public Member getByIdNumber(String idNumber) {
Member member = null;
try {
member = memberDao.getByIdNumber(idNumber);
MemberType memberType = typeDao.getById(member.getTypeId());
member.setType(memberType);
} catch (SQLException e) {
e.printStackTrace();
}
return member;
}
}
添加MemberServlet
类,放在action
包下。
@WebServlet("/member.let")
public class MemberServlet extends HttpServlet {
MemberTypeBiz memberTypeBiz = new MemberTypeBiz();
MemberBiz memberBiz = new MemberBiz();
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
resp.setContentType("text/html;charset=utf-8");
PrintWriter out = resp.getWriter();
HttpSession session = req.getSession();
if (session.getAttribute("user") == null) {
out.println("");
return;
}
String type = req.getParameter("type");
switch (type) {
case "query":
List<Member> memberList = memberBiz.getAll();
req.setAttribute("memberList", memberList);
req.getRequestDispatcher("mem_list.jsp").forward(req, resp);
break;
default:
resp.sendError(404, "请求的地址不存在");
}
}
}
修改left.jsp
部分代码
- 会员开卡
- 会员管理
把mem_list.html
文件转换成mem_list.jsp
文件
修改mem_list.jsp
文件
引入JSTL标准库
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
修改文件的from
<table width="100%" class="cont tr_color">
<tr>
<th>编号th>
<th>会员名称th>
<th>联系电话th>
<th>会员类型th>
<th>余额th>
<th>注册时间th>
<th>身份证号th>
<th>操作th>
tr>
<c:forEach items="${memberList}" var="m">
<tr align="center" class="d">
<td>${m.id}td>
<td>${m.name}td>
<td>${m.tel}td>
<td>${m.type.name}td>
<td>${m.balance}td>
<td>${m.regdate}td>
<td>${m.idNumber}td>
<td>
<a onclick="return confirm('确认修改');" href="member.let?type=modifypre&id=${m.id}">修改a>
<a onclick="return confirm('确认删除');" href="member.let?type=remove&id=${m.id}">删除a>
td>
tr>
c:forEach>
table>
在MemberServlet
的switch添加如下代码
case "addpre":
List<MemberType> memberTypes = memberTypeBiz.getAll();
req.setAttribute("memberTypes", memberTypes);
req.getRequestDispatcher("mem_add.jsp").forward(req, resp);
break;
把mem_add.html
文件转换成mem_add.jsp
文件
修改mem_add.jsp
文件下面显示的代码
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<script src="Js/jquery-3.3.1.min.js">script>
<script language="JavaScript">
$(function () {
$(":radio").each(function(index, element) {
$(this).click(function() {
var value = $(this).next().val();
$("#balance").val(value)
})
})
})
script>
<form action="member.let?type=add" method="post">
<table width="100%" class="cont">
<tr>
<td width="10%">用户名:td>
<td width="20%"><input class="text" type="text" name="name" value="" required/>td>
tr>
<tr>
<td>密码:td>
<td><input class="text" type="password" name="pwd" value="" required />td>
<td>td>
tr>
<tr>
<td>确认密码:td>
<td><input class="text" type="password" name="pwd2" value="" required/>td>
tr>
<tr>
<td>会员类型td>
<td>
<c:forEach items="${memberTypes}" var="mt">
<c:if test="${mt.id==1}" >
<input type="radio" name="memberType" value="${mt.id}" checked/> ${mt.name}
<input type="hidden" value="${mt.recharge}">
c:if>
<c:if test="${mt.id!=1}" >
<input type="radio" name="memberType" value="${mt.id}" /> ${mt.name}
<input type="hidden" value="${mt.recharge}">
c:if>
c:forEach>
td>
tr>
<tr>
<td>充值金额td>
<td>
<input class="text" type="number" name="balance" value="100" id="balance" required/>
td>
tr>
table>
form>
在MemberServlet
的switch添加如下代码
case "add":
String name = req.getParameter("name");
String pwd = req.getParameter("pwd");
long memberTypeId = Long.parseLong(req.getParameter("memberType"));
double balance = Double.parseDouble(req.getParameter("balance"));
String tel = req.getParameter("tel");
String idNumber = req.getParameter("idNumber");
int count = memberBiz.add(name, pwd, memberTypeId, balance, tel, idNumber);
if (count > 0) {
out.println("");
} else {
out.println("");
}
break;
在MemberServlet
的switch添加如下代码
case "remove":
long memId = Long.parseLong(req.getParameter("id"));
try {
int count2 = memberBiz.remove(memId);
if (count2 > 0) {
out.println("");
} else {
out.println("");
}
} catch (Exception e) {
e.printStackTrace();
out.println("alert('"+e.getMessage()+"');location.href='member.let?type=query';");
}
break;
在MemberServlet
的switch添加如下代码
case "modifypre":
long id = Long.parseLong(req.getParameter("id"));
Member member = memberBiz.getById(id);
List<MemberType> memberTypes2 = memberTypeBiz.getAll();
req.setAttribute("member", member);
req.setAttribute("memberTypes", memberTypes2);
req.getRequestDispatcher("mem_modify.jsp").forward(req, resp);
break;
把mem_modify.html
文件转换成mem_modify.jsp
文件
修改mem_modify.jsp
文件下面显示的代码
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<script src="Js/jquery-3.3.1.min.js">script>
<script language="JavaScript">
$(function () {
$(":radio").each(function(index, element) {
$(this).click(function() {
var value = $(this).next().val();
$("#balance").val(value)
})
})
})
script>
<form action="member.let?type=modify" method="post">
<table width="100%" class="cont">
<tr>
<td width="10%">用户编号:td>
<td width="20%"><input class="text" type="text" name="id" value="${member.id}" readonly/>td>
tr>
<tr>
<td width="10%">用户名:td>
<td width="20%"><input class="text" type="text" name="name" value="${member.name}" readonly/>td>
tr>
<tr>
<td>密码:td>
<td><input class="text" type="password" name="pwd" value="${member.pwd}" required/>td>>
tr>
<tr>
<td>会员类型td>
<td>
<c:forEach items="${memberTypes}" var="mt">
<c:if test="${mt.id==member.typeId}">
<input type="radio" name="memberType" value="${mt.id}" checked/>${mt.name}
<input type="hidden" value="${mt.recharge}">
c:if>
<c:if test="${mt.id!=member.typeId}">
<input type="radio" name="memberType" value="${mt.id}"/>${mt.name}
<input type="hidden" value="${mt.recharge}">
c:if>
c:forEach>
td>
tr>
tr>
<tr>
<td>充值金额td>
<td>
<input class="text" id="balance" type="number" name="balance" value="${member.balance}" readonly/>
td>
tr>
<tr>
<td>电话号码td>
<td>
<input class="text" type="tel" name="tel" value="${member.tel}" required />
td>
tr>
<tr>
<td>身份证号td>
<td>
<input class="text" type="text" name="idNumber" value="${member.idNumber}" required />
td>
tr>
table>
form>
在MemberServlet
的switch添加如下代码
case "modify":
long memberId = Long.parseLong(req.getParameter("id"));
String name2 = req.getParameter("name");
String pwd2 = req.getParameter("pwd");
long memberTypeId2 = Long.parseLong(req.getParameter("memberType"));
double balance2 = Double.parseDouble(req.getParameter("balance"));
String tel2 = req.getParameter("tel");
String idNumber2 = req.getParameter("idNumber");
int count3 = memberBiz.modify(memberId, name2, pwd2, memberTypeId2, balance2, tel2, idNumber2);
if (count3 > 0) {
out.println("");
} else {
out.println("");
}
break;
在MemberServlet
的switch添加如下代码
case "modifyrecharge":
String idNumber3 = req.getParameter("idNumber");
double amount = Double.parseDouble(req.getParameter("amount"));
int count4 = memberBiz.modifyBalance(idNumber3, amount);
if (count4 > 0) {
out.println("");
} else {
out.println("");
}
break;
把mem_recharge.html
文件转换成mem_recharge.jsp
文件
修改mem_recharge.jsp
文件下面显示的代码
<form action="member.let?type=modifyrecharge" method="post">