#数据库
CREATE TABLE friend(
fid int PRIMARY KEY AUTO_INCREMENT,
nickname VARCHAR(30) not null
);
CREATE TABLE redpacket(
rid int PRIMARY KEY AUTO_INCREMENT,
fid int not null,
money DOUBLE not null,
message VARCHAR(200),
sendtime datetime not NULL,
FOREIGN key (fid) REFERENCES friend (fid)
);
INSERT INTO friend(nickname) VALUES (‘德华’);
INSERT INTO friend(nickname) VALUES (‘寒寒’);
INSERT INTO friend(nickname) VALUES (‘小猪’);
INSERT INTO friend(nickname) VALUES (‘金刚侠’);
#实体类
package com.neu.entity;
public class Friend {
private Integer fid;
private String nickname;
public Friend() {
super();
// TODO Auto-generated constructor stub
}
public Friend(Integer fid, String nickname) {
super();
this.fid = fid;
this.nickname = nickname;
}
public Integer getFid() {
return fid;
}
public void setFid(Integer fid) {
this.fid = fid;
}
public String getNickname() {
return nickname;
}
public void setNickname(String nickname) {
this.nickname = nickname;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((fid == null) ? 0 : fid.hashCode());
result = prime * result + ((nickname == null) ? 0 : nickname.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Friend other = (Friend) obj;
if (fid == null) {
if (other.fid != null)
return false;
} else if (!fid.equals(other.fid))
return false;
if (nickname == null) {
if (other.nickname != null)
return false;
} else if (!nickname.equals(other.nickname))
return false;
return true;
}
@Override
public String toString() {
return "Friend [fid=" + fid + ", nickname=" + nickname + "]";
}
}
package com.neu.entity;
import java.util.Date;
public class Redpacket {
private Integer rid;
private Friend friend;
private Double money;
private String message;
private Date sendtime;
public Redpacket() {
super();
// TODO Auto-generated constructor stub
}
public Redpacket(Integer rid, Friend friend, Double money, String message, Date sendtime) {
super();
this.rid = rid;
this.friend = friend;
this.money = money;
this.message = message;
this.sendtime = sendtime;
}
public Integer getRid() {
return rid;
}
public void setRid(Integer rid) {
this.rid = rid;
}
public Friend getFriend() {
return friend;
}
public void setFriend(Friend friend) {
this.friend = friend;
}
public Double getMoney() {
return money;
}
public void setMoney(Double money) {
this.money = money;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public Date getSendtime() {
return sendtime;
}
public void setSendtime(Date sendtime) {
this.sendtime = sendtime;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((friend == null) ? 0 : friend.hashCode());
result = prime * result + ((message == null) ? 0 : message.hashCode());
result = prime * result + ((money == null) ? 0 : money.hashCode());
result = prime * result + ((rid == null) ? 0 : rid.hashCode());
result = prime * result + ((sendtime == null) ? 0 : sendtime.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Redpacket other = (Redpacket) obj;
if (friend == null) {
if (other.friend != null)
return false;
} else if (!friend.equals(other.friend))
return false;
if (message == null) {
if (other.message != null)
return false;
} else if (!message.equals(other.message))
return false;
if (money == null) {
if (other.money != null)
return false;
} else if (!money.equals(other.money))
return false;
if (rid == null) {
if (other.rid != null)
return false;
} else if (!rid.equals(other.rid))
return false;
if (sendtime == null) {
if (other.sendtime != null)
return false;
} else if (!sendtime.equals(other.sendtime))
return false;
return true;
}
@Override
public String toString() {
return "Redpacket [rid=" + rid + ", friend=" + friend + ", money=" + money + ", message=" + message
+ ", sendtime=" + sendtime + "]";
}
}
#FriendDao .java
package com.neu.dao;
import java.util.List;
import com.neu.entity.Friend;
public interface FriendDao {
List<Friend> getAll() throws Exception;
Friend getById(int fid) throws Exception;
Friend getByName(String nickname) throws Exception;
}
#FriendDaoImpl.java
package com.neu.dao;
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import com.neu.entity.Friend;
public class FriendDaoImpl implements FriendDao {
@Override
public List<Friend> getAll() throws Exception {
Connection connection = JDBCUtil.getConnection();
String sql = "select * from friend";
ResultSet rs = JDBCUtil.executeQuery(connection, sql, null);
List<Friend> list = new ArrayList<>();
Friend friend = null;
int fid;
String nickname;
while(rs.next()) {
fid = rs.getInt("fid");
nickname = rs.getString("nickname");
friend = new Friend(fid, nickname);
list.add(friend);
}
JDBCUtil.closeConnection(connection);
return list;
}
@Override
public Friend getById(int fid) throws Exception {
Connection connection = JDBCUtil.getConnection();
String sql = "select * from friend where fid = ?";
ResultSet rs = JDBCUtil.executeQuery(connection, sql, new Object[] {fid});
Friend friend=null;
String nickname;
if(rs.next()) {
nickname = rs.getString("nickname");
friend = new Friend(fid, nickname);
}
return friend;
}
@Override
public Friend getByName(String nickname) throws Exception {
Connection connection = JDBCUtil.getConnection();
String sql = "select * from friend where nickname = ?";
ResultSet rs = JDBCUtil.executeQuery(connection, sql, new Object[] {nickname});
Friend friend=null;
int fid;
if(rs.next()) {
fid = rs.getInt("fid");
friend = new Friend(fid, nickname);
}
return friend;
}
}
#RedpacketDao.java
package com.neu.dao;
import java.util.Date;
import java.util.List;
import com.neu.entity.Friend;
import com.neu.entity.Redpacket;
public interface RedpacketDao {
List<Redpacket> getAll() throws Exception;
int getInsert(Friend friend,double money,String message,Date sendtime) throws Exception;
}
#RedpacketDaoImpl.java
package com.neu.dao;
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import com.neu.entity.Friend;
import com.neu.entity.Redpacket;
public class RedpacketDaoImpl implements RedpacketDao {
@Override
public List<Redpacket> getAll() throws Exception {
Connection connection = JDBCUtil.getConnection();
String sql = "select * from redpacket";
ResultSet rs = JDBCUtil.executeQuery(connection, sql, null);
List<Redpacket> list = new ArrayList<>();
FriendDao friendDao = new FriendDaoImpl();
Redpacket redpacket = null;
while (rs.next()) {
int rid = rs.getInt("rid");
int fid = rs.getInt("fid");
double money = rs.getDouble("money");
String message = rs.getString("message");
Date sendtime = rs.getTimestamp("sendtime");
Friend friend = friendDao.getById(fid);
redpacket = new Redpacket(rid, friend, money, message, sendtime);
list.add(redpacket);
}
JDBCUtil.closeConnection(connection);
return list;
}
@Override
public int getInsert(Friend friend,double money,String message,Date sendtime) throws Exception {
String sql = "insert into redpacket(fid,money,message,sendtime) values (?,?,?,?)";
int n = JDBCUtil.executeUpdate(sql, new Object[] {friend.getFid(),money,message,sendtime});
return n;
}
}
#RedPacketServlet.java
package com.neu.servlet;
import java.io.IOException;
import java.util.List;
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 com.neu.entity.Friend;
import com.neu.service.FriendService;
import com.neu.service.FriendServiceImpl;
/**
* Servlet implementation class RedPacketServlet
*/
@WebServlet("/RedPacket")
public class RedPacketServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
FriendService friendService = new FriendServiceImpl();
try {
List<Friend> list = friendService.getAll();
request.setAttribute("list", list);
request.getRequestDispatcher("WEB-INF/weixin/showf.jsp").forward(request, response);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
#showPacketServlet.java
package com.neu.servlet;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
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 com.neu.entity.Friend;
import com.neu.entity.Redpacket;
import com.neu.service.FriendService;
import com.neu.service.FriendServiceImpl;
import com.neu.service.RedpacketService;
import com.neu.service.RedpacketServiceImpl;
/**
* Servlet implementation class showPacketServlet
*/
@WebServlet("/showPacket")
public class showPacketServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
String nickname = request.getParameter("nickname");
double money = Double.parseDouble(request.getParameter("money"));
String message = request.getParameter("message");
SimpleDateFormat d = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date();
RedpacketService redpacketService = new RedpacketServiceImpl();
FriendService friendService = new FriendServiceImpl();
try {
Friend friend = friendService.getByName(nickname);
redpacketService.getInsert(friend, money, message, date);
List<Redpacket> list = redpacketService.getAll();
request.setAttribute("list", list);
request.getRequestDispatcher("/WEB-INF/weixin/redpacketlist.jsp").forward(request, response);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
#showf.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="${pageContext.request.contextPath }/showPacket" method="post">
<table>
<tr>
<td colspan="2">好友列表:</td>
</tr>
<c:forEach items="${ list }" var="friend">
<tr>
<td colspan="2"><input type="radio" name="nickname" value="${ friend.nickname }">${ friend.nickname }</td>
</tr>
</c:forEach>
<tr>
<td>红包金额:</td><td><input type="text" name="money"></td>
</tr>
<tr>
<td>留 言:</td><td><input type="text" name="message" value="恭喜发财,大吉大利"></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="发 送"> <input type="reset" value="重 置"></td>
</tr>
</table>
</form>
</body>
</html>
#redpacketlist.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<table border="1" width="700" >
<tr>
<th>序号</th>
<th>好友名称</th>
<th>金额</th>
<th>留言</th>
<th>发送时间</th>
</tr>
<c:forEach items="${ list }" var="redpacket">
<tr align="center">
<td>${ redpacket.rid }</td>
<td>${ redpacket.friend.nickname }</td>
<td>${ redpacket.money }元</td>
<td>${ redpacket.message }</td>
<td>
<fmt:formatDate value="${ redpacket.sendtime }" pattern="yyyy-MM-dd HH:mm:ss"/>
</td>
</tr>
</c:forEach>
</table>
<a href="${ pageContext.request.contextPath }/RedPacket">返回</a>
</body>
</html>