帮我写一个javaee 收货地址
时间: 2024-05-06 11:15:53 浏览: 138
好的,以下是一个简单的JavaEE收货地址示例:
首先,我们需要一个实体类来表示收货地址:
```
public class Address {
private String recipientName; // 收件人姓名
private String addressLine1; // 地址第一行
private String addressLine2; // 地址第二行
private String city; // 城市
private String state; // 州/省份/地区
private String zipCode; // 邮政编码
private String country; // 国家
// 省略 getter 和 setter 方法
}
```
接下来,我们需要一个DAO类来处理数据库中的地址数据:
```
public class AddressDAO {
private Connection conn;
public AddressDAO() throws SQLException {
// 创建数据库连接
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "username", "password");
}
public void addAddress(Address address) throws SQLException {
// 向数据库中添加收货地址
PreparedStatement ps = conn.prepareStatement("INSERT INTO addresses (recipient_name, address_line1, address_line2, city, state, zip_code, country) VALUES (?, ?, ?, ?, ?, ?, ?)");
ps.setString(1, address.getRecipientName());
ps.setString(2, address.getAddressLine1());
ps.setString(3, address.getAddressLine2());
ps.setString(4, address.getCity());
ps.setString(5, address.getState());
ps.setString(6, address.getZipCode());
ps.setString(7, address.getCountry());
ps.executeUpdate();
}
public List<Address> getAllAddresses() throws SQLException {
// 从数据库中获取所有收货地址
List<Address> addresses = new ArrayList<>();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM addresses");
while (rs.next()) {
Address address = new Address();
address.setRecipientName(rs.getString("recipient_name"));
address.setAddressLine1(rs.getString("address_line1"));
address.setAddressLine2(rs.getString("address_line2"));
address.setCity(rs.getString("city"));
address.setState(rs.getString("state"));
address.setZipCode(rs.getString("zip_code"));
address.setCountry(rs.getString("country"));
addresses.add(address);
}
return addresses;
}
// 省略其他常见的 DAO 方法,例如更新地址、删除地址等
}
```
最后,我们需要一个Servlet来处理HTTP请求,并将数据传递给DAO类:
```
@WebServlet("/addresses")
public class AddressServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
AddressDAO dao = new AddressDAO();
List<Address> addresses = dao.getAllAddresses();
request.setAttribute("addresses", addresses); // 将地址列表传递给JSP页面
request.getRequestDispatcher("addresses.jsp").forward(request, response); // 转发到JSP页面
} catch (SQLException e) {
throw new ServletException("Error getting addresses from database", e);
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String recipientName = request.getParameter("recipientName");
String addressLine1 = request.getParameter("addressLine1");
String addressLine2 = request.getParameter("addressLine2");
String city = request.getParameter("city");
String state = request.getParameter("state");
String zipCode = request.getParameter("zipCode");
String country = request.getParameter("country");
Address address = new Address();
address.setRecipientName(recipientName);
address.setAddressLine1(addressLine1);
address.setAddressLine2(addressLine2);
address.setCity(city);
address.setState(state);
address.setZipCode(zipCode);
address.setCountry(country);
try {
AddressDAO dao = new AddressDAO();
dao.addAddress(address);
response.sendRedirect("addresses"); // 重定向到地址列表页面
} catch (SQLException e) {
throw new ServletException("Error adding address to database", e);
}
}
}
```
最后,我们需要一个JSP页面来显示地址列表和收集新地址:
```
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Addresses</title>
</head>
<body>
<h1>Addresses</h1>
<table>
<tr>
<th>Recipient Name</th>
<th>Address Line 1</th>
<th>Address Line 2</th>
<th>City</th>
<th>State</th>
<th>Zip Code</th>
<th>Country</th>
</tr>
<c:forEach items="${addresses}" var="address">
<tr>
<td>${address.recipientName}</td>
<td>${address.addressLine1}</td>
<td>${address.addressLine2}</td>
<td>${address.city}</td>
<td>${address.state}</td>
<td>${address.zipCode}</td>
<td>${address.country}</td>
</tr>
</c:forEach>
</table>
<h2>Add an Address</h2>
<form method="post" action="addresses">
<label>Recipient Name:</label>
<input type="text" name="recipientName"><br>
<label>Address Line 1:</label>
<input type="text" name="addressLine1"><br>
<label>Address Line 2:</label>
<input type="text" name="addressLine2"><br>
<label>City:</label>
<input type="text" name="city"><br>
<label>State:</label>
<input type="text" name="state"><br>
<label>Zip Code:</label>
<input type="text" name="zipCode"><br>
<label>Country:</label>
<input type="text" name="country"><br>
<input type="submit" value="Add Address">
</form>
</body>
</html>
```
这个例子中,我们使用了JDBC连接到MySQL数据库,并使用Servlet和JSP处理HTTP请求和响应。当用户访问地址列表页面时,我们从数据库中获取所有地址并将它们传递给JSP页面进行显示。当用户提交新地址时,我们将其添加到数据库中并重定向到地址列表页面。
阅读全文