二、实验内容 1、新建一个数据库stbdb,添加一个student表,为student表添加多条数据; 2、编写select.jsp、insert.jsp、update.jsp、delete.jsp页面,实现对student表进行增删改查的操作 用MySQL实现
时间: 2024-06-13 17:07:13 浏览: 153
以下是用MySQL实现学生管理系统的步骤:
1. 创建数据库stbdb和student表,并添加多条数据。
```sql
CREATE DATABASE stbdb;
USE stbdb;
CREATE TABLE student (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(20) NOT NULL,
age INT NOT NULL,
gender VARCHAR(10) NOT NULL,
major VARCHAR(20) NOT NULL
);
INSERT INTO student (name, age, gender, major) VALUES ('张三', 20, '男', '计算机科学');
INSERT INTO student (name, age, gender, major) VALUES ('李四', 21, '女', '软件工程');
INSERT INTO student (name, age, gender, major) VALUES ('王五', 19, '男', '信息安全');
```
2. 编写select.jsp页面,实现对student表进行查询操作。
```jsp
<%@ page import="java.sql.*" %>
<%
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/stbdb", "root", "password");
stmt = conn.createStatement();
rs = stmt.executeQuery("SELECT * FROM student");
while (rs.next()) {
out.println("ID:" + rs.getInt("id") + "<br>");
out.println("姓名:" + rs.getString("name") + "<br>");
out.println("年龄:" + rs.getInt("age") + "<br>");
out.println("性别:" + rs.getString("gender") + "<br>");
out.println("专业:" + rs.getString("major") + "<br><br>");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (rs != null) rs.close();
if (stmt != null) stmt.close();
if (conn != null) conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
%>
```
3. 编写insert.jsp页面,实现对student表进行插入操作。
```jsp
<%@ page import="java.sql.*" %>
<%
request.setCharacterEncoding("UTF-8");
String name = request.getParameter("name");
int age = Integer.parseInt(request.getParameter("age"));
String gender = request.getParameter("gender");
String major = request.getParameter("major");
Connection conn = null;
Statement stmt = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/stbdb", "root", "password");
stmt = conn.createStatement();
String sql = "INSERT INTO student (name, age, gender, major) VALUES ('" + name + "', " + age + ", '" + gender + "', '" + major + "')";
stmt.executeUpdate(sql);
out.println("插入成功!");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (stmt != null) stmt.close();
if (conn != null) conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
%>
```
4. 编写update.jsp页面,实现对student表进行更新操作。
```jsp
<%@ page import="java.sql.*" %>
<%
request.setCharacterEncoding("UTF-8");
int id = Integer.parseInt(request.getParameter("id"));
String name = request.getParameter("name");
int age = Integer.parseInt(request.getParameter("age"));
String gender = request.getParameter("gender");
String major = request.getParameter("major");
Connection conn = null;
Statement stmt = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/stbdb", "root", "password");
stmt = conn.createStatement();
String sql = "UPDATE student SET name='" + name + "', age=" + age + ", gender='" + gender + "', major='" + major + "' WHERE id=" + id;
stmt.executeUpdate(sql);
out.println("更新成功!");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (stmt != null) stmt.close();
if (conn != null) conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
%>
```
5. 编写delete.jsp页面,实现对student表进行删除操作。
```jsp
<%@ page import="java.sql.*" %>
<%
int id = Integer.parseInt(request.getParameter("id"));
Connection conn = null;
Statement stmt = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/stbdb", "root", "password");
stmt = conn.createStatement();
String sql = "DELETE FROM student WHERE id=" + id;
stmt.executeUpdate(sql);
out.println("删除成功!");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (stmt != null) stmt.close();
if (conn != null) conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
%>
```
阅读全文