用jsp编写一个宠物增加修改删除代码
时间: 2023-08-02 07:11:43 浏览: 15
好的,以下是一个简单的宠物信息增删改查的JSP代码示例:
首先是添加宠物信息的代码,假设我们有一个Pet表,其中有id、name、age、type和gender等字段:
```
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<%
request.setCharacterEncoding("UTF-8"); // 设置请求编码为UTF-8
String name = request.getParameter("name"); // 获取表单提交的数据
String age = request.getParameter("age");
String type = request.getParameter("type");
String gender = request.getParameter("gender");
Connection conn = null;
PreparedStatement stmt = null;
try {
Class.forName("com.mysql.jdbc.Driver"); // 加载MySQL驱动
// 建立数据库连接
String url = "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC";
String user = "root";
String password = "123456";
conn = DriverManager.getConnection(url, user, password);
// 构造SQL语句
String sql = "INSERT INTO Pet(name, age, type, gender) VALUES(?, ?, ?, ?)";
stmt = conn.prepareStatement(sql);
// 设置参数
stmt.setString(1, name);
stmt.setInt(2, Integer.parseInt(age));
stmt.setString(3, type);
stmt.setString(4, gender);
// 执行SQL语句
int rows = stmt.executeUpdate();
if (rows > 0) {
out.print("添加成功!");
} else {
out.print("添加失败!");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// 关闭数据库连接
try {
if (stmt != null) {
stmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
%>
```
然后是修改宠物信息的代码,假设我们要根据id来修改宠物信息:
```
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<%
request.setCharacterEncoding("UTF-8"); // 设置请求编码为UTF-8
int id = Integer.parseInt(request.getParameter("id")); // 获取表单提交的数据
String name = request.getParameter("name");
String age = request.getParameter("age");
String type = request.getParameter("type");
String gender = request.getParameter("gender");
Connection conn = null;
PreparedStatement stmt = null;
try {
Class.forName("com.mysql.jdbc.Driver"); // 加载MySQL驱动
// 建立数据库连接
String url = "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC";
String user = "root";
String password = "123456";
conn = DriverManager.getConnection(url, user, password);
// 构造SQL语句
String sql = "UPDATE Pet SET name=?, age=?, type=?, gender=? WHERE id=?";
stmt = conn.prepareStatement(sql);
// 设置参数
stmt.setString(1, name);
stmt.setInt(2, Integer.parseInt(age));
stmt.setString(3, type);
stmt.setString(4, gender);
stmt.setInt(5, id);
// 执行SQL语句
int rows = stmt.executeUpdate();
if (rows > 0) {
out.print("修改成功!");
} else {
out.print("修改失败!");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// 关闭数据库连接
try {
if (stmt != null) {
stmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
%>
```
最后是删除宠物信息的代码,同样是根据id来删除:
```
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<%
request.setCharacterEncoding("UTF-8"); // 设置请求编码为UTF-8
int id = Integer.parseInt(request.getParameter("id")); // 获取表单提交的数据
Connection conn = null;
PreparedStatement stmt = null;
try {
Class.forName("com.mysql.jdbc.Driver"); // 加载MySQL驱动
// 建立数据库连接
String url = "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC";
String user = "root";
String password = "123456";
conn = DriverManager.getConnection(url, user, password);
// 构造SQL语句
String sql = "DELETE FROM Pet WHERE id=?";
stmt = conn.prepareStatement(sql);
// 设置参数
stmt.setInt(1, id);
// 执行SQL语句
int rows = stmt.executeUpdate();
if (rows > 0) {
out.print("删除成功!");
} else {
out.print("删除失败!");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// 关闭数据库连接
try {
if (stmt != null) {
stmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
%>
```
另外,还需要编写一个查询宠物信息的代码,用于显示宠物列表、编辑宠物信息等功能。具体实现方式可以使用JDBC连接数据库,构造SQL语句查询Pet表中的数据,并将结果以表格的形式显示在页面上。
相关推荐











对于JSP页面的编写,你需要掌握HTML、CSS和Java语言等知识,并且了解JSP的基本语法和标签。下面是一个简单的JSP页面示例,供你参考:
jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>新闻列表</title>
</head>
<body>
新闻列表
标题
分类
发布时间
<%
List<News> newsList = (List<News>) request.getAttribute("newsList");
for (News news : newsList) {
%>
<%= news.getTitle() %>
<%= news.getCategory() %>
<%= news.getCreateTime() %>
<% } %>
</body>
</html>
以上是一个简单的新闻列表页面,页面中展示了新闻标题、分类和发布时间等信息。在JSP页面中,你可以使用HTML标签和JSP标签混合编写,在JSP标签中可以嵌入Java代码,实现动态数据的展示和交互。
在上面的示例代码中,我们使用了JSP中的Scriptlet标签<% %>来嵌入Java代码,通过request.getAttribute()方法获取数据,并且通过循环展示列表数据。同时,我们还使用了EL表达式${ }来简化代码,例如${ news.getTitle() }可以简写为${news.title}。
当然,这只是一个简单的示例,实际项目中你需要根据具体需求来编写JSP页面,同时需要注意页面的布局、样式和交互等方面,保证页面的美观和用户友好性。



很抱歉,我是一名语言模型,无法编写实际的代码。不过,以下是一个简单的图书管理系统的JSP代码示例,供参考:
index.jsp
<%@ page contentType="text/html; charset=UTF-8" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>图书管理系统</title>
</head>
<body>
欢迎使用图书管理系统
查看图书列表
添加新书
</body>
</html>
bookList.jsp
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="com.example.Book" %>
<%
// 模拟从数据库中获取图书列表
ArrayList<Book> bookList = new ArrayList<>();
bookList.add(new Book(1, "Java编程思想", "Bruce Eckel", 59.99));
bookList.add(new Book(2, "Head First Java", "Kathy Sierra", 49.99));
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>图书列表</title>
</head>
<body>
图书列表
ID
书名
作者
价格
<%
for (Book book : bookList) {
%>
<%= book.getId() %>
<%= book.getName() %>
<%= book.getAuthor() %>
<%= book.getPrice() %>
<%
}
%>
返回首页
</body>
</html>
addBook.jsp
<%@ page contentType="text/html; charset=UTF-8" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>添加图书</title>
</head>
<body>
添加新书
<form method="post" action="addBookAction.jsp">
<label for="name">书名:</label><input type="text" name="name" id="name">
<label for="author">作者:</label><input type="text" name="author" id="author">
<label for="price">价格:</label><input type="number" name="price" id="price">
<input type="submit" value="添加">
</form>
返回首页
</body>
</html>
addBookAction.jsp
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ page import="com.example.Book" %>
<%
// 获取表单提交的数据
String name = request.getParameter("name");
String author = request.getParameter("author");
double price = Double.parseDouble(request.getParameter("price"));
// 模拟将新书添加到数据库中
Book newBook = new Book(3, name, author, price);
// 跳转回图书列表页
response.sendRedirect("bookList.jsp");
%>
这些代码只是一个示例,实际的图书管理系统可能需要更多的功能和代码。此外,需要注意安全性和性能方面的问题,例如防止SQL注入等。


