1、新建一个数据库stbdb,添加一个student表,为student表添加多条数据; 2、编写select.jsp、insert.jsp、update.jsp、delete.jsp页面,实现对student表进行增删改查的操作
时间: 2024-06-13 18:07:19 浏览: 107
以下是针对问题的回答:
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 driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/stbdb";
String user = "root";
String password = "123456";
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
// 加载数据库驱动
Class.forName(driver);
// 获取数据库连接
conn = DriverManager.getConnection(url, user, password);
// 创建Statement对象
stmt = conn.createStatement();
// 执行查询语句
rs = stmt.executeQuery("SELECT * FROM student");
// 遍历结果集
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");
%>
<tr>
<td><%=id%></td>
<td><%=name%></td>
<td><%=age%></td>
<td><%=gender%></td>
<td><%=address%></td>
</tr>
<%
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 关闭数据库连接
try {
if (rs != null) {
rs.close();
}
if (stmt != null) {
stmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
%>
</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 type="text/javascript">
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="insertAction.jsp" method="post" onsubmit="return checkForm()">
<table>
<tr>
<td>姓名:</td>
<td><input type="text" name="name" id="name"></td>
</tr>
<tr>
<td>年龄:</td>
<td><input type="text" name="age" id="age"></td>
</tr>
<tr>
<td>性别:</td>
<td>
<select name="gender" id="gender">
<option value="">请选择</option>
<option value="男">男</option>
<option value="女">女</option>
</select>
</td>
</tr>
<tr>
<td>地址:</td>
<td><input type="text" name="address" id="address"></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="添加"></td>
</tr>
</table>
</form>
</body>
</html>
```
insertAction.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>
<%
// 获取表单数据
String name = request.getParameter("name");
int age = Integer.parseInt(request.getParameter("age"));
String gender = request.getParameter("gender");
String address = request.getParameter("address");
// 数据库连接信息
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/stbdb";
String user = "root";
String password = "123456";
Connection conn = null;
Statement stmt = null;
try {
// 加载数据库驱动
Class.forName(driver);
// 获取数据库连接
conn = DriverManager.getConnection(url, user, password);
// 创建Statement对象
stmt = conn.createStatement();
// 执行插入语句
String sql = "INSERT INTO student (name, age, gender, address) VALUES ('" + name + "', " + age + ", '" + gender + "', '" + address + "')";
int result = stmt.executeUpdate(sql);
if (result > 0) {
%>
<h1>添加成功!</h1>
<%
} else {
%>
<h1>添加失败!</h1>
<%
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 关闭数据库连接
try {
if (stmt != null) {
stmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
%>
</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 type="text/javascript">
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>
<%
// 获取要修改的学生ID
int id = Integer.parseInt(request.getParameter("id"));
// 数据库连接信息
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/stbdb";
String user = "root";
String password = "123456";
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
// 加载数据库驱动
Class.forName(driver);
// 获取数据库连接
conn = DriverManager.getConnection(url, user, password);
// 创建Statement对象
stmt = conn.createStatement();
// 执行查询语句
rs = stmt.executeQuery("SELECT * FROM student WHERE id=" + id);
// 遍历结果集
if (rs.next()) {
String name = rs.getString("name");
int age = rs.getInt("age");
String gender = rs.getString("gender");
String address = rs.getString("address");
%>
<form action="updateAction.jsp" method="post" onsubmit="return checkForm()">
<table>
<tr>
<td>ID:</td>
<td><%=id%></td>
</tr>
<tr>
<td>姓名:</td>
<td><input type="text" name="name" id="name" value="<%=name%>"></td>
</tr>
<tr>
<td>年龄:</td>
<td><input type="text" name="age" id="age" value="<%=age%>"></td>
</tr>
<tr>
<td>性别:</td>
<td>
<select name="gender" id="gender">
<option value="">请选择</option>
<option value="男" <%=gender.equals("男") ? "selected" : ""%>>男</option>
<option value="女" <%=gender.equals("女") ? "selected" : ""%>>女</option>
</select>
</td>
</tr>
<tr>
<td>地址:</td>
<td><input type="text" name="address" id="address" value="<%=address%>"></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="修改"></td>
</tr>
</table>
</form>
<%
} else {
%>
<h1>学生不存在!</h1>
<%
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 关闭数据库连接
try {
if (rs != null) {
rs.close();
}
if (stmt != null) {
stmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
%>
</body>
</html>
```
updateAction.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>
<%
//
阅读全文