JAVAWEB实现增删查改(图书信息管理)之添加功能实现

Posted winton-nfs

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JAVAWEB实现增删查改(图书信息管理)之添加功能实现相关的知识,希望对你有一定的参考价值。

    addBooks.jsp页面代码:↓

 1 <%--
 2   Created by IntelliJ IDEA.
 3   User: NFS
 4   Date: 2019-7-12
 5   Time: 14:30
 6   To change this template use File | Settings | File Templates.
 7 --%>
 8 
 9 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
10 <html>
11 <head>
12     <title>添加书籍</title>
13 
14     <style>
15         label 
16             display: flex;
17             margin-bottom: 5px;
18         
19         label > span 
20             flex: 0 0 80px;
21         
22         label > input 
23             flex: 0 0 200px;
24         
25     </style>
26 </head>
27 <body>
28 
29 
30 <h3>书籍信息</h3>
31 <div>
32     <form method="post" action="add">
33         <label>
34             <span>书名:</span>
35             <input name="book_name">
36         </label>
37         <label>
38             <span>作者:</span>
39             <input name="author">
40         </label>
41         <label>
42             <span>数量:</span>
43             <input name="number">
44         </label>
45         <label>
46             <span>价格:</span>
47             <input name="price">
48         </label>
49         <label>
50             <span>出版社:</span>
51             <input name="pub">
52         </label>
53         <input type="submit" value="添加书籍">
54     </form>
55 </div>
56 
57 
58 
59 <footer>
60     <a href="<%=request.getContextPath()%>/books/lst">返回首页</a>
61 </footer>
62 </body>
63 </html>

 

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

 

 1 package BookSystem.CRUD;
 2 import BookSystem.Other.DButil;
 3 
 4 import javax.servlet.ServletException;
 5 import javax.servlet.annotation.WebServlet;
 6 import javax.servlet.http.HttpServlet;
 7 import javax.servlet.http.HttpServletRequest;
 8 import javax.servlet.http.HttpServletResponse;
 9 import java.io.IOException;
10 import java.sql.Connection;
11 import java.sql.PreparedStatement;
12 import java.sql.SQLException;
13 
14 @WebServlet("/books/add")
15 public class AddBooks extends HttpServlet 
16     @Override
17     protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException 
18         // 获取添加的页面
19 
20         req.getRequestDispatcher("/Book/addBooks.jsp").forward(req, resp);
21     
22 
23     @Override
24     protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException 
25         // 提交保存数据
26         req.setCharacterEncoding("UTF-8");
27         resp.setCharacterEncoding("UTF-8");
28         //获取数据
29         String name=req.getParameter("book_name");
30         String author=req.getParameter("author");
31         int number =Integer.parseInt(req.getParameter("number"));
32         float price = Float.parseFloat(req.getParameter("price"));
33         String pub = req.getParameter("pub");
34         Connection connection= new DButil().getConnection();
35         PreparedStatement psmt=null;
36         try 
37             //插入数据
38             psmt=connection.prepareStatement("insert  into  BookInfo values (?,?,?,?,?)");
39 
40             psmt.setString(1,name);
41             psmt.setString(2,author);
42             psmt.setInt(3,number);
43             psmt.setFloat(4,price);
44             psmt.setString(5,pub);
45             psmt.executeUpdate();
46         catch (SQLException e)
47             e.printStackTrace();
48         finally 
49             try 
50                 connection.close();
51                 psmt.close();
52              catch (SQLException e) 
53                 e.printStackTrace();
54             
55 
56         
57         resp.sendRedirect(req.getContextPath()+"/books/lst");
58     
59 

 

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

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

 

以上是关于JAVAWEB实现增删查改(图书信息管理)之添加功能实现的主要内容,如果未能解决你的问题,请参考以下文章

JAVAWEB实现增删查改(图书信息管理)之删除功能实现

JAVAWEB实现增删查改(图书信息管理)之修改功能实现

采用JAVA设计一个小型图书管理程序(只要增删查改)

VisualStudio 连接 MySql 实现增删查改

Django框架之图书管理系统

java实现简单的数据库的增删查改,并布局交互界面