Jsp+Servlet+Mysql realizes book management system

Based on some needs, I tried to develop a book management system, in addition to registration and login, the basic functions of adding, deleting, modifying and checking the system were realized.

Project Name: Book Information Management System

Implementation method: Jsp+Servlet+Mysql

Tools: eclipse, mysql, Navicat for MySQL

Interface display:

main page:

Add page:

Modify the page:
database:
Project directory form:

The BookDAO.java used by dao to store the interface in this project is used to define the interface, and its content includes the methods of adding, deleting, modifying and checking.
DbHelper is used to connect data, including database connection address, database user name, database password, etc. Entity is used to store entity classes, and the implementation of abstract methods must pass through entity classes.
In the web package, I store the servlet file. The servlet file is used to receive the front-end request and call the dao interface to realize the front-end request, which is equivalent to the controller.

accomplish:

Query all book information

Since there is no value to query for all book information, you can directly call getAllBook() in dao with Java script in jsp. The code is as follows:

    <%
   BookDAO dao = new BookDAO(); 
   List<Book> books = dao.getAllBook();
   for(int i = 0;i<books.size();i++){
   Book book = books.get(i);		
	%>
    
    <tr>
    <td><%=book.getId() %></td>
    <td><%=book.getName()%></td>
    <td><%=book.getAuthor()%></td>
    <td><%=book.getCategory()%></td>
    
    <td><a href="DeleteServlet?id=<%=book.getId() %>">删除</a>|<a href="update.jsp?id=<%=book.getId() %>">修改</a></td>
    </tr>
    	                 		<%
					}
					%>

复制代码

Increase

Jsp uses post to pass the information entered by the user to AddServlet, and Addservlet uses getParameter to get the value of jsp, for example:

String name = req.getParameter("name");
复制代码

After that, Addservlet is saved to the book and calls the addBook() method in dao. The operation of the database is realized through the interface of dao. The calling code is as follows:

Book book = new Book();
book.setId(Integer.parseInt(id));
book.setName(name); 
book.setAuthor(author);
book.setCategory(category);
BookDAO dao = new BookDAO();
dao.addBook(book);
复制代码

When the operation on the database is successfully completed, I redirect to all.jsp to display all book information to see the information I have added. The code is as follows:

req.getRequestDispatcher("all.jsp").forward(req, resp);
复制代码

delete

When Jsp clicks on the delete operation, the id is obtained by DeleteServlet. Note here that the obtained id is of String type and needs to be converted into an integer type. The conversion is as follows:

String idStr = req.getParameter("id");
int id = Integer.valueOf(idStr);
复制代码

Then DeleteServlet calls dao to execute the corresponding method and delete the corresponding id. After the deletion is successful, it will redirect to all.jsp to see all the book information after deletion. The code is as follows:

BookDAO dao=new BookDAO();
dao.deleteBook(id);
req.getRequestDispatcher("AllServlet").forward(req, resp);
复制代码
req.getRequestDispatcher("all.jsp").forward(req, resp);
复制代码

Revise

When the jsp clicks the modification operation, the UpdateServlet obtains the modification information input by the user, then calls the updateBook() method in the dao, and redirects to all.jsp after completion. The calling code is as follows:

Book book = new Book();
book.setId(Integer.parseInt(id));
book.setName(name);
book.setAuthor(author);
book.setCategory(category);
BookDAO dao = new BookDAO();
dao.updateBook(book);
复制代码

Inquire

The query here I only wrote to query by book name.

When querying, jsp passes the name of the book to be queried to SelectServlet, SelectServlet gets the name of the book to be queried, executes the selectBook() method inside, and then the queried information is stored in the array list, and then obtained by JSP, the code is as follows :

String name = req.getParameter("name");	
BookDAO dao = new BookDAO();
List<Book> list = dao.selectBook(name);		
req.setAttribute("list", list);
req.getRequestDispatcher("queryshow.jsp").forward(req, resp);
复制代码

The Java script for jsp to get the list is as follows:

    ArrayList<Book> list = (ArrayList<Book>)request.getAttribute("list");
                       for(Book book:list){
								%>
    
    <tr>
    <td><%=book.getId() %></td>
    <td><%=book.getName()%></td>
    <td><%=book.getAuthor()%></td>
    <td><%=book.getCategory() %></td>
    
    <td><a href="DeleteServlet?id=<%=book.getId() %>">删除</a>|<a href="update.jsp?id=<%=book.getId() %>">修改</a></td>
    </tr>
    	         		<%
					}
				%>

复制代码

Conclusion: The realization of the library management system this time is a summary and study of the knowledge I have learned in the past for some reason. It took a long time to do this small project and encountered many problems, but in general It is still relatively smooth, this time sharing this article, I hope it will help you and me a little bit more or less.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=324090417&siteId=291194637