新建一个数据库stbdb,添加一个student表,为student表添加多条数据; 2、编写select.jsp、insert.jsp、update.jsp、delete.jsp页面,实现对student表进行增删改查的操作
时间: 2024-06-13 09:07:19 浏览: 117
以下是针对问题的回答:
1. 新建一个数据库stbdb,添加一个student表,为student表添加多条数据
```sql
-- 创建数据库
CREATE DATABASE stbdb;
-- 使用数据库
USE stbdb;
-- 创建student表
CREATE TABLE student (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(20) NOT NULL,
age INT NOT NULL,
gender VARCHAR(10) NOT NULL,
address VARCHAR(50) NOT NULL
);
-- 插入多条数据
INSERT INTO student (name, age, gender, address) VALUES
('张三', 18, '男', '北京市海淀区'),
('李四', 20, '女', '上海市浦东新区'),
('王五', 19, '男', '广州市天河区'),
('赵六', 21, '女', '深圳市福田区');
```
2. 编写select.jsp、insert.jsp、update.jsp、delete.jsp页面,实现对student表进行增删改查的操作
select.jsp页面代码:
```html
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<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/stbdb?useSSL=false&serverTimezone=UTC";
String user = "root";
String password = "123456";
Connection conn = DriverManager.getConnection(url, user, password);
// 查询数据
String sql = "SELECT * FROM student";
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
// 输出数据
while (rs.next()) {
int id = rs.getInt("id");
String name = rs.getString("name");
int age = rs.getInt("age");
String gender = rs.getString("gender");
String address = rs.getString("address");
out.println("<tr>");
out.println("<td>" + id + "</td>");
out.println("<td>" + name + "</td>");
out.println("<td>" + age + "</td>");
out.println("<td>" + gender + "</td>");
out.println("<td>" + address + "</td>");
out.println("</tr>");
}
// 关闭连接
rs.close();
stmt.close();
conn.close();
%>
</table>
</body>
</html>
```
insert.jsp页面代码:
```html
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>添加学生信息</title>
<script>
function checkForm() {
var name = document.getElementById("name").value;
var age = document.getElementById("age").value;
var gender = document.getElementById("gender").value;
var address = document.getElementById("address").value;
if (name == "" || age == "" || gender == "" || address == "") {
alert("请填写完整信息!");
return false;
}
return true;
}
</script>
</head>
<body>
<h1>添加学生信息</h1>
<form action="insert.jsp" method="post" onsubmit="return checkForm()">
<label>姓名:</label>
<input type="text" name="name" id="name"><br>
<label>年龄:</label>
<input type="text" name="age" id="age"><br>
<label>性别:</label>
<input type="text" name="gender" id="gender"><br>
<label>地址:</label>
<input type="text" name="address" id="address"><br>
<input type="submit" value="添加">
</form>
<%
// 判断是否提交表单
if (request.getMethod().equals("POST")) {
// 获取表单数据
String name = request.getParameter("name");
int age = Integer.parseInt(request.getParameter("age"));
String gender = request.getParameter("gender");
String address = request.getParameter("address");
// 连接数据库
String url = "jdbc:mysql://localhost:3306/stbdb?useSSL=false&serverTimezone=UTC";
String user = "root";
String password = "123456";
Connection conn = DriverManager.getConnection(url, user, password);
// 插入数据
String sql = "INSERT INTO student (name, age, gender, address) VALUES (?, ?, ?, ?)";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, name);
pstmt.setInt(2, age);
pstmt.setString(3, gender);
pstmt.setString(4, address);
pstmt.executeUpdate();
// 关闭连接
pstmt.close();
conn.close();
// 跳转到查询页面
response.sendRedirect("select.jsp");
}
%>
</body>
</html>
```
update.jsp页面代码:
```html
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>修改学生信息</title>
<script>
function checkForm() {
var name = document.getElementById("name").value;
var age = document.getElementById("age").value;
var gender = document.getElementById("gender").value;
var address = document.getElementById("address").value;
if (name == "" || age == "" || gender == "" || address == "") {
alert("请填写完整信息!");
return false;
}
return true;
}
</script>
</head>
<body>
<h1>修改学生信息</h1>
<form action="update.jsp" method="post" onsubmit="return checkForm()">
<label>ID:</label>
<input type="text" name="id" id="id"><br>
<label>姓名:</label>
<input type="text" name="name" id="name"><br>
<label>年龄:</label>
<input type="text" name="age" id="age"><br>
<label>性别:</label>
<input type="text" name="gender" id="gender"><br>
<label>地址:</label>
<input type="text" name="address" id="address"><br>
<input type="submit" value="修改">
</form>
<%
// 判断是否提交表单
if (request.getMethod().equals("POST")) {
// 获取表单数据
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 address = request.getParameter("address");
// 连接数据库
String url = "jdbc:mysql://localhost:3306/stbdb?useSSL=false&serverTimezone=UTC";
String user = "root";
String password = "123456";
Connection conn = DriverManager.getConnection(url, user, password);
// 更新数据
String sql = "UPDATE student SET name=?, age=?, gender=?, address=? WHERE id=?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, name);
pstmt.setInt(2, age);
pstmt.setString(3, gender);
pstmt.setString(4, address);
pstmt.setInt(5, id);
pstmt.executeUpdate();
// 关闭连接
pstmt.close();
conn.close();
// 跳转到查询页面
response.sendRedirect("select.jsp");
}
%>
</body>
</html>
```
delete.jsp页面代码:
```html
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>删除学生信息</title>
</head>
<body>
<h1>删除学生信息</h1>
<form action="delete.jsp" method="post">
<label>ID:</label>
<input type="text" name="id"><br>
<input type="submit" value="删除">
</form>
<%
// 判断是否提交表单
if (request.getMethod().equals("POST")) {
// 获取表单数据
int id = Integer.parseInt(request.getParameter("id"));
// 连接数据库
String url = "jdbc:mysql://localhost:3306/stbdb?useSSL=false&serverTimezone=UTC";
String user = "root";
String password = "123456";
Connection conn = DriverManager.getConnection(url, user, password);
// 删除数据
String sql = "DELETE FROM student WHERE id=?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, id);
pstmt.executeUpdate();
// 关闭连接
pstmt.close();
conn.close();
// 跳转到查询页面
response.sendRedirect("select.jsp");
}
%>
</body>
</html>
```
阅读全文