第9题 简易高校二手物品交易系统 该系统需提供物品的信息显示、包括:发布人、所属高校、学号、物品类别、物品名称、新旧程度、原价格、拟售价格、成交人、成交日期、实付款金额。 要求: 完成交易信息登记(新增)、修改、删除、查询。 在购买的过程中,有意向的买家会和卖家进行沟通,系统需要保存每次沟通的信息。例如:买家1、提问内容XXX、提问日期。 卖家可根据买家的提问内容进行1-多次回复、包括回复内容、回复日期、回复人。javaweb代码实现
时间: 2023-07-04 09:14:11 浏览: 111
以下是简易高校二手物品交易系统的部分JavaWeb代码实现,包括交易信息的新增、修改、删除、查询,以及买家和卖家之间的沟通记录保存和回复。
1. 交易信息的新增
```java
// 获取前端提交的交易信息
String publisher = request.getParameter("publisher");
String university = request.getParameter("university");
String studentId = request.getParameter("studentId");
String category = request.getParameter("category");
String itemName = request.getParameter("itemName");
String condition = request.getParameter("condition");
double originalPrice = Double.parseDouble(request.getParameter("originalPrice"));
double price = Double.parseDouble(request.getParameter("price"));
// 将交易信息插入数据库
String sql = "INSERT INTO transactions (publisher, university, student_id, category, item_name, `condition`, original_price, price) VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, publisher);
pstmt.setString(2, university);
pstmt.setString(3, studentId);
pstmt.setString(4, category);
pstmt.setString(5, itemName);
pstmt.setString(6, condition);
pstmt.setDouble(7, originalPrice);
pstmt.setDouble(8, price);
pstmt.executeUpdate();
```
2. 交易信息的修改
```java
// 获取前端提交的交易信息
int id = Integer.parseInt(request.getParameter("id"));
String publisher = request.getParameter("publisher");
String university = request.getParameter("university");
String studentId = request.getParameter("studentId");
String category = request.getParameter("category");
String itemName = request.getParameter("itemName");
String condition = request.getParameter("condition");
double originalPrice = Double.parseDouble(request.getParameter("originalPrice"));
double price = Double.parseDouble(request.getParameter("price"));
// 更新数据库中的交易信息
String sql = "UPDATE transactions SET publisher=?, university=?, student_id=?, category=?, item_name=?, `condition`=?, original_price=?, price=? WHERE id=?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, publisher);
pstmt.setString(2, university);
pstmt.setString(3, studentId);
pstmt.setString(4, category);
pstmt.setString(5, itemName);
pstmt.setString(6, condition);
pstmt.setDouble(7, originalPrice);
pstmt.setDouble(8, price);
pstmt.setInt(9, id);
pstmt.executeUpdate();
```
3. 交易信息的删除
```java
// 获取前端提交的交易信息ID
int id = Integer.parseInt(request.getParameter("id"));
// 从数据库中删除该交易信息
String sql = "DELETE FROM transactions WHERE id=?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, id);
pstmt.executeUpdate();
```
4. 交易信息的查询
```java
// 查询所有交易信息
String sql = "SELECT * FROM transactions";
PreparedStatement pstmt = conn.prepareStatement(sql);
ResultSet rs = pstmt.executeQuery();
// 将查询结果转换成JSON格式的字符串
JSONArray jsonArray = new JSONArray();
while (rs.next()) {
JSONObject jsonObject = new JSONObject();
jsonObject.put("id", rs.getInt("id"));
jsonObject.put("publisher", rs.getString("publisher"));
jsonObject.put("university", rs.getString("university"));
jsonObject.put("studentId", rs.getString("student_id"));
jsonObject.put("category", rs.getString("category"));
jsonObject.put("itemName", rs.getString("item_name"));
jsonObject.put("condition", rs.getString("condition"));
jsonObject.put("originalPrice", rs.getDouble("original_price"));
jsonObject.put("price", rs.getDouble("price"));
jsonObject.put("buyer", rs.getString("buyer"));
jsonObject.put("dealDate", rs.getString("deal_date"));
jsonObject.put("paidAmount", rs.getDouble("paid_amount"));
jsonArray.add(jsonObject);
}
// 将JSON格式的字符串返回给前端
response.getWriter().write(jsonArray.toString());
```
5. 买家和卖家之间的沟通记录保存和回复
```java
// 获取前端提交的沟通信息
int transactionId = Integer.parseInt(request.getParameter("transactionId"));
String buyer = request.getParameter("buyer");
String question = request.getParameter("question");
String askingDate = request.getParameter("askingDate");
// 将沟通记录插入数据库
String sql = "INSERT INTO communications (transaction_id, buyer, question, asking_date) VALUES (?, ?, ?, ?)";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, transactionId);
pstmt.setString(2, buyer);
pstmt.setString(3, question);
pstmt.setString(4, askingDate);
pstmt.executeUpdate();
// 查询该交易的所有沟通记录
sql = "SELECT * FROM communications WHERE transaction_id=?";
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, transactionId);
ResultSet rs = pstmt.executeQuery();
// 将查询结果转换成JSON格式的字符串
JSONArray jsonArray = new JSONArray();
while (rs.next()) {
JSONObject jsonObject = new JSONObject();
jsonObject.put("id", rs.getInt("id"));
jsonObject.put("transactionId", rs.getInt("transaction_id"));
jsonObject.put("buyer", rs.getString("buyer"));
jsonObject.put("question", rs.getString("question"));
jsonObject.put("askingDate", rs.getString("asking_date"));
jsonObject.put("reply", rs.getString("reply"));
jsonObject.put("replyDate", rs.getString("reply_date"));
jsonObject.put("replier", rs.getString("replier"));
jsonArray.add(jsonObject);
}
// 将JSON格式的字符串返回给前端
response.getWriter().write(jsonArray.toString());
```
以上是简易高校二手物品交易系统的部分JavaWeb代码实现,仅供参考。实际开发中,还需要根据具体需求进行完善和优化。
阅读全文