void add_books(books *head) { page_title("新增图书"); books *q,*p; q=head; while(q->next) q=q->next; p=(books *)malloc(LEN); printf("请输入序列号:"); scanf("%d",&p->id); printf("请输入日期:"); scanf("%d",&p->data); printf("请输入作者:"); scanf("%s",&p->author); printf("请输入书名:"); scanf("%s",&p->name); q->next=p; p->next=NULL; print(head); }
时间: 2024-02-14 16:21:39 浏览: 142
这段代码是用来实现一个新增图书功能的函数,其中books是一个结构体类型,包含了编号id,日期data,作者author和书名name等属性。函数的作用是在链表的末尾添加一本新书,需要先遍历整个链表找到最后一个节点,然后再将新书添加到链表的末尾。最后调用print函数打印出整个链表。在输入作者和书名时,使用%s来读入字符串,而不是使用%c。
相关问题
javaweb 在数据库中建立一张表:T- BOOK,包含图书ID,图书名称,图书价格。 编写图书模糊查询界面,录入图书名称的模糊资料,界面下方显示图书信息,要求交给servlet完成。
好的,我会为您详细解答。
首先,在数据库中创建 T_BOOK 表,包含图书ID,图书名称和图书价格三个字段,可以使用以下 SQL 语句:
```sql
CREATE TABLE T_BOOK (
id INT PRIMARY KEY,
name VARCHAR(100),
price DECIMAL(10,2)
);
```
接下来,您可以编写一个简单的 HTML 页面,用于输入图书名称的模糊查询条件,并将查询结果显示在页面上。
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>图书查询</title>
</head>
<body>
<form action="BookServlet" method="post">
<label for="name">图书名称:</label>
<input type="text" id="name" name="name">
<button type="submit">查询</button>
</form>
<hr>
<table>
<thead>
<tr>
<th>ID</th>
<th>名称</th>
<th>价格</th>
</tr>
</thead>
<tbody>
<%-- 查询结果展示在这里 --%>
</tbody>
</table>
</body>
</html>
```
注意,这里的表单提交方式为 POST,提交的目标为名为 BookServlet 的 Servlet。
接下来,编写 Servlet,用于处理表单提交并查询数据库。以下是一个简单的示例:
```java
import java.io.IOException;
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/BookServlet")
public class BookServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String name = request.getParameter("name");
List<Book> books = new ArrayList<>();
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "password");
String sql = "SELECT id, name, price FROM T_BOOK WHERE name LIKE ?";
ps = con.prepareStatement(sql);
ps.setString(1, "%" + name + "%");
rs = ps.executeQuery();
while (rs.next()) {
Book book = new Book();
book.setId(rs.getInt("id"));
book.setName(rs.getString("name"));
book.setPrice(rs.getBigDecimal("price"));
books.add(book);
}
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
} finally {
try {
if (rs != null) {
rs.close();
}
if (ps != null) {
ps.close();
}
if (con != null) {
con.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
request.setAttribute("books", books);
request.getRequestDispatcher("/book.jsp").forward(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public static class Book {
private int id;
private String name;
private BigDecimal price;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public BigDecimal getPrice() {
return price;
}
public void setPrice(BigDecimal price) {
this.price = price;
}
}
}
```
这个 Servlet 主要做了以下几件事情:
1. 从请求参数中获取图书名称的模糊查询条件;
2. 查询数据库,并将查询结果封装成 Book 对象;
3. 将查询结果保存到请求属性中,并转发到 book.jsp 页面进行展示。
最后,编写 book.jsp 页面,用于展示查询结果:
```html
<%@ page contentType="text/html;charset=UTF-8" %>
<%@ page import="com.example.BookServlet.Book" %>
<html>
<head>
<meta charset="UTF-8">
<title>图书列表</title>
</head>
<body>
<table>
<thead>
<tr>
<th>ID</th>
<th>名称</th>
<th>价格</th>
</tr>
</thead>
<tbody>
<c:forEach items="${books}" var="book">
<tr>
<td>${book.id}</td>
<td>${book.name}</td>
<td>${book.price}</td>
</tr>
</c:forEach>
</tbody>
</table>
</body>
</html>
```
在这个页面中,我们使用了 JSTL 标签库来遍历查询结果并展示在表格中。
以上就是一个简单的图书模糊查询界面的实现过程。希望可以帮到你!
设计实现一个图书管理系统。图书信息存放到一个数据库中,图书包含信息:图书号、图书名、作者、价格。请实现对图书的增删改查操作。采用JavaBean+Servlet+JSP+DAO的Web架构设计该系统
好的,下面是采用JavaBean+Servlet+JSP+DAO的Web架构设计的图书管理系统。
首先,我们需要建立一个图书Bean,包含以下字段:
```java
public class Book {
private int id;
private String name;
private String author;
private double price;
// 构造函数、getters 和 setters 略
}
```
接下来,我们可以编写一个简单的 DAO 类,使用 JDBC 来实现增删改查操作。例如:
```java
public class BookDAO {
public static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
public static final String DB_URL = "jdbc:mysql://localhost:3306/books";
public static final String USER = "root";
public static final String PASS = "password";
public List<Book> getAllBooks() throws SQLException {
List<Book> books = new ArrayList<>();
Connection conn = null;
Statement stmt = null;
try {
Class.forName(JDBC_DRIVER);
conn = DriverManager.getConnection(DB_URL, USER, PASS);
stmt = conn.createStatement();
String sql = "SELECT * FROM book";
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
Book book = new Book();
book.setId(rs.getInt("id"));
book.setName(rs.getString("name"));
book.setAuthor(rs.getString("author"));
book.setPrice(rs.getDouble("price"));
books.add(book);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
if (stmt != null) stmt.close();
if (conn != null) conn.close();
}
return books;
}
public void addBook(Book book) throws SQLException {
Connection conn = null;
PreparedStatement stmt = null;
try {
Class.forName(JDBC_DRIVER);
conn = DriverManager.getConnection(DB_URL, USER, PASS);
String sql = "INSERT INTO book (name, author, price) VALUES (?, ?, ?)";
stmt = conn.prepareStatement(sql);
stmt.setString(1, book.getName());
stmt.setString(2, book.getAuthor());
stmt.setDouble(3, book.getPrice());
stmt.executeUpdate();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
if (stmt != null) stmt.close();
if (conn != null) conn.close();
}
}
public void updateBook(int id, Book book) throws SQLException {
Connection conn = null;
PreparedStatement stmt = null;
try {
Class.forName(JDBC_DRIVER);
conn = DriverManager.getConnection(DB_URL, USER, PASS);
String sql = "UPDATE book SET name=?, author=?, price=? WHERE id=?";
stmt = conn.prepareStatement(sql);
stmt.setString(1, book.getName());
stmt.setString(2, book.getAuthor());
stmt.setDouble(3, book.getPrice());
stmt.setInt(4, id);
stmt.executeUpdate();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
if (stmt != null) stmt.close();
if (conn != null) conn.close();
}
}
public void deleteBook(int id) throws SQLException {
Connection conn = null;
PreparedStatement stmt = null;
try {
Class.forName(JDBC_DRIVER);
conn = DriverManager.getConnection(DB_URL, USER, PASS);
String sql = "DELETE FROM book WHERE id=?";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, id);
stmt.executeUpdate();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
if (stmt != null) stmt.close();
if (conn != null) conn.close();
}
}
}
```
这个 DAO 类使用 MySQL 数据库来存储图书信息,提供了四个方法:
- getAllBooks:获取所有图书信息
- addBook:添加一本新书
- updateBook:更新一本图书的信息
- deleteBook:删除一本图书
接下来,我们可以编写一个 Servlet 类,处理 HTTP 请求并调用 DAO 类的方法来实现增删改查操作。例如:
```java
public class BookServlet extends HttpServlet {
private BookDAO dao = new BookDAO();
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
List<Book> books = null;
try {
books = dao.getAllBooks();
} catch (SQLException e) {
e.printStackTrace();
}
request.setAttribute("books", books);
request.getRequestDispatcher("/book_list.jsp").forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String action = request.getParameter("action");
if ("add".equals(action)) {
String name = request.getParameter("name");
String author = request.getParameter("author");
double price = Double.parseDouble(request.getParameter("price"));
Book book = new Book();
book.setName(name);
book.setAuthor(author);
book.setPrice(price);
try {
dao.addBook(book);
} catch (SQLException e) {
e.printStackTrace();
}
} else if ("update".equals(action)) {
int id = Integer.parseInt(request.getParameter("id"));
String name = request.getParameter("name");
String author = request.getParameter("author");
double price = Double.parseDouble(request.getParameter("price"));
Book book = new Book();
book.setName(name);
book.setAuthor(author);
book.setPrice(price);
try {
dao.updateBook(id, book);
} catch (SQLException e) {
e.printStackTrace();
}
} else if ("delete".equals(action)) {
int id = Integer.parseInt(request.getParameter("id"));
try {
dao.deleteBook(id);
} catch (SQLException e) {
e.printStackTrace();
}
}
response.sendRedirect(request.getContextPath() + "/book");
}
}
```
这个 Servlet 类处理了 GET 和 POST 请求,GET 请求返回所有图书信息,POST 请求根据参数判断是添加、更新还是删除图书信息。
最后,我们可以编写一个 JSP 页面,展示所有图书信息,并提供表单来添加、更新和删除图书信息。例如:
```jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>图书管理系统</title>
</head>
<body>
<h1>图书列表</h1>
<table border="1">
<tr>
<th>图书号</th>
<th>图书名</th>
<th>作者</th>
<th>价格</th>
<th>操作</th>
</tr>
<c:forEach items="${books}" var="book">
<tr>
<td>${book.id}</td>
<td>${book.name}</td>
<td>${book.author}</td>
<td>${book.price}</td>
<td>
<form action="${pageContext.request.contextPath}/book" method="post">
<input type="hidden" name="id" value="${book.id}">
<input type="hidden" name="action" value="update">
<input type="text" name="name" value="${book.name}">
<input type="text" name="author" value="${book.author}">
<input type="text" name="price" value="${book.price}">
<input type="submit" value="更新">
</form>
<form action="${pageContext.request.contextPath}/book" method="post">
<input type="hidden" name="id" value="${book.id}">
<input type="hidden" name="action" value="delete">
<input type="submit" value="删除">
</form>
</td>
</tr>
</c:forEach>
</table>
<h1>添加图书</h1>
<form action="${pageContext.request.contextPath}/book" method="post">
<input type="hidden" name="action" value="add">
<label>图书名:</label><input type="text" name="name"><br>
<label>作者:</label><input type="text" name="author"><br>
<label>价格:</label><input type="text" name="price"><br>
<input type="submit" value="添加">
</form>
</body>
</html>
```
这个 JSP 页面展示了所有图书信息,并提供表单来添加、更新和删除图书信息。
最后,我们需要将这些组件组合在一起,例如在 web.xml 中配置 Servlet 和 JSP 的映射关系:
```xml
<servlet>
<servlet-name>BookServlet</servlet-name>
<servlet-class>com.example.BookServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>BookServlet</servlet-name>
<url-pattern>/book</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>book.jsp</welcome-file>
</welcome-file-list>
```
这个配置将 BookServlet 映射到 /book 路径,将 book.jsp 设置为默认欢迎页面。
这样,我们就完成了采用JavaBean+Servlet+JSP+DAO的Web架构设计的图书管理系统。您可以使用 Tomcat 等 Web 服务器来部署和运行这个系统,然后在浏览器中访问 /book 路径来使用它。
阅读全文