Javaweb实现背单词功能的代码
时间: 2023-10-16 15:34:55 浏览: 73
以下是一个简单的背单词功能的 JavaWeb 代码示例:
1. 单词列表页面
```
<%@ 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>
<table border="1">
<tr>
<th>单词</th>
<th>释义</th>
</tr>
<%
// 从数据库中读取单词列表
List<Word> wordList = WordDAO.getAllWords();
for (Word word : wordList) {
%>
<tr>
<td><%= word.getWord() %></td>
<td><%= word.getMeaning() %></td>
</tr>
<%
}
%>
</table>
<br>
<a href="add_word.jsp">添加单词</a>
</body>
</html>
```
2. 添加单词页面
```
<%@ 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 action="add_word.do" method="post">
<label for="word">单词:</label>
<input type="text" id="word" name="word"><br>
<label for="meaning">释义:</label>
<textarea id="meaning" name="meaning"></textarea><br>
<input type="submit" value="添加">
<input type="reset" value="重置">
</form>
<br>
<a href="word_list.jsp">返回单词列表</a>
</body>
</html>
```
3. 添加单词的 Servlet
```
@WebServlet("/add_word.do")
public class AddWordServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
String word = request.getParameter("word");
String meaning = request.getParameter("meaning");
// 将单词添加到数据库
boolean result = WordDAO.addWord(word, meaning);
if (result) {
response.sendRedirect("word_list.jsp");
} else {
response.getWriter().println("添加单词失败!");
}
}
}
```
4. WordDAO 类
```
public class WordDAO {
private static final String DRIVER_NAME = "com.mysql.jdbc.Driver";
private static final String URL = "jdbc:mysql://localhost:3306/word_db?useSSL=false&useUnicode=true&characterEncoding=UTF-8";
private static final String USERNAME = "root";
private static final String PASSWORD = "root";
public static List<Word> getAllWords() {
List<Word> wordList = new ArrayList<>();
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
Class.forName(DRIVER_NAME);
conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
stmt = conn.createStatement();
rs = stmt.executeQuery("SELECT * FROM word");
while (rs.next()) {
Word word = new Word(rs.getInt("id"), rs.getString("word"), rs.getString("meaning"));
wordList.add(word);
}
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
} finally {
try {
if (rs != null) rs.close();
if (stmt != null) stmt.close();
if (conn != null) conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return wordList;
}
public static boolean addWord(String word, String meaning) {
Connection conn = null;
PreparedStatement pstmt = null;
try {
Class.forName(DRIVER_NAME);
conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
pstmt = conn.prepareStatement("INSERT INTO word (word, meaning) VALUES (?, ?)");
pstmt.setString(1, word);
pstmt.setString(2, meaning);
int rows = pstmt.executeUpdate();
return rows > 0;
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
} finally {
try {
if (pstmt != null) pstmt.close();
if (conn != null) conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return false;
}
}
```
以上代码只是一个简单的示例,实际应用中可能会有更复杂的要求和实现方式。
相关推荐
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![](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)