JSP(JavaServer Pages)是一种基于Java技术的动态网页开发技术,它允许开发者在HTML或XML等静态页面中嵌入Java代码,从而实现动态内容的生成和交互。下面是一个简单的员工管理系统的JSP实现示例,包括基本的增删改查功能。
以下是一个简单的员工管理系统的JSP页面示例,包括员工列表展示和添加新员工的功能。
db.url=jdbc:mysql://localhost:3306/employee_db
db.username=root
db.password=rootimport java.sql.*;
import java.util.Properties;
public class DBUtil {
private static Connection conn = null;
public static Connection getConnection() {
if (conn != null) {
return conn;
}
try {
Properties props = new Properties();
props.load(DBUtil.class.getClassLoader().getResourceAsStream("db.properties"));
String url = props.getProperty("db.url");
String user = props.getProperty("db.username");
String password = props.getProperty("db.password");
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(url, user, password);
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
public static void close(Connection conn, Statement stmt, ResultSet rs) {
try {
if (rs != null) rs.close();
if (stmt != null) stmt.close();
if (conn != null) conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}<%@ page import="java.sql.*" %>
<%@ page import="java.util.*" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>员工列表</title>
</head>
<body>
<h1>员工列表</h1>
<table border="1">
<tr>
<th>ID</th>
<th>姓名</th>
<th>职位</th>
<th>操作</th>
</tr>
<%
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
conn = DBUtil.getConnection();
stmt = conn.createStatement();
rs = stmt.executeQuery("SELECT * FROM employees");
while (rs.next()) {
%>
<tr>
<td><%= rs.getInt("id") %></td>
<td><%= rs.getString("name") %></td>
<td><%= rs.getString("position") %></td>
<td>
<a href="editEmployee.jsp?id=<%= rs.getInt("id") %>">编辑</a>
<a href="deleteEmployee.jsp?id=<%= rs.getInt("id") %>">删除</a>
</td>
</tr>
<%
}
} catch (Exception e) {
e.printStackTrace();
} finally {
DBUtil.close(conn, stmt, rs);
}
%>
</table>
<a href="addEmployee.jsp">添加新员工</a>
</body>
</html><%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>添加新员工</title>
</head>
<body>
<h1>添加新员工</h1>
<form action="addEmployeeAction.jsp" method="post">
姓名:<input type="text" name="name"><br>
职位:<input type="text" name="position"><br>
<input type="submit" value="提交">
</form>
</body>
</html><%@ page import="java.sql.*" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>添加员工结果</title>
</head>
<body>
<%
String name = request.getParameter("name");
String position = request.getParameter("position");
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = DBUtil.getConnection();
String sql = "INSERT INTO employees (name, position) VALUES (?, ?)";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, name);
pstmt.setString(2, position);
pstmt.executeUpdate();
out.println("员工添加成功!");
} catch (Exception e) {
e.printStackTrace();
out.println("员工添加失败:" + e.getMessage());
} finally {
DBUtil.close(conn, pstmt, null);
}
%>
<a href="listEmployees.jsp">返回员工列表</a>
</body>
</html><%@ page contentType="text/html;charset=UTF-8" language="java" %>。通过以上步骤,你可以构建一个基本的员工管理系统。对于更复杂的功能和更好的性能优化,可以考虑使用MVC架构、ORM框架(如Hibernate)等技术。