利用JavaBean和MySQL实现留言板功能
时间: 2024-03-12 17:47:59 浏览: 108
实现留言板功能需要使用到JavaBean和MySQL数据库,以下是一个简单的示例:
1. 首先创建一个名为Message的JavaBean类,用于存储留言信息。
```
public class Message {
private int id;
private String name;
private String email;
private String message;
public Message() {
}
public Message(String name, String email, String message) {
this.name = name;
this.email = email;
this.message = message;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
```
2. 创建一个名为MessageDao的Java类,用于实现与MySQL数据库的交互。
```
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class MessageDao {
private Connection connection;
public MessageDao() {
connection = ConnectionFactory.getConnection();
}
public void addMessage(Message message) {
String sql = "INSERT INTO messages (name, email, message) VALUES (?, ?, ?)";
try (PreparedStatement statement = connection.prepareStatement(sql)) {
statement.setString(1, message.getName());
statement.setString(2, message.getEmail());
statement.setString(3, message.getMessage());
statement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
public List<Message> getAllMessages() {
String sql = "SELECT * FROM messages";
List<Message> messages = new ArrayList<>();
try (PreparedStatement statement = connection.prepareStatement(sql)) {
ResultSet resultSet = statement.executeQuery();
while (resultSet.next()) {
int id = resultSet.getInt("id");
String name = resultSet.getString("name");
String email = resultSet.getString("email");
String message = resultSet.getString("message");
Message msg = new Message(name, email, message);
msg.setId(id);
messages.add(msg);
}
} catch (SQLException e) {
e.printStackTrace();
}
return messages;
}
}
```
3. 创建一个名为MessageServlet的Java类,用于处理网页请求和响应。
```
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;
@WebServlet("/messages")
public class MessageServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private MessageDao messageDao;
public MessageServlet() {
super();
messageDao = new MessageDao();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
List<Message> messages = messageDao.getAllMessages();
request.setAttribute("messages", messages);
request.getRequestDispatcher("messages.jsp").forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String name = request.getParameter("name");
String email = request.getParameter("email");
String message = request.getParameter("message");
Message msg = new Message(name, email, message);
messageDao.addMessage(msg);
response.sendRedirect(request.getContextPath() + "/messages");
}
}
```
4. 创建一个名为messages.jsp的JSP页面,用于显示留言板界面和留言列表。
```
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>留言板</title>
</head>
<body>
<h1>留言板</h1>
<form method="post" action="messages">
<label for="name">姓名:</label>
<input type="text" id="name" name="name" required><br>
<label for="email">邮箱:</label>
<input type="email" id="email" name="email" required><br>
<label for="message">留言:</label>
<textarea id="message" name="message" required></textarea><br>
<button type="submit">提交</button>
</form>
<hr>
<h2>留言列表</h2>
<c:forEach var="msg" items="${messages}">
<div>
<h3>${msg.name} <span>${msg.email}</span></h3>
<p>${msg.message}</p>
</div>
</c:forEach>
</body>
</html>
```
5. 创建一个名为ConnectionFactory的Java类,用于创建MySQL数据库连接。
```
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class ConnectionFactory {
private static final String URL = "jdbc:mysql://localhost:3306/messages";
private static final String USERNAME = "root";
private static final String PASSWORD = "password";
public static Connection getConnection() {
Connection connection = null;
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
return connection;
}
}
```
以上是一个简单的利用JavaBean和MySQL实现留言板功能的示例,你可以根据需要进行修改和扩展。
阅读全文