新建一个数据库stbdb,添加一个student表,为student表添加多条数据; 2、编写select.jsp、insert.jsp、update.jsp、delete.jsp页面,实现对student表进行增删改查的操作
时间: 2024-06-13 10:07:19 浏览: 95
jsp连接SQLserver数据库实现增删改查。项目、数据库、题目都准备齐全
3星 · 编辑精心推荐
以下是针对问题的回答:
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 contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.sql.*" %>
<%
// 数据库连接信息
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/stbdb";
String user = "root";
String password = "123456";
// 查询语句
String sql = "SELECT * FROM student";
// 执行查询
try {
Class.forName(driver);
Connection conn = DriverManager.getConnection(url, user, password);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
// 输出查询结果
out.println("<table border='1'>");
out.println("<tr><th>ID</th><th>姓名</th><th>年龄</th><th>性别</th><th>地址</th></tr>");
while (rs.next()) {
out.println("<tr>");
out.println("<td>" + rs.getInt("id") + "</td>");
out.println("<td>" + rs.getString("name") + "</td>");
out.println("<td>" + rs.getInt("age") + "</td>");
out.println("<td>" + rs.getString("gender") + "</td>");
out.println("<td>" + rs.getString("address") + "</td>");
out.println("</tr>");
}
out.println("</table>");
// 关闭连接
rs.close();
stmt.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
%>
```
insert.jsp页面代码:
```html
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.sql.*" %>
<%
// 数据库连接信息
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/stbdb";
String user = "root";
String password = "123456";
// 获取表单数据
String name = request.getParameter("name");
int age = Integer.parseInt(request.getParameter("age"));
String gender = request.getParameter("gender");
String address = request.getParameter("address");
// 插入语句
String sql = "INSERT INTO student (name, age, gender, address) VALUES (?, ?, ?, ?)";
// 执行插入
try {
Class.forName(driver);
Connection conn = DriverManager.getConnection(url, user, password);
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, name);
pstmt.setInt(2, age);
pstmt.setString(3, gender);
pstmt.setString(4, address);
pstmt.executeUpdate();
// 输出插入成功信息
out.println("<h3>插入成功!</h3>");
// 关闭连接
pstmt.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
%>
<form action="insert.jsp" method="post">
<label>姓名:</label><input type="text" name="name" required><br>
<label>年龄:</label><input type="number" name="age" required><br>
<label>性别:</label><input type="radio" name="gender" value="男" checked>男
<input type="radio" name="gender" value="女">女<br>
<label>地址:</label><input type="text" name="address" required><br>
<input type="submit" value="提交">
</form>
```
update.jsp页面代码:
```html
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.sql.*" %>
<%
// 数据库连接信息
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/stbdb";
String user = "root";
String password = "123456";
// 获取表单数据
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 sql = "UPDATE student SET name=?, age=?, gender=?, address=? WHERE id=?";
// 执行更新
try {
Class.forName(driver);
Connection conn = DriverManager.getConnection(url, user, password);
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();
// 输出更新成功信息
out.println("<h3>更新成功!</h3>");
// 关闭连接
pstmt.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
%>
<form action="update.jsp" method="post">
<label>ID:</label><input type="number" name="id" required><br>
<label>姓名:</label><input type="text" name="name" required><br>
<label>年龄:</label><input type="number" name="age" required><br>
<label>性别:</label><input type="radio" name="gender" value="男" checked>男
<input type="radio" name="gender" value="女">女<br>
<label>地址:</label><input type="text" name="address" required><br>
<input type="submit" value="提交">
</form>
```
delete.jsp页面代码:
```html
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.sql.*" %>
<%
// 数据库连接信息
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/stbdb";
String user = "root";
String password = "123456";
// 获取表单数据
int id = Integer.parseInt(request.getParameter("id"));
// 删除语句
String sql = "DELETE FROM student WHERE id=?";
// 执行删除
try {
Class.forName(driver);
Connection conn = DriverManager.getConnection(url, user, password);
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, id);
pstmt.executeUpdate();
// 输出删除成功信息
out.println("<h3>删除成功!</h3>");
// 关闭连接
pstmt.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
%>
<form action="delete.jsp" method="post">
<label>ID:</label><input type="number" name="id" required><br>
<input type="submit" value="提交">
</form>
```
阅读全文