21xrx.com
2024-09-17 03:37:24 Tuesday
登录
文章检索 我的文章 写文章
我最近在学习javaweb
2023-06-15 07:42:15 深夜i     --     --
javaweb 简单管理系统 代码

我最近在学习javaweb,尝试着写了一个简单的管理系统。这个管理系统仅有几个基础功能,但也可以帮助我更好地理解Java语言的运用。今天我想和大家分享一下这个系统的关键代码。

首先,我在Eclipse中创建了一个动态Web项目。在这个项目中,我新建了三个Java类:User(用户)、Product(产品)和DBUtil(数据库工具类)。User和Product都继承了一个Base类,该类提供了关于ID和Name的公共属性。

以下是我的User和Product类:


public class User extends Base {

  private String password;

  private String email;

  public String getPassword()

    return password;

  

  public void setPassword(String password)

    this.password = password;

  

  public String getEmail()

    return email;

  

  public void setEmail(String email)

    this.email = email;

  

}


public class Product extends Base {

  private String description;

  private int price;

  public String getDescription()

    return description;

  

  public void setDescription(String description)

    this.description = description;

  

  public int getPrice()

    return price;

  

  public void setPrice(int price)

    this.price = price;

  

}

我使用了MySQL数据库来存储数据,所以我创建了一个DBUtil类,来建立与数据库的连接以及执行SQL语句。以下是我的DBUtil类:


public class DBUtil {

  // 数据库连接

  public static Connection getConnection() {

    String url = "jdbc:mysql://localhost:3306/test";

    String username = "root";

    String password = "password";

    try {

      Class.forName("com.mysql.jdbc.Driver");

      Connection conn = DriverManager.getConnection(url, username, password);

      return conn;

    } catch (ClassNotFoundException | SQLException e) {

      e.printStackTrace();

    }

    return null;

  }

  // 执行查询语句

  public static ResultSet executeQuery(String sql) {

    try {

      Connection conn = getConnection();

      Statement stmt = conn.createStatement();

      ResultSet rs = stmt.executeQuery(sql);

      return rs;

    } catch (SQLException e) {

      e.printStackTrace();

    }

    return null;

  }

  // 执行更新语句

  public static boolean executeUpdate(String sql) {

    try {

      Connection conn = getConnection();

      Statement stmt = conn.createStatement();

      int count = stmt.executeUpdate(sql);

      return count > 0;

    } catch (SQLException e) {

      e.printStackTrace();

    }

    return false;

  }

}

在这个简单的管理系统中,我只实现了两个基本功能:用户的新增和产品的查询。在我的Servlet类中,我实现了对用户和产品的增加和查找操作。以下是我的Servlet类:


public class UserServlet extends HttpServlet {

  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    doPost(request, response);

  }

  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    request.setCharacterEncoding("UTF-8");

    response.setCharacterEncoding("UTF-8");

    String method = request.getParameter("method");

    if("add".equals(method)) {

      addUser(request, response);

    }

  }

  private void addUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    User user = new User();

    user.setName(request.getParameter("name"));

    user.setPassword(request.getParameter("password"));

    user.setEmail(request.getParameter("email"));

    boolean result = DBUtil.executeUpdate("insert into user(name,password,email) values('"+user.getName()+"','"+user.getPassword()+"','"+user.getEmail()+"')");

    if(result) {

      request.getRequestDispatcher("/success.jsp").forward(request, response);

    } else {

      request.getRequestDispatcher("/fail.jsp").forward(request, response);

    }

  }

}

public class ProductServlet extends HttpServlet {

  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    doPost(request, response);

  }

  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    request.setCharacterEncoding("UTF-8");

    response.setCharacterEncoding("UTF-8");

    String method = request.getParameter("method");

    if("search".equals(method)) {

      searchProduct(request, response);

    }

  }

  private void searchProduct(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    String searchName = request.getParameter("searchName");

    ResultSet rs = DBUtil.executeQuery("select * from product where name='"+searchName+"'");

    List list = new ArrayList<>();

    try {

      while(rs.next()) {

        Product product = new Product();

        product.setId(rs.getInt("id"));

        product.setName(rs.getString("name"));

        product.setDescription(rs.getString("description"));

        product.setPrice(rs.getInt("price"));

        list.add(product);

      }

      request.setAttribute("list", list);

      request.getRequestDispatcher("/product.jsp").forward(request, response);

    } catch (SQLException e) {

      e.printStackTrace();

    }

  }

}

使用Servlet在前端页面上实现了用户的新增和产品的查询,这两项业务相对简单,足够了解Java语言在javaweb中的运用,通过实例加深对Java的理解。

以上就是我写的javaweb简单管理系统的代码,虽然只是一个简单的小项目,但也让我收获了不少,希望对那些和我一样正在学习javaweb的初学者对javaweb有更深入的了解与学习。

  
  

评论区

{{item['qq_nickname']}}
()
回复
回复
    相似文章