实现论坛管理,1、在mysql中创建论坛表,包含留言时间、留言IP地址、留言内容,2、在Servlet程序中实现发表留言、留言显示、删除留言
时间: 2024-05-21 11:10:17 浏览: 15
1. 在MySQL中创建论坛表:
CREATE TABLE `forum` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`message_time` datetime DEFAULT NULL,
`ip_address` varchar(20) DEFAULT NULL,
`message_content` varchar(500) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
2. 在Servlet程序中实现发表留言、留言显示、删除留言:
发表留言:
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String ipAddress = request.getRemoteAddr();
String message = request.getParameter("message");
Connection conn = null;
PreparedStatement stmt = null;
try {
conn = DBUtil.getConnection();
String sql = "INSERT INTO forum (message_time, ip_address, message_content) VALUES (?,?,?)";
stmt = conn.prepareStatement(sql);
stmt.setTimestamp(1, new Timestamp(System.currentTimeMillis()));
stmt.setString(2, ipAddress);
stmt.setString(3, message);
stmt.executeUpdate();
response.sendRedirect("forum.jsp");
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(stmt);
DBUtil.close(conn);
}
}
留言显示:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
List<ForumMessage> messages = new ArrayList<ForumMessage>();
try {
conn = DBUtil.getConnection();
String sql = "SELECT * FROM forum ORDER BY message_time DESC";
stmt = conn.prepareStatement(sql);
rs = stmt.executeQuery();
while (rs.next()) {
ForumMessage message = new ForumMessage();
message.setId(rs.getInt("id"));
message.setMessageTime(rs.getTimestamp("message_time"));
message.setIpAddress(rs.getString("ip_address"));
message.setMessageContent(rs.getString("message_content"));
messages.add(message);
}
request.setAttribute("messages", messages);
request.getRequestDispatcher("forum.jsp").forward(request, response);
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(rs);
DBUtil.close(stmt);
DBUtil.close(conn);
}
}
删除留言:
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
int messageId = Integer.parseInt(request.getParameter("messageId"));
Connection conn = null;
PreparedStatement stmt = null;
try {
conn = DBUtil.getConnection();
String sql = "DELETE FROM forum WHERE id = ?";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, messageId);
stmt.executeUpdate();
response.sendRedirect("forum.jsp");
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(stmt);
DBUtil.close(conn);
}
}
其中,ForumMessage是一个JavaBean,用于存储留言信息。DBUtil是一个数据库工具类,用于获取数据库连接和关闭资源。forum.jsp是留言显示页面,包含留言列表和删除留言的链接。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![xlsx](https://img-home.csdnimg.cn/images/20210720083732.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)