要开发一个简单的JSP仓库设备管理系统,核心步骤包括:数据库设计、JSP页面编写、Servlet编写、以及与数据库的连接和操作。本文将详细介绍如何实现这些步骤,并为你提供一些示例代码。
在开始编写代码之前,首先需要设计数据库。可以使用MySQL数据库来存储设备信息。创建一个名为warehouse_management
的数据库,并在其中创建一个名为equipment
的表。该表包含以下字段:
CREATE DATABASE warehouse_management;
USE warehouse_management;
CREATE TABLE equipment (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
type VARCHAR(100) NOT NULL,
quantity INT NOT NULL,
location VARCHAR(100) NOT NULL
);
这个表将存储设备的ID、名称、类型、数量和位置等信息。
在Web应用程序中,我们将使用JSP页面来显示和管理设备信息。首先创建一个名为index.jsp
的文件,用于显示设备列表:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.sql.*, java.util.*" %>
<!DOCTYPE html>
<html>
<head>
<title>仓库设备管理系统</title>
</head>
<body>
<h1>设备列表</h1>
<table border="1">
<tr>
<th>ID</th>
<th>名称</th>
<th>类型</th>
<th>数量</th>
<th>位置</th>
</tr>
<%
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/warehouse_management", "root", "password");
stmt = conn.createStatement();
rs = stmt.executeQuery("SELECT * FROM equipment");
while(rs.next()) {
%>
<tr>
<td><%= rs.getInt("id") %></td>
<td><%= rs.getString("name") %></td>
<td><%= rs.getString("type") %></td>
<td><%= rs.getInt("quantity") %></td>
<td><%= rs.getString("location") %></td>
</tr>
<%
}
} catch(Exception e) {
e.printStackTrace();
} finally {
if(rs != null) rs.close();
if(stmt != null) stmt.close();
if(conn != null) conn.close();
}
%>
</table>
<a href="addEquipment.jsp">添加设备</a>
</body>
</html>
这个页面将从数据库中检索设备信息并以表格形式显示。
为了添加设备,需要创建一个名为addEquipment.jsp
的文件:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>添加设备</title>
</head>
<body>
<h1>添加新设备</h1>
<form action="addEquipmentServlet" method="post">
名称: <input type="text" name="name"><br>
类型: <input type="text" name="type"><br>
数量: <input type="number" name="quantity"><br>
位置: <input type="text" name="location"><br>
<input type="submit" value="添加">
</form>
</body>
</html>
这个页面包含一个表单,用户可以在其中输入新设备的信息。
创建一个名为AddEquipmentServlet
的Servlet来处理表单提交并将设备信息插入数据库:
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
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("/addEquipmentServlet")
public class AddEquipmentServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String name = request.getParameter("name");
String type = request.getParameter("type");
int quantity = Integer.parseInt(request.getParameter("quantity"));
String location = request.getParameter("location");
Connection conn = null;
PreparedStatement pstmt = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/warehouse_management", "root", "password");
String sql = "INSERT INTO equipment (name, type, quantity, location) VALUES (?, ?, ?, ?)";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, name);
pstmt.setString(2, type);
pstmt.setInt(3, quantity);
pstmt.setString(4, location);
pstmt.executeUpdate();
} catch(Exception e) {
e.printStackTrace();
} finally {
if(pstmt != null) try { pstmt.close(); } catch(Exception e) {}
if(conn != null) try { conn.close(); } catch(Exception e) {}
}
response.sendRedirect("index.jsp");
}
}
这个Servlet将接受表单数据,并将其插入到数据库中,然后重定向回设备列表页面。
为了实现设备的更新和删除功能,需要分别创建updateEquipment.jsp
和deleteEquipmentServlet
。
updateEquipment.jsp
页面:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<!DOCTYPE html>
<html>
<head>
<title>更新设备</title>
</head>
<body>
<h1>更新设备信息</h1>
<%
int id = Integer.parseInt(request.getParameter("id"));
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
String name = "", type = "", location = "";
int quantity = 0;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/warehouse_management", "root", "password");
String sql = "SELECT * FROM equipment WHERE id = ?";
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, id);
rs = pstmt.executeQuery();
if(rs.next()) {
name = rs.getString("name");
type = rs.getString("type");
quantity = rs.getInt("quantity");
location = rs.getString("location");
}
} catch(Exception e) {
e.printStackTrace();
} finally {
if(rs != null) rs.close();
if(pstmt != null) pstmt.close();
if(conn != null) conn.close();
}
%>
<form action="updateEquipmentServlet" method="post">
<input type="hidden" name="id" value="<%= id %>">
名称: <input type="text" name="name" value="<%= name %>"><br>
类型: <input type="text" name="type" value="<%= type %>"><br>
数量: <input type="number" name="quantity" value="<%= quantity %>"><br>
位置: <input type="text" name="location" value="<%= location %>"><br>
<input type="submit" value="更新">
</form>
</body>
</html>
UpdateEquipmentServlet
代码:
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
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("/updateEquipmentServlet")
public class UpdateEquipmentServlet 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 type = request.getParameter("type");
int quantity = Integer.parseInt(request.getParameter("quantity"));
String location = request.getParameter("location");
Connection conn = null;
PreparedStatement pstmt = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/warehouse_management", "root", "password");
String sql = "UPDATE equipment SET name = ?, type = ?, quantity = ?, location = ? WHERE id = ?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, name);
pstmt.setString(2, type);
pstmt.setInt(3, quantity);
pstmt.setString(4, location);
pstmt.setInt(5, id);
pstmt.executeUpdate();
} catch(Exception e) {
e.printStackTrace();
} finally {
if(pstmt != null) try { pstmt.close(); } catch(Exception e) {}
if(conn != null) try { conn.close(); } catch(Exception e) {}
}
response.sendRedirect("index.jsp");
}
}
DeleteEquipmentServlet
代码:
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
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("/deleteEquipmentServlet")
public class DeleteEquipmentServlet 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"));
Connection conn = null;
PreparedStatement pstmt = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/warehouse_management", "root", "password");
String sql = "DELETE FROM equipment WHERE id = ?";
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, id);
pstmt.executeUpdate();
} catch(Exception e) {
e.printStackTrace();
} finally {
if(pstmt != null) try { pstmt.close(); } catch(Exception e) {}
if(conn != null) try { conn.close(); } catch(Exception e) {}
}
response.sendRedirect("index.jsp");
}
}
项目结构应该如下:
WarehouseManagement/
├── WebContent/
│ ├── addEquipment.jsp
│ ├── index.jsp
│ ├── updateEquipment.jsp
├── src/
│ ├── AddEquipmentServlet.java
│ ├── UpdateEquipmentServlet.java
│ ├── DeleteEquipmentServlet.java
├── build.xml
├── .classpath
├── .project
在web.xml
中配置Servlet:
<web-app>
<servlet>
<servlet-name>AddEquipmentServlet</servlet-name>
<servlet-class>AddEquipmentServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>AddEquipmentServlet</servlet-name>
<url-pattern>/addEquipmentServlet</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>UpdateEquipmentServlet</servlet-name>
<servlet-class>UpdateEquipmentServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>UpdateEquipmentServlet</servlet-name>
<url-pattern>/updateEquipmentServlet</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>DeleteEquipmentServlet</servlet-name>
<servlet-class>DeleteEquipmentServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DeleteEquipmentServlet</servlet-name>
<url-pattern>/deleteEquipmentServlet</url-pattern>
</servlet-mapping>
</web-app>
将项目部署到Tomcat服务器上,并启动服务器。访问http://localhost:8080/WarehouseManagement/index.jsp
查看和管理设备信息。
此仓库设备管理系统的代码示例展示了如何使用JSP和Servlet实现一个简单的CRUD操作。通过这个示例,你可以进一步扩展和完善系统,例如添加用户认证、日志记录、数据验证等功能。
构建一个简单的JSP仓库设备管理系统,涉及到多个方面,包括数据库设计、前端展示、后端逻辑处理等。以下是一个简化的示例代码,帮助你快速上手。
首先,设计一个简单的数据库表,用于存储设备信息。可以使用MySQL数据库,创建一个名为devices
的表。
CREATE TABLE devices (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(100) NOT NULL,
type VARCHAR(50) NOT NULL,
quantity INT NOT NULL,
location VARCHAR(100) NOT NULL
);
接下来,创建一个简单的JSP页面,用于展示设备信息和添加新设备。
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<!DOCTYPE html>
<html>
<head>
<title>仓库设备管理系统</title>
</head>
<body>
<h1>设备列表</h1>
<table border="1">
<tr>
<th>ID</th>
<th>设备名称</th>
<th>设备类型</th>
<th>数量</th>
<th>位置</th>
</tr>
<%
String url = "jdbc:mysql://localhost:3306/yourdatabase";
String user = "root";
String password = "yourpassword";
Connection con = DriverManager.getConnection(url, user, password);
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM devices");
while (rs.next()) {
%>
<tr>
<td><%= rs.getInt("id") %></td>
<td><%= rs.getString("name") %></td>
<td><%= rs.getString("type") %></td>
<td><%= rs.getInt("quantity") %></td>
<td><%= rs.getString("location") %></td>
</tr>
<%
}
rs.close();
stmt.close();
con.close();
%>
</table>
<h2>添加新设备</h2>
<form action="addDevice.jsp" method="post">
设备名称: <input type="text" name="name"><br>
设备类型: <input type="text" name="type"><br>
数量: <input type="number" name="quantity"><br>
位置: <input type="text" name="location"><br>
<input type="submit" value="添加设备">
</form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<%
String name = request.getParameter("name");
String type = request.getParameter("type");
int quantity = Integer.parseInt(request.getParameter("quantity"));
String location = request.getParameter("location");
String url = "jdbc:mysql://localhost:3306/yourdatabase";
String user = "root";
String password = "yourpassword";
Connection con = DriverManager.getConnection(url, user, password);
String sql = "INSERT INTO devices (name, type, quantity, location) VALUES (?, ?, ?, ?)";
PreparedStatement pstmt = con.prepareStatement(sql);
pstmt.setString(1, name);
pstmt.setString(2, type);
pstmt.setInt(3, quantity);
pstmt.setString(4, location);
pstmt.executeUpdate();
pstmt.close();
con.close();
response.sendRedirect("index.jsp");
%>
在你的项目中,文件结构可以如下:
/your-web-app
├── index.jsp
├── addDevice.jsp
└── WEB-INF
└── web.xml
在WEB-INF
文件夹下创建web.xml
文件,配置Servlet和JSP的访问。
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<servlet>
<servlet-name>jsp</servlet-name>
<servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class>
<init-param>
<param-name>modificationTestInterval</param-name>
<param-value>0</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jsp</servlet-name>
<url-pattern>*.jsp</url-pattern>
</servlet-mapping>
</web-app>
确保你已经正确配置了MySQL数据库,并将JDBC驱动放在你的项目中。你可以使用Tomcat等服务器运行该项目。在浏览器中访问http://localhost:8080/your-web-app/index.jsp
,你将看到设备列表和添加设备的表单。
以上是一个简单的JSP仓库设备管理系统的示例代码,涵盖了设备的展示和添加功能。你可以根据自己的需求,进一步扩展功能,如编辑和删除设备、用户权限管理等。
为了提升开发效率,推荐一个好用的低代码开发平台,5分钟即可搭建一个管理软件:
地址: https://www.informat.cn/(或直接右上角申请体验)x6aj1;
100+企业管理系统模板免费使用>>>无需下载,在线安装:
地址: https://www.informat.cn/(或直接右上角申请体验)7wtn5;
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系邮箱:hopper@cornerstone365.cn 处理,核实后本网站将在24小时内删除。