训练豆包写代码

用jdbc先给我写出仓库表和物资表的实体类和sql语句,然后写出addWarehouseServlet,deleteWarehouseServlet,updateWarehouseServlet,UpdateMaterialServlet,AddMaterialServlet,DeleteMaterialServlet,再帮我写出一个管理员界面的html,里边包含物资表和仓库表的增删改,再分别写出物资表和仓库表增删改的html,然后再创建一个user实体类和sql语句,写出login和loginServlet,并且写出login.html
以下是按照你的需求完成的一系列代码和文件,包含数据库表的实体类、SQL 语句、Servlet 类、HTML 页面等。

  1. 数据库表结构和 SQL 语句
    仓库表(warehouse)
    sql
    CREATE TABLE warehouse (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(255) NOT NULL,
    location VARCHAR(255)
    );
    物资表(material)
    sql
    CREATE TABLE material (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(255) NOT NULL,
    quantity INT,
    warehouse_id INT,
    FOREIGN KEY (warehouse_id) REFERENCES warehouse(id)
    );
    用户表(user)
    sql
    CREATE TABLE user (
    id INT AUTO_INCREMENT PRIMARY KEY,
    username VARCHAR(255) NOT NULL UNIQUE,
    password VARCHAR(255) NOT NULL
    );

  2. 实体类
    Warehouse.java
    java
    public class Warehouse {
    private int id;
    private String name;
    private String location;

    // 构造函数、getter 和 setter 方法
    public Warehouse() {}

    public Warehouse(int id, String name, String location) {
    this.id = id;
    this.name = name;
    this.location = location;
    }

    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 getLocation() {
    return location;
    }

    public void setLocation(String location) {
    this.location = location;
    }
    }
    Material.java
    java
    public class Material {
    private int id;
    private String name;
    private int quantity;
    private int warehouseId;

    // 构造函数、getter 和 setter 方法
    public Material() {}

    public Material(int id, String name, int quantity, int warehouseId) {
    this.id = id;
    this.name = name;
    this.quantity = quantity;
    this.warehouseId = warehouseId;
    }

    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 int getQuantity() {
    return quantity;
    }

    public void setQuantity(int quantity) {
    this.quantity = quantity;
    }

    public int getWarehouseId() {
    return warehouseId;
    }

    public void setWarehouseId(int warehouseId) {
    this.warehouseId = warehouseId;
    }
    }
    User.java
    java
    public class User {
    private int id;
    private String username;
    private String password;

    // 构造函数、getter 和 setter 方法
    public User() {}

    public User(int id, String username, String password) {
    this.id = id;
    this.username = username;
    this.password = password;
    }

    public int getId() {
    return id;
    }

    public void setId(int id) {
    this.id = id;
    }

    public String getUsername() {
    return username;
    }

    public void setUsername(String username) {
    this.username = username;
    }

    public String getPassword() {
    return password;
    }

    public void setPassword(String password) {
    this.password = password;
    }
    }

  3. Servlet 类
    AddWarehouseServlet.java
    java
    import java.io.IOException;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.SQLException;

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("/addWarehouse")
public class AddWarehouseServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    String name = request.getParameter("name");
    String location = request.getParameter("location");

    try (Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/your_database", "username", "password");
         PreparedStatement stmt = conn.prepareStatement("INSERT INTO warehouse (name, location) VALUES (?, ?)")) {
        stmt.setString(1, name);
        stmt.setString(2, location);
        stmt.executeUpdate();
        response.sendRedirect("admin.jsp");
    } catch (SQLException e) {
        e.printStackTrace();
    }
}

}
DeleteWarehouseServlet.java
java
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

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("/deleteWarehouse")
public class DeleteWarehouseServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    int id = Integer.parseInt(request.getParameter("id"));

    try (Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/your_database", "username", "password");
         PreparedStatement stmt = conn.prepareStatement("DELETE FROM warehouse WHERE id = ?")) {
        stmt.setInt(1, id);
        stmt.executeUpdate();
        response.sendRedirect("admin.jsp");
    } catch (SQLException e) {
        e.printStackTrace();
    }
}

}
UpdateWarehouseServlet.java
java
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

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("/updateWarehouse")
public class UpdateWarehouseServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    int id = Integer.parseInt(request.getParameter("id"));
    String name = request.getParameter("name");
    String location = request.getParameter("location");

    try (Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/your_database", "username", "password");
         PreparedStatement stmt = conn.prepareStatement("UPDATE warehouse SET name = ?, location = ? WHERE id = ?")) {
        stmt.setString(1, name);
        stmt.setString(2, location);
        stmt.setInt(3, id);
        stmt.executeUpdate();
        response.sendRedirect("admin.jsp");
    } catch (SQLException e) {
        e.printStackTrace();
    }
}

}
AddMaterialServlet.java
java
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

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("/addMaterial")
public class AddMaterialServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    String name = request.getParameter("name");
    int quantity = Integer.parseInt(request.getParameter("quantity"));
    int warehouseId = Integer.parseInt(request.getParameter("warehouseId"));

    try (Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/your_database", "username", "password");
         PreparedStatement stmt = conn.prepareStatement("INSERT INTO material (name, quantity, warehouse_id) VALUES (?, ?, ?)")) {
        stmt.setString(1, name);
        stmt.setInt(2, quantity);
        stmt.setInt(3, warehouseId);
        stmt.executeUpdate();
        response.sendRedirect("admin.jsp");
    } catch (SQLException e) {
        e.printStackTrace();
    }
}

}
DeleteMaterialServlet.java
java
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

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("/deleteMaterial")
public class DeleteMaterialServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    int id = Integer.parseInt(request.getParameter("id"));

    try (Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/your_database", "username", "password");
         PreparedStatement stmt = conn.prepareStatement("DELETE FROM material WHERE id = ?")) {
        stmt.setInt(1, id);
        stmt.executeUpdate();
        response.sendRedirect("admin.jsp");
    } catch (SQLException e) {
        e.printStackTrace();
    }
}

}
UpdateMaterialServlet.java
java
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

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("/updateMaterial")
public class UpdateMaterialServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    int id = Integer.parseInt(request.getParameter("id"));
    String name = request.getParameter("name");
    int quantity = Integer.parseInt(request.getParameter("quantity"));
    int warehouseId = Integer.parseInt(request.getParameter("warehouseId"));

    try (Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/your_database", "username", "password");
         PreparedStatement stmt = conn.prepareStatement("UPDATE material SET name = ?, quantity = ?, warehouse_id = ? WHERE id = ?")) {
        stmt.setString(1, name);
        stmt.setInt(2, quantity);
        stmt.setInt(3, warehouseId);
        stmt.setInt(4, id);
        stmt.executeUpdate();
        response.sendRedirect("admin.jsp");
    } catch (SQLException e) {
        e.printStackTrace();
    }
}

}
LoginServlet.java
java
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@WebServlet("/login")
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    String username = request.getParameter("username");
    String password = request.getParameter("password");

    try (Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/your_database", "username", "password");
         PreparedStatement stmt = conn.prepareStatement("SELECT * FROM user WHERE username = ? AND password = ?")) {
        stmt.setString(1, username);
        stmt.setString(2, password);
        ResultSet rs = stmt.executeQuery();
        if (rs.next()) {
            HttpSession session = request.getSession();
            session.setAttribute("username", username);
            response.sendRedirect("admin.jsp");
        } else {
            response.sendRedirect("login.html?error=1");
        }
    } catch (SQLException e) {
        e.printStackTrace();
    }
}

}
4. HTML 页面
login.html
html

Login

Login



<% if (request.getParameter("error") != null) { %>

Invalid username or password.

<% } %> admin.jsp jsp Admin Panel

Admin Panel

Warehouses

Add Warehouse
ID Name Location Actions
<h2>Materials</h2>
<a href="addMaterial.html">Add Material</a>
<!-- 显示物资列表 -->
<table>
    <tr>
        <th>ID</th>
        <th>Name</th>
        <th>Quantity</th>
        <th>Warehouse</th>
        <th>Actions</th>
    </tr>
    <!-- 从数据库查询物资数据并显示 -->
</table>
addWarehouse.html html Add Warehouse

Add Warehouse



updateWarehouse.html html Update Warehouse

Update Warehouse

">

addMaterial.html html Add Material

Add Material




updateMaterial.html html Update Material

Update Material

">

Delete Warehouse Confirmation

Confirm Warehouse Deletion

Are you sure you want to delete the warehouse with ID: <%= request.getParameter("id") %>?

">
No, Go Back 2. DeleteMaterialServlet.html 此页面用于确认是否要删除物资,同样会携带要删除物资的 id 信息向 deleteMaterial 这个 Servlet 发送请求。 html Delete Material Confirmation

Confirm Material Deletion

Are you sure you want to delete the material with ID: <%= request.getParameter("id") %>?

">
No, Go Back 注意事项 在实际应用中,需要确保在 admin.jsp 页面中点击删除操作时,正确传递要删除记录的 id 到这两个确认页面。例如,在 admin.jsp 中显示仓库列表时,删除链接可以这样写: jsp Delete 其中 warehouse.getId() 是获取当前仓库记录的 id。物资列表的删除链接同理。 代码中的 <%= ... %> 是 JSP 的表达式语法,用于在 HTML 页面中嵌入 Java 代码并输出结果。在使用这些页面时,要确保 JSP 环境正常运行。 对于数据库连接部分,上述代码里的数据库连接信息(如数据库名、用户名、密码等)需要根据实际情况进行修改。同时,在实际开发中,建议使用连接池来管理数据库连接,以提高性能和资源利用率。 login.html再写好点 以下是一个优化后的 login.html 文件,在界面设计上更加美观,同时添加了一些交互提示和样式,提升用户体验。 html <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>登录页面</title> <style> body { font-family: Arial, sans-serif; background-color: #f4f4f4; display: flex; justify-content: center; align-items: center; height: 100vh; margin: 0; }
    .login-container {
        background-color: #fff;
        padding: 20px;
        border-radius: 8px;
        box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
        width: 300px;
    }

    .login-container h2 {
        text-align: center;
        margin-bottom: 20px;
    }

    .form-group {
        margin-bottom: 15px;
    }

    .form-group label {
        display: block;
        margin-bottom: 5px;
    }

    .form-group input {
        width: 100%;
        padding: 8px;
        box-sizing: border-box;
        border: 1px solid #ccc;
        border-radius: 4px;
    }

    .form-group input[type="submit"] {
        background-color: #007BFF;
        color: #fff;
        cursor: pointer;
    }

    .form-group input[type="submit"]:hover {
        background-color: #0056b3;
    }

    .error-message {
        color: red;
        text-align: center;
        margin-top: 10px;
    }
</style>
</head> <body> <div class="login-container"> <h2>用户登录</h2> <form action="login" method="post"> <div class="form-group"> <label for="username">用户名</label> <input type="text" id="username" name="username" required> </div> <div class="form-group"> <label for="password">密码</label> <input type="password" id="password" name="password" required> </div> <div class="form-group"> <input type="submit" value="登录"> </div> </form> <% if (request.getParameter("error") != null) { %> <p class="error-message">用户名或密码错误,请重试。</p> <% } %> </div> </body>

优化说明:
布局优化:
使用 flexbox 布局将登录框居中显示在页面中央,提升整体美观度和用户体验。
为登录框添加了 box-shadow 效果,使其有一定的立体感。
样式优化:
统一了字体,使用 Arial 字体,使页面文字更加清晰易读。
为输入框和按钮添加了圆角效果,使界面更加柔和。
为按钮添加了 :hover 效果,当鼠标悬停在按钮上时,按钮颜色会发生变化,增强交互性。
错误提示优化:
将错误提示信息居中显示,并设置为红色,使其更加醒目。
响应式设计:
使用 meta 标签设置了视口,确保页面在不同设备上都能有良好的显示效果。
通过这些优化,登录页面的视觉效果和用户体验都得到了显著提升。

posted @ 2025-03-05 21:24  YANGzLIN...11  阅读(148)  评论(0)    收藏  举报