代码生成 使用JSP实现医生管理功能,Mysql作为后台数据库,功能包括科室信息 查询、添加医生信息和医生信息修改、删除功能。
时间: 2024-03-17 13:42:40 浏览: 88
好的,我可以给你提供一个简单的代码示例,帮助你实现医生管理功能。
首先,我们需要创建一个数据库,包括两个表:科室表(department)和医生表(doctor),表结构如下:
department表:
| 字段名 | 类型 | 是否为空 | 主键 | 备注 |
| ------------ | -------- | -------- | ---- | -------- |
| id | int | not null | 是 | 科室编号 |
| name | varchar | not null | | 科室名称 |
| description | varchar | | | 科室描述 |
doctor表:
| 字段名 | 类型 | 是否为空 | 主键 | 备注 |
| ------------ | -------- | -------- | ---- | ---------- |
| id | int | not null | 是 | 医生编号 |
| name | varchar | not null | | 医生姓名 |
| title | varchar | | | 医生职称 |
| departmentId | int | not null | | 所属科室id |
接下来,我们需要在JSP页面中实现医生管理功能。代码如下:
1. 查询科室信息
```
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.sql.*" %>
<html>
<head>
<title>查询科室信息</title>
</head>
<body>
<form action="department.jsp" method="post">
<label for="name">科室名称:</label>
<input type="text" name="name" id="name">
<input type="submit" value="查询">
</form>
<table border="1">
<thead>
<tr>
<th>科室编号</th>
<th>科室名称</th>
<th>科室描述</th>
</tr>
</thead>
<tbody>
<%
String name = request.getParameter("name");
String sql = "select * from department";
if (name != null && !name.equals("")) {
sql += " where name like '%" + name + "%'";
}
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "123456");
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
%>
<tr>
<td><%=rs.getInt("id")%></td>
<td><%=rs.getString("name")%></td>
<td><%=rs.getString("description")%></td>
</tr>
<%
}
rs.close();
stmt.close();
conn.close();
%>
</tbody>
</table>
</body>
</html>
```
2. 添加医生信息
```
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.sql.*" %>
<html>
<head>
<title>添加医生信息</title>
</head>
<body>
<form action="addDoctor.jsp" method="post">
<label for="name">医生姓名:</label>
<input type="text" name="name" id="name"><br>
<label for="title">医生职称:</label>
<input type="text" name="title" id="title"><br>
<label for="departmentId">所属科室:</label>
<select name="departmentId" id="departmentId">
<%
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "123456");
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select * from department");
while (rs.next()) {
%>
<option value="<%=rs.getInt("id")%>"><%=rs.getString("name")%></option>
<%
}
rs.close();
stmt.close();
conn.close();
%>
</select><br>
<input type="submit" value="添加">
</form>
</body>
</html>
```
```
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.sql.*" %>
<html>
<head>
<title>添加医生信息</title>
</head>
<body>
<%
String name = request.getParameter("name");
String title = request.getParameter("title");
int departmentId = Integer.parseInt(request.getParameter("departmentId"));
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "123456");
PreparedStatement pstmt = conn.prepareStatement("insert into doctor (name, title, departmentId) values (?, ?, ?)");
pstmt.setString(1, name);
pstmt.setString(2, title);
pstmt.setInt(3, departmentId);
pstmt.executeUpdate();
pstmt.close();
conn.close();
%>
<script>
alert("添加成功!");
location.href = "doctor.jsp";
</script>
</body>
</html>
```
3. 修改医生信息
```
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.sql.*" %>
<html>
<head>
<title>修改医生信息</title>
</head>
<body>
<form action="updateDoctor.jsp" method="post">
<input type="hidden" name="id" value="<%=request.getParameter("id")%>">
<label for="name">医生姓名:</label>
<input type="text" name="name" id="name" value="<%=rs.getString("name")%>"><br>
<label for="title">医生职称:</label>
<input type="text" name="title" id="title" value="<%=rs.getString("title")%>"><br>
<label for="departmentId">所属科室:</label>
<select name="departmentId" id="departmentId">
<%
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "123456");
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select * from department");
while (rs.next()) {
%>
<option value="<%=rs.getInt("id")%>" <%=rs.getInt("id")==doctor.getDepartmentId()?"selected":""%>><%=rs.getString("name")%></option>
<%
}
rs.close();
stmt.close();
conn.close();
%>
</select><br>
<input type="submit" value="保存">
</form>
</body>
</html>
```
```
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.sql.*" %>
<html>
<head>
<title>修改医生信息</title>
</head>
<body>
<%
int id = Integer.parseInt(request.getParameter("id"));
String name = request.getParameter("name");
String title = request.getParameter("title");
int departmentId = Integer.parseInt(request.getParameter("departmentId"));
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "123456");
PreparedStatement pstmt = conn.prepareStatement("update doctor set name=?, title=?, departmentId=? where id=?");
pstmt.setString(1, name);
pstmt.setString(2, title);
pstmt.setInt(3, departmentId);
pstmt.setInt(4, id);
pstmt.executeUpdate();
pstmt.close();
conn.close();
%>
<script>
alert("保存成功!");
location.href = "doctor.jsp";
</script>
</body>
</html>
```
4. 删除医生信息
```
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.sql.*" %>
<html>
<head>
<title>删除医生信息</title>
</head>
<body>
<%
int id = Integer.parseInt(request.getParameter("id"));
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "123456");
PreparedStatement pstmt = conn.prepareStatement("delete from doctor where id=?");
pstmt.setInt(1, id);
pstmt.executeUpdate();
pstmt.close();
conn.close();
%>
<script>
alert("删除成功!");
location.href = "doctor.jsp";
</script>
</body>
</html>
```
以上代码仅供参考,实际开发中需要根据具体需求进行修改和优化。希望能对你有所帮助!
阅读全文