18
今天想把javaweb重整一下,因为上学期学的不是很扎实,整图书管理系统
系统功能:主要实现图书的增删改查,我建了四层,如下图所示:

菜单:

添加图书信息:

管理图书信息:

今天主要完成图书信息的添加功能
entity层:
1 package entity;
2
3 public class Book {
4 private int id;
5 private String bianhao;
6 private String bname;
7 private String wname;
8 private String bhome;
9 private int num;
10 public void setId(int id) {
11 this.id=id;
12 }
13 public void setBianhao(String bianhao) {
14 this.bianhao=bianhao;
15 }
16 public void setBname(String bname) {
17 this.bname=bname;
18 }
19 public void setWname(String wname) {
20 this.wname=wname;
21 }
22 public void setBhome(String bhome) {
23 this.bhome=bhome;
24 }
25 public void setNum(int num) {
26 this.num=num;
27 }
28 public int getId() {
29 return this.id;
30 }
31 public String getBianhao() {
32 return this.bianhao;
33 }
34 public String getBname() {
35 return this.bname;
36 }
37 public String getWname() {
38 return this.wname;
39 }
40 public String getBhome() {
41 return this.bhome;
42 }
43 public int getNum() {
44 return this.num;
45 }
46 public Book() {}
47 public Book(int id,String bianhao,String bname,String wname,String bhome,int num) {
48 this.id=id;
49 this.bianhao=bianhao;
50 this.bname=bname;
51 this.wname=wname;
52 this.bhome=bhome;
53 this.num=num;
54 }
55 public Book(String bianhao,String bname,String wname,String bhome,int num) {
56 this.bianhao=bianhao;
57 this.bname=bname;
58 this.wname=wname;
59 this.bhome=bhome;
60 this.num=num;
61 }
62 }
DBUtil层:
1 package DBUtil;
2
3 import java.sql.Connection;
4 import java.sql.DriverManager;
5 import java.sql.PreparedStatement;
6 import java.sql.ResultSet;
7 import java.sql.SQLException;
8 import java.sql.Statement;
9
10 public class DBUtil {
11
12 public static String db_url = "jdbc:mysql://localhost:3306/xuanke?serverTimezone=GMT%2B8&useSSL=false";
13 public static String db_user = "root";
14 public static String db_pass = "0424wyhhxx";
15
16 public static Connection getConn () {
17 Connection conn = null;
18
19 try {
20 Class.forName("com.mysql.cj.jdbc.Driver");
21 conn = DriverManager.getConnection(db_url, db_user, db_pass);
22 } catch (Exception e) {
23 e.printStackTrace();
24 }
25
26 return conn;
27 }
28
29 public static void close (Statement state, Connection conn) {
30 if (state != null) {
31 try {
32 state.close();
33 } catch (SQLException e) {
34 e.printStackTrace();
35 }
36 }
37
38 if (conn != null) {
39 try {
40 conn.close();
41 } catch (SQLException e) {
42 e.printStackTrace();
43 }
44 }
45 }
46
47 public static void close (ResultSet rs, Statement state, Connection conn) {
48 if (rs != null) {
49 try {
50 rs.close();
51 } catch (SQLException e) {
52 e.printStackTrace();
53 }
54 }
55
56 if (state != null) {
57 try {
58 state.close();
59 } catch (SQLException e) {
60 e.printStackTrace();
61 }
62 }
63
64 if (conn != null) {
65 try {
66 conn.close();
67 } catch (SQLException e) {
68 e.printStackTrace();
69 }
70 }
71 }
72
73 public static void main(String[] args) throws SQLException {
74 Connection conn = getConn();
75 PreparedStatement pstmt = null;
76 ResultSet rs = null;
77 String sql ="select * from xuanke";
78 pstmt = conn.prepareStatement(sql);
79 rs = pstmt.executeQuery();
80 if(rs.next()){
81 System.out.println("成功");
82 }else{
83 System.out.println("失败");
84 }
85 }
86 }
dao层:
1 import java.sql.Connection;
2 import java.sql.PreparedStatement;
3 import java.sql.ResultSet;
4 import java.sql.SQLException;
5 import java.sql.Statement;
6 import java.util.ArrayList;
7 import java.util.List;
8
9 import DBUtil.DBUtil;
10 import entity.Book;
11 //添加新书信息
12 public boolean addBook(Book stu) {
13 Connection conn = DBUtil.getConn();
14 PreparedStatement pstmt = null;
15 boolean f = false;
16 int a = 0;
17 try {
18 String sql = "insert into Book(bianhao,bname,wname,bhome,num) value(?,?,?,?,?)";
19 pstmt = conn.prepareStatement(sql);
20 pstmt.setString(1, stu.getBianhao());
21 pstmt.setString(2, stu.getBname());
22 pstmt.setString(3, stu.getWname());
23 pstmt.setString(4, stu.getBhome());
24 pstmt.setLong(5, stu.getNum());
25 a = pstmt.executeUpdate();
26 } catch (SQLException e) {
27 e.printStackTrace();
28 } finally {
29 DBUtil.close(pstmt, conn);
30 }
31 if (a > 0)
32 f = true;
33
34 return f;
35 }
36 }
servlet层:
1 import java.io.IOException;
2 import java.util.List;
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
10 import dao.Dao;
11 import entity.Book;
12
13 @WebServlet("/Servlet")
14 public class Servlet extends HttpServlet {
15 private static final long serialVersionUID = 1L;
16 Dao dao = new Dao();
17
18 protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
19 req.setCharacterEncoding("utf-8");
20 String method = req.getParameter("method");
21 if ("addBook".equals(method)) {
22 addBook(req, resp);
23 }
24 }
25 private void addBook(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
26 req.setCharacterEncoding("utf-8");
27 String bianhao = req.getParameter("bianhao");
28 String bname = req.getParameter("bname");
29 String wname = req.getParameter("wname");
30 String bhome = req.getParameter("bhome");
31 int num = Integer.parseInt(req.getParameter("num"));
32 Book book = new Book(bianhao, bname, wname, bhome, num);
33 if (dao.addBook(book)) {
34 req.setAttribute("book", book);
35 req.setAttribute("message", "添加成功");
36 req.getRequestDispatcher("addBook.jsp").forward(req, resp);
37 } else {
38 req.setAttribute("message", "书籍信息重复,请重新输入");
39 req.getRequestDispatcher("addBook.jsp").forward(req, resp);
40 }
41 }
42 }
添加图书的JSP:
1 <%@ page language="java" contentType="text/html; charset=UTF-8"
2 pageEncoding="UTF-8"%>
3 <!DOCTYPE html>
4 <html>
5 <head>
6 <meta charset="UTF-8">
7 <title>添加图书信息</title>
8 <script>
9 </script>
10 </head>
11 <body>
12 <%
13 Object message = request.getAttribute("message");
14 if(message!=null && !"".equals(message)){
15
16 %>
17 <script type="text/javascript">
18 alert("<%=request.getAttribute("message")%>");
19 </script>
20 <%} %>
21 <table align="center" border="0px" cellpadding="10px" cellspacing="10px">
22 <form action="Servlet?method=addBook" method="post" onsubmit="return check()">
23 <tr>
24 <td><a href="guanli.jsp">返回菜单</a></td>
25 </tr>
26 <tr>
27 <td>图书编号:</td>
28 <td><input type="text" name="bianhao" id="bianhao"></td>
29 </tr>
30 <tr>
31 <td>书名:</td>
32 <td><input type="text" name="bname" id="bname"></td>
33 </tr>
34
35 <tr>
36 <td>作者名:</td>
37 <td><input type="text" name="wname" id="wname"></td>
38 </tr>
39 <tr>
40 <tr>
41 <td>出版社名称:</td>
42 <td><input type="text" name="bhome" id="bhome"></td>
43 </tr>
44 <tr>
45 <tr>
46 <td>可借阅数量:</td>
47 <td><input type="text" name="num" id="num"></td>
48 </tr>
49 <tr>
50 <tr align="center">
51 <th colspan="2">
52 <input type="submit" value="提交">
53 </th>
54 </tr>
55 </form>
56 </table>
57 </body>
58 </html>
添加图书信息:

数据库添加成功:


浙公网安备 33010602011771号