addBooks.jsp页面代码:↓

 <%--
Created by IntelliJ IDEA.
User: NFS
Date: 2019-7-12
Time: 14:30
To change this template use File | Settings | File Templates.
--%> <%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>添加书籍</title> <style>
label {
display: flex;
margin-bottom: 5px;
}
label > span {
flex: 0 0 80px;
}
label > input {
flex: 0 0 200px;
}
</style>
</head>
<body> <h3>书籍信息</h3>
<div>
<form method="post" action="add">
<label>
<span>书名:</span>
<input name="book_name">
</label>
<label>
<span>作者:</span>
<input name="author">
</label>
<label>
<span>数量:</span>
<input name="number">
</label>
<label>
<span>价格:</span>
<input name="price">
</label>
<label>
<span>出版社:</span>
<input name="pub">
</label>
<input type="submit" value="添加书籍">
</form>
</div> <footer>
<a href="<%=request.getContextPath()%>/books/lst">返回首页</a>
</footer>
</body>
</html>

    addBooks.jsp  对应的servlet: addBooks.java↓

 package BookSystem.CRUD;
import BookSystem.Other.DButil; import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException; @WebServlet("/books/add")
public class AddBooks extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 获取添加的页面 req.getRequestDispatcher("/Book/addBooks.jsp").forward(req, resp);
} @Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 提交保存数据
req.setCharacterEncoding("UTF-8");
resp.setCharacterEncoding("UTF-8");
//获取数据
String name=req.getParameter("book_name");
String author=req.getParameter("author");
int number =Integer.parseInt(req.getParameter("number"));
float price = Float.parseFloat(req.getParameter("price"));
String pub = req.getParameter("pub");
Connection connection= new DButil().getConnection();
PreparedStatement psmt=null;
try {
//插入数据
psmt=connection.prepareStatement("insert into BookInfo values (?,?,?,?,?)"); psmt.setString(1,name);
psmt.setString(2,author);
psmt.setInt(3,number);
psmt.setFloat(4,price);
psmt.setString(5,pub);
psmt.executeUpdate();
}catch (SQLException e){
e.printStackTrace();
}finally {
try {
connection.close();
psmt.close();
} catch (SQLException e) {
e.printStackTrace();
} }
resp.sendRedirect(req.getContextPath()+"/books/lst");
}
}

      注:该整个CRUD不展示效果图,整体CSS应当有属于自己的

————————————————————————————————————————————————————————————

05-11 19:45