Java Web下MySQL数据库的增删改查(二)

前文:https://www.cnblogs.com/Arisf/p/14095002.html

在之前图书管理系统上做了改进优化

图书管理系统v2

首先是项目结构:

1.数据库的连接:

  1 package db;
  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     private static String url = "jdbc:mysql://localhost:3306/book system?&useSSL=false&serverTimezone=UTC";
 12     private static String user = "root";//用户名一般默认是root
 13     private static String password = "";//自己的密码
 14     private static String jdbcName="com.mysql.cj.jdbc.Driver";
 15     private Connection con=null;
 16     public static  Connection getConnection() {
 17         Connection con=null;
 18         try {
 19             Class.forName(jdbcName);
 20             con=DriverManager.getConnection(url, user, password);
 21             //System.out.println("数据库连接成功");
 22         } catch (Exception e) {
 23             // TODO Auto-generated catch block
 24             //System.out.println("数据库连接失败");
 25             e.printStackTrace();
 26         }
 27         try {
 28             con = DriverManager.getConnection(url,user,password);
 29             System.out.println("数据库连接成功");
 30 
 31 
 32         } catch (SQLException e) {
 33             // TODO: handle exception
 34             e.printStackTrace();
 35         }
 36         return con;
 37     }
 38     public static void main(String[] args)throws SQLException { 
 39         Connection conn = getConnection();
 40         PreparedStatement pstmt = null;
 41         ResultSet rs = null;
 42         String sql ="select * from user_information";
 43         pstmt = conn.prepareStatement(sql);
 44         rs = pstmt.executeQuery();
 45         System.out.println(getConnection());
 46         while(rs.next()){
 47             System.out.println("数据表连接成功");
 48         }
 49 
 50     }
 51            
 52     public static void close(Connection con) {
 53         if(con!=null)
 54             try {
 55                 con.close();
 56             } catch (SQLException e) {
 57                 // TODO Auto-generated catch block
 58                 e.printStackTrace();
 59             }
 60         
 61     }
 62     public static void close(Statement state, Connection conn) {
 63         if(state!=null) {
 64             try {
 65                 state.close();
 66             } catch (SQLException e) {
 67                 e.printStackTrace();
 68             }
 69         }
 70         if(conn!=null) {
 71             try {
 72                 conn.close();
 73             } catch (SQLException e) {
 74                 e.printStackTrace();
 75             }
 76         }
 77     }
 78     
 79     public static void close(ResultSet rs, Statement state, Connection conn) {
 80         if(rs!=null) {
 81             try {
 82                 rs.close();
 83             } catch (SQLException e) {
 84                 e.printStackTrace();
 85             }
 86         }
 87         if(state!=null) {
 88             try {
 89                 state.close();
 90             } catch (SQLException e) {
 91                 e.printStackTrace();
 92             }
 93         }
 94         if(conn!=null) {
 95             try {
 96                 conn.close();
 97             } catch (SQLException e) {
 98                 e.printStackTrace();
 99             }
100         }
101     }
102 
103 }

2.实体类(3个——图书表,借书表,用户表)

 1 package bean;
 2 
 3 public class Bean_book {
 4     private int id;
 5     private String name;
 6     private String writer;
 7     private String press;
 8     private int num;
 9 
10     
11     public int getId() {
12         return id;
13     }
14     public void setId(int id) {
15         this.id = id;
16     }
17     
18     public String getName() {
19         return name;
20     }
21     public void setName(String name) {
22         this.name = name;
23     }
24     
25     public String getWriter() {
26         return writer;
27     }
28     public void setWriter(String writer) {
29         this.writer = writer;
30     }
31 
32     public String getPress() {
33         return press;
34     }
35     public void setPress(String press) {
36         this.press = press;
37     }
38     
39     public int getNum() {
40         return num;
41     }
42     public void setNum(int num) {
43         this.num = num;
44     }
45     
46     
47     public Bean_book(int id, String name, String writer,  String press,int num) {
48         this.id = id;
49         this.name = name;
50         this.writer = writer;
51         this.press = press;
52         this.num = num;
53     }
54     
55     
56     
57     public String toString() {
58         return "Book{" +
59                 "id=" + id +
60                 ", name='" + name + '\'' +
61                 ", writer='" + writer + '\'' +
62                 ", press='" + press + '\'' +
63                 ", num=" + num +
64                 '}';
65     }
66 
67 
68 }
 1 package bean;
 2 
 3 
 4 public class Bean_borrowing {
 5     private int id;
 6     private String name;
 7     private String writer;
 8     private String press;
 9     private String date;
10     private int borrower;
11 
12 
13 
14     
15     public int getId() {
16         return id;
17     }
18     public void setId(int id) {
19         this.id = id;
20     }
21     
22     public String getName() {
23         return name;
24     }
25     public void setName(String name) {
26         this.name = name;
27     }
28     
29     public String getWriter() {
30         return writer;
31     }
32     public void setWriter(String writer) {
33         this.writer = writer;
34     }
35 
36     public String getPress() {
37         return press;
38     }
39     public void setPress(String press) {
40         this.press = press;
41     }
42     
43     public String getDate() {
44         return date;
45     }
46     public void setDate(String date) {
47         this.date = date;
48     }
49     public int getBorrower() {
50         return borrower;
51     }
52     public void setBorrower(int borrower) {
53         this.borrower = borrower;
54     }
55     
56     public Bean_borrowing(int id, String name, String writer,  String press,String date,int borrower) {
57         this.id = id;
58         this.name = name;
59         this.writer = writer;
60         this.press = press;
61         this.date = date;
62         this.borrower=borrower;
63     }
64     
65     public String toString() {
66         return "Borrowing{" +
67                 "id=" + id +
68                 ", name='" + name + '\'' +
69                 ", writer='" + writer + '\'' +
70                 ", press='" + press + '\'' +
71                 ", date='" + date + '\'' +
72                 ", borrower=" + borrower +
73                 '}';
74     }
75 
76 }
 1 package bean;
 2 
 3 public class Bean_user {
 4     
 5     private int uid;
 6     private String name;
 7     private String sex;
 8     private String college;
 9     private String password;
10     private String identity;
11 
12     
13     public int getUid() {
14         return uid;
15     }
16     public void setUid(int uid) {
17         this.uid = uid;
18     }
19     
20     public String getName() {
21         return name;
22     }
23     public void setName(String name) {
24         this.name = name;
25     }
26     
27     public String getSex() {
28         return sex;
29     }
30     public void setSex(String sex) {
31         this.sex = sex;
32     }
33 
34     public String getCollege() {
35         return college;
36     }
37     public void setCollege(String college) {
38         this.college = college;
39     }
40     
41     public String getPassword() {
42         return password;
43     }
44     public void setPassword(String password) {
45         this.password = password;
46     }
47     
48     public String getIdentity() {
49         return identity;
50     }
51     public void setIdentity(String identity) {
52         this.identity = identity;
53     }
54     
55     public Bean_user(int uid, String name, String sex,  String college,String password,String identity) {
56         this.uid = uid;
57         this.name = name;
58         this.sex = sex;
59         this.college = college;
60         this.password = password;
61         this.identity = identity;
62     }
63     
64     
65     
66     
67     public String toString() {
68         return "User{" +
69                 "uid=" + uid +
70                 ", name='" + name + '\'' +
71                 ", sex='" + sex + '\'' +
72                 ", college='" + college + '\'' +
73                 ", password='" + password + '\'' +
74                 ", identity=" + identity +
75                 '}';
76     }
77 
78 }

3.Dao

  1 package dao;
  2 
  3 import java.sql.Connection;
  4 import java.sql.PreparedStatement;
  5 import java.sql.ResultSet;
  6 import java.sql.SQLException;
  7 import java.sql.Statement;
  8 import java.text.SimpleDateFormat;
  9 import java.util.ArrayList;
 10 import java.util.Date;
 11 import java.util.List;
 12 
 13 import bean.Bean_user;
 14 import bean.Bean_book;
 15 import bean.Bean_borrowing;
 16 import db.DBUtil;
 17 
 18 public class Dao {
 19     //dao层
 20     private DBUtil dbutil=new DBUtil();
 21 
 22     
 23 public Dao() {
 24     // TODO Auto-generated constructor stub
 25 }
 26 public boolean insert_user(Bean_user bean) {//插入读者数据的方法
 27     boolean f=false;
 28     String sql="insert into user_information(uid,name,sex,college,password,identity) values('"+bean.getUid()+"','"+bean.getName()+"','"+bean.getSex()+"','"+bean.getCollege()+"','"+bean.getPassword()+"','"+bean.getIdentity()+"')";
 29     Connection conn=DBUtil.getConnection();//数据库连接,加载驱动
 30     Statement state=null;
 31     try
 32     {
 33         state=conn.createStatement();//实例化Statement对象,方便对sql语句进行操作
 34         System.out.println(conn);
 35         state.executeUpdate(sql);
 36         f=true;
 37         //执行数据库更新操作用于执行INSERT、UPDATE或DELETE语句以及SQLDDL(数据定义语言)语句,
 38         //例如CREATETABLE和DROPTABLE,(创建表和删除表)
 39     }catch(Exception e)//当try语句中s出现异常时,会执行catch中的语句
 40       {
 41         e.printStackTrace();//捕获异常的语句
 42       }
 43      finally //finally作为异常处理的一部分,它只能用在try/catch语句中,并且附带一个语句块,表示这段语句最终一定会被执行(不管有没有抛出异常),经常被用在需要释放资源的情况下。
 44      {
 45          DBUtil.close(conn);
 46      }
 47     return f;
 48 }
 49 
 50 public boolean insert_book(Bean_book bean) {//插入图书数据的方法
 51     boolean f=false;
 52     String sql="insert into book_information(id,name,writer,press,num) values('"+bean.getId()+"','"+bean.getName()+"','"+bean.getWriter()+"','"+bean.getPress()+"','"+bean.getNum()+"')";
 53     Connection conn=DBUtil.getConnection();//数据库连接,加载驱动
 54     Statement state=null;
 55     try
 56     {
 57         state=conn.createStatement();//实例化Statement对象,方便对sql语句进行操作
 58         System.out.println(conn);
 59         state.executeUpdate(sql);
 60         f=true;
 61         //执行数据库更新操作用于执行INSERT、UPDATE或DELETE语句以及SQLDDL(数据定义语言)语句,
 62         //例如CREATETABLE和DROPTABLE,(创建表和删除表)
 63     }catch(Exception e)//当try语句中s出现异常时,会执行catch中的语句
 64       {
 65         e.printStackTrace();//捕获异常的语句
 66       }
 67      finally //finally作为异常处理的一部分,它只能用在try/catch语句中,并且附带一个语句块,表示这段语句最终一定会被执行(不管有没有抛出异常),经常被用在需要释放资源的情况下。
 68      {
 69          DBUtil.close(conn);
 70      }
 71     return f;
 72 }
 73 
 74 public boolean insert_borrowing(Bean_borrowing bean) {//插入图书数据的方法
 75     boolean f=false;
 76     String sql="insert into borrowing_information(id,name,writer,press,date,borrower) values('"+bean.getId()+"','"+bean.getName()+"','"+bean.getWriter()+"','"+bean.getPress()+"','"+bean.getDate()+"','"+bean.getBorrower()+"')";
 77     Connection conn=DBUtil.getConnection();//数据库连接,加载驱动
 78     Statement state=null;
 79     try
 80     {
 81         state=conn.createStatement();//实例化Statement对象,方便对sql语句进行操作
 82         System.out.println(conn);
 83         state.executeUpdate(sql);
 84         f=true;
 85         //执行数据库更新操作用于执行INSERT、UPDATE或DELETE语句以及SQLDDL(数据定义语言)语句,
 86         //例如CREATETABLE和DROPTABLE,(创建表和删除表)
 87     }catch(Exception e)//当try语句中s出现异常时,会执行catch中的语句
 88       {
 89         e.printStackTrace();//捕获异常的语句
 90       }
 91      finally //finally作为异常处理的一部分,它只能用在try/catch语句中,并且附带一个语句块,表示这段语句最终一定会被执行(不管有没有抛出异常),经常被用在需要释放资源的情况下。
 92      {
 93          DBUtil.close(conn);
 94      }
 95     return f;
 96 }
 97 
 98 
 99 
100 public List<Bean_book> list(){//查询所有方法
101     String sql="select * from book_information order by id ASC";
102     Connection conn=DBUtil.getConnection();
103     Statement st=null;
104     List<Bean_book> list=new ArrayList<>();
105     ResultSet rs=null;
106     Bean_book bean=null;
107     try {
108         st=conn.createStatement();
109         st.executeQuery(sql);
110         rs=st.executeQuery(sql);
111         while(rs.next()) {
112             int id=rs.getInt("id");
113             String name = rs.getString("name");
114             String writer = rs.getString("writer");
115             String press = rs.getString("press");
116             int num=rs.getInt("num");
117             bean=new Bean_book(id,name,writer,press,num);
118             list.add(bean);
119         }
120     } catch (SQLException e) {
121         // TODO Auto-generated catch block
122         e.printStackTrace();
123     }
124     finally {
125         DBUtil.close(rs, st, conn);
126     }
127     return list;
128 }
129 
130 public List<Bean_borrowing> borrower(int uid){//查询所有方法
131     String sql="select * from borrowing_information where borrower='"+uid+"' order by date ASC";
132     Connection conn=DBUtil.getConnection();
133     Statement st=null;
134     List<Bean_borrowing> borrower=new ArrayList<>();
135     ResultSet rs=null;
136     Bean_borrowing bean=null;
137     try {
138         st=conn.createStatement();
139         st.executeQuery(sql);
140         rs=st.executeQuery(sql);
141         while(rs.next()) {
142             int id=rs.getInt("id");
143             String name = rs.getString("name");
144             String writer = rs.getString("writer");
145             String press = rs.getString("press");
146             String date = rs.getString("date");
147             bean=new Bean_borrowing(id,name,writer,press,date,uid);
148             borrower.add(bean);
149         }
150     } catch (SQLException e) {
151         // TODO Auto-generated catch block
152         e.printStackTrace();
153     }
154     finally {
155         DBUtil.close(rs, st, conn);
156     }
157     return borrower;
158 }
159 
160 public List<Bean_borrowing> list_overtime(){//查询过期书
161     
162     Date date=new Date();
163     SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
164     String now=sdf.format(date);
165     
166     String sql="select * from borrowing_information where date < '"+now+"' order by date ASC";
167     Connection conn=DBUtil.getConnection();
168     Statement st=null;
169     List<Bean_borrowing> list_overtime=new ArrayList<>();
170     ResultSet rs=null;
171     Bean_borrowing bean=null;
172     try {
173         st=conn.createStatement();
174         st.executeQuery(sql);
175         rs=st.executeQuery(sql);
176         while(rs.next()) {
177             int id=rs.getInt("id");
178             int borrower=rs.getInt("borrower");
179             String name = rs.getString("name");
180             String writer = rs.getString("writer");
181             String press = rs.getString("press");
182             String date1 = rs.getString("date");
183             bean=new Bean_borrowing(id,name,writer,press,date1,borrower);
184             list_overtime.add(bean);
185         }
186     } catch (SQLException e) {
187         // TODO Auto-generated catch block
188         e.printStackTrace();
189     }
190     finally {
191         DBUtil.close(rs, st, conn);
192     }
193     return list_overtime;
194 }
195 
196 
197 public List<Bean_book> searchByName(String str) throws SQLException{//查询条件方法
198     String sql="select * from book_information where(name like '%"+str+"%')";
199     Connection conn=DBUtil.getConnection();
200     Statement st=null;
201     PreparedStatement pt = conn.prepareStatement(sql);
202     List<Bean_book> search=new ArrayList<>();
203     ResultSet rs=null;
204     Bean_book bean=null;
205     try {
206       pt=conn.prepareStatement(sql);
207         rs=pt.executeQuery();
208         while(rs.next()) {
209             int id=rs.getInt("id");
210             String name = rs.getString("name");
211             String writer = rs.getString("writer");
212             String press = rs.getString("press");
213             int num=rs.getInt("num");
214             bean=new Bean_book(id,name,writer,press,num);
215             search.add(bean);
216         }
217     } catch (SQLException e) {
218         // TODO Auto-generated catch block
219         e.printStackTrace();
220     }
221     finally {
222         DBUtil.close(rs, st, conn);
223     }
224     return search;
225 }
226 
227 public List<Bean_book> searchByWriter(String str) throws SQLException{//查询条件方法
228     String sql="select * from book_information where(writer like '%"+str+"%')";
229     Connection conn=DBUtil.getConnection();
230     Statement st=null;
231     PreparedStatement pt = conn.prepareStatement(sql);
232     List<Bean_book> search=new ArrayList<>();
233     ResultSet rs=null;
234     Bean_book bean=null;
235     try {
236       pt=conn.prepareStatement(sql);
237         rs=pt.executeQuery();
238         while(rs.next()) {
239             int id=rs.getInt("id");
240             String name = rs.getString("name");
241             String writer = rs.getString("writer");
242             String press=rs.getString("press");
243             int num=rs.getInt("num");
244             bean=new Bean_book(id,name,writer,press,num);
245             search.add(bean);
246         }
247     } catch (SQLException e) {
248         // TODO Auto-generated catch block
249         e.printStackTrace();
250     }
251     finally {
252         DBUtil.close(rs, st, conn);
253     }
254     return search;
255 }
256 
257 public boolean update(Bean_book bean) {//更新自减方法
258     String sql="update book_information set num='"+bean.getNum()+"',name='"+bean.getName()+"',writer='"+bean.getWriter()+"',press='"+bean.getPress()+"'where id='"+bean.getId()+"'";
259     Connection conn=DBUtil.getConnection();
260     boolean f=false;
261     Statement st=null;
262     try {
263         st=conn.createStatement();
264         st.executeUpdate(sql);
265         f=true;
266     } catch (SQLException e) {
267         // TODO Auto-generated catch block
268         e.printStackTrace();
269     }
270     return f;
271 }
272 
273 public boolean delete_book(int id ) {//删除方法
274     String sql="delete from book_information where id='"+id+"'";
275     boolean f=false;
276     Connection conn =DBUtil.getConnection();
277     Statement st=null;
278     try {
279         st=conn.createStatement();
280         st.executeUpdate(sql);
281         f=true;
282     } catch (SQLException e) {
283         // TODO Auto-generated catch block
284         e.printStackTrace();
285     }
286     finally{
287         DBUtil.close(st, conn);
288     }
289     return f;
290 }
291 
292 public boolean return_book(int id,int uid ) {//删除方法
293     String sql="delete from borrowing_information where id='"+id+"' and borrower='"+uid+"'";
294     boolean f=false;
295     Connection conn =DBUtil.getConnection();
296     Statement st=null;
297     try {
298         st=conn.createStatement();
299         st.executeUpdate(sql);
300         f=true;
301     } catch (SQLException e) {
302         // TODO Auto-generated catch block
303         e.printStackTrace();
304     }
305     finally{
306         DBUtil.close(st, conn);
307     }
308     return f;
309 }
310 
311 }

4.Servlet

 1 package servlet;
 2 
 3 
 4 import java.io.IOException;
 5 import java.sql.Connection;
 6 
 7 import java.sql.DriverManager;
 8 import java.sql.PreparedStatement;
 9 import java.sql.ResultSet;
10 import java.text.SimpleDateFormat;
11 import java.util.Date;
12 
13 import javax.servlet.ServletException;
14 import javax.servlet.annotation.WebServlet;
15 import javax.servlet.http.HttpServlet;
16 import javax.servlet.http.HttpServletRequest;
17 import javax.servlet.http.HttpServletResponse;
18 
19 
20 
21 /**
22  * Servlet implementation class UserServlet
23  */
24 @WebServlet("/LoginServlet")
25 public class LoginServlet extends HttpServlet {
26     private static final long serialVersionUID = 1L;
27        
28     /**
29      * @see HttpServlet#HttpServlet()
30      */
31     public LoginServlet() {
32         super();
33         // TODO Auto-generated constructor stub
34     }
35 
36     /**
37      * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
38      */
39     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
40         // TODO Auto-generated method stub
41         response.getWriter().append("Served at: ").append(request.getContextPath());
42     }
43          
44     /**
45      * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
46      */
47     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
48         // TODO Auto-generated method stub
49         request.setCharacterEncoding("UTF-8");
50         response.setCharacterEncoding("UTF-8");
51         int uid=Integer.parseInt(request.getParameter("uid"));
52         String pass=String.valueOf(request.getParameter("password"));
53         String identity=String.valueOf(request.getParameter("identity"));
54         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
55         String date=sdf.format(new Date());
56         try {
57             Class.forName("com.mysql.cj.jdbc.Driver");
58             String url="jdbc:mysql://localhost:3306/book system?&useSSL=false&serverTimezone=UTC";
59             String username="root";
60             String password="whyjlbcdy2001";
61             Connection conn=DriverManager.getConnection(url,username,password);
62             
63             String sql="select * from user_information where uid='"+uid+"'and password='"+pass+"'and identity='"+identity+"'";
64             
65             PreparedStatement ps=conn.prepareStatement(sql);
66             ResultSet rs=ps.executeQuery();
67             if(rs.next()) {
68                 System.out.println(date+"  "+uid+" "+"login"+""+identity);
69                 System.out.println();
70                 request.setAttribute("uid", uid);
71                 if(identity.equals("读者")){
72                     request.getRequestDispatcher("user_index.jsp").forward(request,response);
73                 }
74                 if(identity.equals("管理员")){
75                     request.getRequestDispatcher("admin_index.jsp").forward(request,response);
76                 }
77             }else{
78                 request.setAttribute("message", "用户名或密码错误");
79                 request.getRequestDispatcher("login.jsp").forward(request,response);
80             }
81         }catch(Exception e) {
82             e.printStackTrace();
83         }finally{
84             
85         }
86     }
87 
88 }
 1 package servlet;
 2 
 3 import java.io.IOException;
 4 import java.sql.SQLException;
 5 import java.util.List;
 6 
 7 import javax.servlet.ServletException;
 8 import javax.servlet.annotation.WebServlet;
 9 import javax.servlet.http.HttpServlet;
10 import javax.servlet.http.HttpServletRequest;
11 import javax.servlet.http.HttpServletResponse;
12 
13 import bean.Bean_book;
14 import dao.Dao;
15 
16 /**
17  * Servlet implementation class searchServlet_admin
18  */
19 @WebServlet("/searchServlet_admin")
20 public class SearchServlet_admin extends HttpServlet {
21     private static final long serialVersionUID = 1L;
22        
23     /**
24      * @see HttpServlet#HttpServlet()
25      */
26     public SearchServlet_admin() {
27         super();
28         // TODO Auto-generated constructor stub
29     }
30 
31     /**
32      * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
33      */
34     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
35         //response.getWriter().append("Served at: ").append(request.getContextPath());
36         request.setCharacterEncoding("utf-8");
37         response.setCharacterEncoding("utf-8");
38         String cxfs=request.getParameter("cxfs");
39         System.out.print(cxfs);        
40         String str=request.getParameter("value");
41         Dao dao=new Dao();
42         List<Bean_book> list = null;         
43         try {
44             if("1".equals(cxfs)){
45                  list=dao.searchByName(str);
46              } 
47             if("2".equals(cxfs)){
48                  list=dao.searchByWriter(str);
49              } 
50             }catch (SQLException e) {
51              // TODO 自动生成的 catch 块
52                 e.printStackTrace();
53              }
54         request.setAttribute("list", list);
55         request.getRequestDispatcher("delete book.jsp").forward(request,response);
56         System.out.print(list.size());
57     }
58     
59     /**
60      * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
61      */
62     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
63         // TODO Auto-generated method stub
64         doGet(request, response);
65     }
66 
67 }
 1 package servlet;
 2 
 3 import java.io.IOException;
 4 import java.sql.SQLException;
 5 import java.util.List;
 6 
 7 import javax.servlet.ServletException;
 8 import javax.servlet.annotation.WebServlet;
 9 import javax.servlet.http.HttpServlet;
10 import javax.servlet.http.HttpServletRequest;
11 import javax.servlet.http.HttpServletResponse;
12 
13 import bean.Bean_book;
14 import dao.Dao;
15 
16 /**
17  * Servlet implementation class searchServlet
18  */
19 @WebServlet("/SearchServlet")
20 public class SearchServlet extends HttpServlet {
21     private static final long serialVersionUID = 1L;
22        
23     /**
24      * @see HttpServlet#HttpServlet()
25      */
26     public SearchServlet() {
27         super();
28         // TODO Auto-generated constructor stub
29     }
30 
31     /**
32      * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
33      */
34     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
35         // TODO Auto-generated method stub
36         //response.getWriter().append("Served at: ").append(request.getContextPath());
37         request.setCharacterEncoding("utf-8");
38         response.setCharacterEncoding("utf-8");
39         String cxfs=request.getParameter("cxfs");
40         System.out.print(cxfs);
41         
42         String str=request.getParameter("value");
43          Dao dao=new Dao();
44          List<Bean_book> list = null;
45          
46          try {
47              if("1".equals(cxfs)){
48                      list=dao.searchByName(str);
49                  } 
50              if("2".equals(cxfs)){
51                      list=dao.searchByWriter(str);
52                  } 
53              } catch (SQLException e) {
54                  // TODO 自动生成的 catch 块
55                  e.printStackTrace();
56              }
57          request.setAttribute("list", list);
58          request.getRequestDispatcher("list book.jsp").forward(request,response);
59          System.out.print(list.size());
60     }
61 
62     /**
63      * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
64      */
65     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
66         // TODO Auto-generated method stub
67         doGet(request, response);
68     }
69 
70 }
  1 package servlet;
  2 
  3 import java.io.IOException;
  4 import java.io.UnsupportedEncodingException;
  5 import java.util.List;
  6 
  7 import javax.servlet.ServletException;
  8 import javax.servlet.annotation.WebServlet;
  9 import javax.servlet.http.HttpServlet;
 10 import javax.servlet.http.HttpServletRequest;
 11 import javax.servlet.http.HttpServletResponse;
 12 
 13 
 14  
 15 import java.util.Calendar;
 16 import java.util.Date;
 17 import java.text.ParseException;
 18 import java.text.SimpleDateFormat; 
 19 
 20 import bean.Bean_user;
 21 import bean.Bean_book;
 22 import bean.Bean_borrowing;
 23 import dao.Dao;
 24 
 25 /**
 26  * Servlet implementation class servlet
 27  */
 28 @WebServlet("/Servlet")
 29 public class Servlet extends HttpServlet {
 30     Dao dao = new Dao();
 31     private static final long serialVersionUID = 1L;
 32        
 33     /**
 34      * @see HttpServlet#HttpServlet()
 35      */
 36     public Servlet() {
 37         super();
 38         // TODO Auto-generated constructor stub
 39     }
 40     
 41     private void insert_user(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {// 42         // TODO Auto-generated method stub
 43         request.setCharacterEncoding("utf-8");
 44         int uid = Integer.parseInt(request.getParameter("uid"));
 45         String name = request.getParameter("name");
 46         String sex = request.getParameter("sex");
 47         String college= request.getParameter("college");
 48         String password= request.getParameter("password");
 49         String identity= request.getParameter("identity");
 50         Bean_user bean=new Bean_user(uid,name,sex,college,password,identity);
 51  
 52         if(dao.insert_user(bean)) {
 53             request.setAttribute("message", "添加成功");
 54             request.getRequestDispatcher("add user.jsp").forward(request, response);
 55         }
 56     }
 57     
 58     private void insert_book(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {// 59         // TODO Auto-generated method stub
 60         request.setCharacterEncoding("utf-8");
 61         
 62         int id = Integer.parseInt(request.getParameter("id"));
 63         String name = request.getParameter("name");
 64         String writer = request.getParameter("writer");
 65         String press= request.getParameter("press");
 66         int num = Integer.parseInt(request.getParameter("num"));
 67         Bean_book bean=new Bean_book(id,name,writer,press,num);
 68  
 69         if(dao.insert_book(bean)) {
 70             request.setAttribute("message", "添加成功");
 71             request.getRequestDispatcher("add book.jsp").forward(request, response);
 72         }
 73     }
 74     
 75     private void list(HttpServletRequest request, HttpServletResponse response) throws Exception {
 76         // TODO Auto-generated method stub
 77         request.setCharacterEncoding("UTF-8");
 78         String uid = request.getParameter("uid");
 79         //int uid = Integer.parseInt(request.getParameter("id"));
 80         List<Bean_book> list = dao.list();
 81         request.setAttribute("list", list);
 82         request.setAttribute("uid", uid);
 83 
 84         request.getRequestDispatcher("list book.jsp").forward(request,response);
 85     }
 86     
 87     private void Mylist(HttpServletRequest request, HttpServletResponse response) throws Exception {
 88         // TODO Auto-generated method stub
 89         request.setCharacterEncoding("UTF-8");
 90         String uid_string = request.getParameter("uid");
 91         int uid  = Integer.parseInt(uid_string);
 92         //int uid = Integer.parseInt(request.getParameter("id"));
 93         List<Bean_borrowing> borrower = dao.borrower(uid);
 94         request.setAttribute("list", borrower);
 95         request.setAttribute("uid", uid);
 96 
 97         request.getRequestDispatcher("my_list.jsp").forward(request,response);
 98     }
 99     
100     private void list_admin(HttpServletRequest request, HttpServletResponse response) throws Exception {
101         // TODO Auto-generated method stub
102         request.setCharacterEncoding("utf-8");
103         List<Bean_book> list_admin = dao.list();
104         request.setAttribute("list", list_admin);
105         request.getRequestDispatcher("delete book.jsp").forward(request,response);
106     }
107     
108     private void list_overtime(HttpServletRequest request, HttpServletResponse response) throws Exception {
109         // TODO Auto-generated method stub
110         request.setCharacterEncoding("UTF-8");
111         
112         String uid_string = request.getParameter("uid");
113         int uid  = Integer.parseInt(uid_string);
114         //int uid = Integer.parseInt(request.getParameter("id"));
115         List<Bean_borrowing> list_overtime = dao.list_overtime();
116         
117         request.setAttribute("list", list_overtime);
118         request.setAttribute("uid", uid);
119 
120         request.getRequestDispatcher("list_overtime.jsp").forward(request,response);
121     }
122     
123     
124     
125     @SuppressWarnings("deprecation")
126     private void borrow(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {//127         // TODO Auto-generated method stub
128         request.setCharacterEncoding("utf-8");
129         Date date0=new Date();
130         date0.setMonth(date0.getMonth()+3);
131         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
132         String date=sdf.format(date0);
133         int id = Integer.parseInt(request.getParameter("id"));
134         String name = request.getParameter("name");
135         String writer = request.getParameter("writer");
136         String press = request.getParameter("press");
137         
138         int num= Integer.parseInt(request.getParameter("num"));
139         num=num-1;
140         
141         String uid_string = request.getParameter("uid");
142         int uid  = Integer.parseInt(uid_string);
143 
144 
145         Bean_book bean1=new Bean_book(id,name,writer,press,num);
146         Bean_borrowing bean2=new Bean_borrowing(id,name,writer,press,date,uid);
147 
148         dao.update(bean1);
149         dao.insert_borrowing(bean2);
150         request.setAttribute("uid", uid);
151         request.getRequestDispatcher("Servlet?method=list").forward(request, response);
152     }
153     
154     private void delete_book(HttpServletRequest request, HttpServletResponse response) throws Exception, IOException {//155         // TODO Auto-generated method stub
156         request.setCharacterEncoding("UTF-8");
157         int id=Integer.parseInt(request.getParameter("id"));
158         dao.delete_book(id); //进行数据库的删除操作
159         request.setAttribute("message", "删除成功");
160         request.getRequestDispatcher("Servlet?method=list_admin").forward(request, response);
161     }
162     
163     private void return_book(HttpServletRequest request, HttpServletResponse response) throws Exception, IOException {//164         // TODO Auto-generated method stub        
165         request.setCharacterEncoding("UTF-8");
166         String uid_string = request.getParameter("uid");
167         int uid  = Integer.parseInt(uid_string);
168         int id=Integer.parseInt(request.getParameter("id"));       
169         dao.return_book(id,uid); //进行数据库的删除操作
170         request.setAttribute("message", "归还成功");
171         request.setAttribute("uid", uid);
172         request.getRequestDispatcher("Servlet?method=Mylist").forward(request, response);
173     }
174 
175     /**
176      * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
177      */
178     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
179         // TODO Auto-generated method stub
180         request.setCharacterEncoding("utf-8");
181         String method=request.getParameter("method");
182          if("insert_user".equals(method)) {
183              insert_user(request,response);           
184             }
185          else if("insert_book".equals(method)) {
186              insert_book(request,response);
187          }
188          else if("list".equals(method)) {
189                 try {
190                     list(request,response);
191                 } catch (Exception e) {
192                     // TODO Auto-generated catch block
193                     e.printStackTrace();
194                 }
195          }
196          else if("list_overtime".equals(method)) {
197                 try {
198                     list_overtime(request,response);
199                 } catch (Exception e) {
200                     // TODO Auto-generated catch block
201                     e.printStackTrace();
202                 }
203          }
204          else if("list_admin".equals(method)) {
205                 try {
206                     list_admin(request,response);
207                 } catch (Exception e) {
208                     // TODO Auto-generated catch block
209                     e.printStackTrace();
210                 }
211          }
212          else if("Mylist".equals(method)) {
213                 try {
214                     Mylist(request,response);
215                 } catch (Exception e) {
216                     // TODO Auto-generated catch block
217                     e.printStackTrace();
218                 }
219          }
220          else if("borrow".equals(method)) {
221                 borrow(request,response);
222             }
223          else if("delete_book".equals(method)) {
224                 try {
225                     delete_book(request,response);
226                 } catch (Exception e) {
227                     // TODO Auto-generated catch block
228                     e.printStackTrace();
229                 }                
230             }
231          else if("return_book".equals(method)) {
232                 try {
233                     return_book(request,response);
234                 } catch (Exception e) {
235                     // TODO Auto-generated catch block
236                     e.printStackTrace();
237                 }                
238             }
239     }
240 
241     /**
242      * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
243      */
244     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
245         // TODO Auto-generated method stub
246         doGet(request, response);
247     }
248 
249 }

5.前端页面

 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 </head>
 9 <body>
10 <%
11         Object message = request.getAttribute("message");
12         if (message != null && !"".equals(message)) {
13     %>
14     <script type="text/javascript">
15               alert("<%=request.getAttribute("message")%>"); //弹出对话框
16     </script>
17     <%
18         }
19     %>
20 <form name="user" action="LoginServlet" method="post" onsubmit="return check()">
21       <table id="addTable" class="table table-bordered" align="center">                 
22       
23        <tr class="text-center row">
24                 <td class="col-sm-2">
25                     学号/工号
26                 </td>
27                 <td class="col-sm-4">
28                        <input type="number" class="form-control"  name="uid" id="uid" >
29                 </td>
30        </tr>           
31                   
32        <tr class="text-center row">
33                 <td class="col-sm-2">
34                       密码
35                 </td>
36                 <td class="col-sm-4">
37                   <input type="password" class="form-control"  name="password" id="password">
38                 </td>
39        </tr>
40 
41 
42        <tr class="text-center row">
43                 <td>
44                   登陆身份
45                 </td>
46                 <td colspan="3">
47                   <select class="form-control" id="identity" name="identity">
48                     <option value="读者">读者</option>
49                     <option value="管理员">管理员</option>
50                   </select>
51                 </td>
52        </tr>
53        
54        
55         <tr class="text-center row" align="center">
56                 <td colspan="3">
57                     <input type="submit" value="登录" >   
58                 </td>
59        </tr>      
60        </table>
61 
62 </form>
63 <script type="text/javascript">
64         function check(){
65             if(user.uid.value==""||user.uid.value==null){
66                 alert("学号/工号为空!");
67                 return false;
68             }
69             if(user.password.value==""||user.password.value==null){
70                 alert("密码为空!");
71                 return false;
72             }
73         }
74     </script>
75 </body>
76 </html>
登陆页面
 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3                <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
 4 <!DOCTYPE html>
 5 <html>
 6 <head>
 7 <meta charset="UTF-8">
 8 <title>读者功能页</title>
 9 </head>
10 <body>
11 <%
12         Object uid = request.getAttribute("uid");
13         if (uid != null && !"".equals(uid)) {
14     %>
15     <script type="text/javascript">
16               alert("欢迎您!读者"+<%=request.getAttribute("uid")%>); //弹出对话框
17     </script>
18     <%
19         }
20     %>
21 当前用户:<%=uid %>    
22     <div align="center" font-size="30px">
23         <h1>读者</h1>
24         <div>
25             <a  href="Servlet?method=list&uid=<%=uid%>">浏览图书信息</a>
26         </div>
27         <div>
28             <a href="Servlet?method=list&uid=<%=uid%>">查询图书信息</a>
29         </div>
30         <div>
31             <a href="Servlet?method=list&uid=<%=uid%>">借阅图书</a>
32         </div>
33         <div>
34             <a href="Servlet?method=Mylist&uid=<%=uid%>">浏览催还书目</a>
35         </div>
36         <div>
37             <a href="Servlet?method=Mylist&uid=<%=uid%>">归还图书</a>
38         </div>
39     </div>
40 </body>
41 </html>
读者主页
 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 </head>
 9 <body>
10 <%
11         Object uid = request.getAttribute("uid");
12         if (uid != null && !"".equals(uid)) {
13     %>
14     <script type="text/javascript">
15               alert("工号"+<%=request.getAttribute("uid")%>+'你好!'); //弹出对话框
16     </script>
17     <%
18         }
19     %>
20 当前用户:<%=uid %>     
21    <div align="center" font-size="30px">
22         <h1>管理员</h1>
23         <div>
24             <a  href="add user.jsp">添加用户信息</a>
25         </div>
26         <div>
27             <a href="add book.jsp">添加新书信息</a>
28         </div>
29         <div>
30             <a href="Servlet?method=list_admin">删除书目信息</a>
31         </div>
32         <div>
33             <a href="Servlet?method=list_overtime&uid=<%=uid%>">打印催还书目</a>
34         </div>
35     </div>
36 </body>
37 </html>
管理员主页
 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3            <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
 4 <!DOCTYPE html>
 5 <html>
 6 <head>
 7 <meta charset="UTF-8">
 8 <title>查询</title>
 9 </head>
10 <body>
11 <%
12         Object uid = request.getAttribute("uid");
13     %>
14 当前用户:<%=uid%>  
15     <div align="center">
16         <h1 >信息列表</h1>
17         <h3>当前时间:<input  id="sysDate" style="font-size:18.72px;color:red;width: 320px;text-align: center;"></h3>
18           <h1> 
19         <form action="SearchServlet" method="post">
20         <select name="cxfs">
21   <option  id="cxfs"value ="1">书名</option>
22   <option  id="cxfs" value ="2">作者名</option>
23 </select>
24             <input type="text" id="value" name="value" placeholder="请输入条件">
25             <input type="submit" id="select" name="select" value="查询" />
26       </form>
27         
28         </h1>
29         <table >
30             <tr>
31                 <td>图书编号</td>
32                 <td>书名</td>
33                 <td>作者名</td>
34                 <td>出版社名称</td>
35                 <td>可借阅数量</td>
36                 <td align="center" colspan="2">操作</td>
37             </tr>
38             <c:forEach items="${list}" var="item">
39                 <tr>
40                     <td>${item.id}</td>
41                     <td>${item.name}</td>
42                     <td>${item.writer}</td>
43                     <td>${item.press}</td>
44                     <td>${item.num}</td>
45                     <td><a href="Servlet?method=borrow&id=${item.id}&name=${item.name}&writer=${item.writer}&press=${item.press}&num=${item.num}&uid=<%=uid%> " 
46                     onclick="next()">订阅</a></td>
47                 </tr>
48             </c:forEach>
49         </table>
50     </div>
51 </body>
52 
53 <script type="text/javascript">
54 
55         /*---------- 动态获取系统当前日期方法start ------*/
56         setInterval(
57                 "document.getElementById('sysDate').value=new Date().toLocaleString()+'  星期'+'日一二三四五六'.charAt(new Date().getDay());",
58                 1000);
59         setInterval(
60                 "document.getElementById('sysSysDate').value=new Date().toLocaleString();",
61                 1000);
62  
63         /*---------- 动态获取系统当前日期方法end ------*/
64         var d = new Date();
65         d.setMonth(d.getMonth()+3);
66         d=d.toLocaleString();
67 
68 
69         function next(){
70             alert('订阅成功!还书日期为'+d);
71         }
72         
73     
74 
75 </script>
76 </html>
读者查询书籍
 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3      <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
 4 <!DOCTYPE html>
 5 <html>
 6 <head>
 7 <meta charset="UTF-8">
 8 <title>我的借阅</title>
 9 </head>
10 <body>
11 <%
12         Object uid = request.getAttribute("uid");
13     %>
14 
15 当前用户:<%=uid%> 
16 <div align="center">
17         <h1 >我的借阅</h1>
18         <h3>当前时间:<input  id="sysDate" style="font-size:18.72px;color:red;width: 320px;text-align: center;"></h3>
19 
20         <table >
21             <tr>
22                 <td>图书编号</td>
23                 <td>书名</td>
24                 <td>作者名</td>
25                 <td>出版社名称</td>
26                 <td>还书日期</td>
27                 <td align="center" colspan="2">操作</td>
28             </tr>
29             <c:forEach items="${list}" var="item">
30                 <tr>
31                     <td>${item.id}</td>
32                     <td>${item.name}</td>
33                     <td>${item.writer}</td>
34                     <td>${item.press}</td>
35                     <td>${item.date}</td>
36                     <td><a href="Servlet?method=return_book&id=${item.id}&uid=<%=uid%>" 
37                     >还书</a></td>
38                 </tr>
39             </c:forEach>
40         </table>
41     </div> 
42 
43 </body>
44 <script type="text/javascript">
45 
46         /*---------- 动态获取系统当前日期方法start ------*/
47         setInterval(
48                 "document.getElementById('sysDate').value=new Date().toLocaleString()+'  星期'+'日一二三四五六'.charAt(new Date().getDay());",
49                 1000);
50         setInterval(
51                 "document.getElementById('sysSysDate').value=new Date().toLocaleString();",
52                 1000);
53  
54         /*---------- 动态获取系统当前日期方法end ------*/
55 
56 </script>
57 </html>
根据读者账号查询个人的借阅书籍
  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 </head>
  9 <body>
 10 <%
 11         Object message = request.getAttribute("message");
 12 
 13         if (message != null && !"".equals(message)) {
 14     %>
 15     <script type="text/javascript">
 16               alert("<%=request.getAttribute("message")%>"); //弹出对话框
 17     </script>
 18     <%
 19         }
 20     %>
 21 
 22 <form action="Servlet?method=insert_book" method="post">
 23 
 24       <table id="addTable" class="table table-bordered  ">
 25       
 26       <tr class="text-center row">
 27                 <td class="col-sm-2">
 28                 图书编号
 29                 </td>
 30                 <td class="col-sm-4">
 31                     <input type="text" class="form-control"  name="id" id="id" >
 32                 </td>
 33        </tr>         
 34                   
 35       
 36        <tr class="text-center row">
 37                 <td class="col-sm-2">
 38                 书名
 39                 </td>
 40                 <td class="col-sm-4">
 41                        <input type="text" class="form-control"  name="name" id="name" >
 42                 </td>
 43        </tr>           
 44                   
 45 
 46        <tr class="text-center row">
 47                 <td class="col-sm-2">
 48                 作者名
 49                 </td>
 50                 <td class="col-sm-4">
 51                        <input type="text" class="form-control"  name="writer" id="writer" >
 52                 </td>
 53        </tr>
 54        
 55        <tr class="text-center row">
 56                 <td class="col-sm-2">
 57                 出版社名
 58                 </td>
 59                 <td class="col-sm-4">
 60                        <input type="text" class="form-control"  name="press" id="press" >
 61                 </td>
 62        </tr>
 63        
 64        <tr class="text-center row">
 65                 <td class="col-sm-2">
 66                 可借阅数量
 67                 </td>
 68                 <td class="col-sm-4">
 69                        <input type="text" class="form-control"  name="num" id="num" >
 70                 </td>
 71        </tr> 
 72        
 73              
 74        
 75        </table>
 76             <input type="submit" value="添加" onclick="check()">         
 77 </form> 
 78 </body>
 79 <script type="text/javascript">
 80         function check() //封装一个<body>中做成点击事件的函数
 81         {
 82             if(document.getElementById('id').value=='') {
 83                   alert('图书编号不能为空!');
 84                   document.getElementById('id').focus();
 85                   return false;
 86             }
 87             if(document.getElementById('name').value==''){
 88                   alert('书名不能为空!');
 89                     document.getElementById('name').focus();
 90                     return false;
 91             }
 92             if(document.getElementById('writer').value=='') {
 93                       alert('作者名不能为空!');
 94                       document.getElementById('writer').focus();
 95                       return false;
 96                }
 97             else if(document.getElementById('press').value==''){
 98                   alert('出版社名不能为空!');
 99                   document.getElementById('press').focus();
100                   return false;
101             }
102             if(document.getElementById('num').value=='') {
103                       alert('借阅数量不能为空!');
104                       document.getElementById('num').focus();
105                       return false;
106                }
107         }
108         
109     </script>
110 </html>
管理员添加书籍
  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 </head>
  9 <body>
 10 <%
 11         Object message = request.getAttribute("message");
 12 
 13         if (message != null && !"".equals(message)) {
 14     %>
 15     <script type="text/javascript">
 16               alert("<%=request.getAttribute("message")%>"); //弹出对话框
 17     </script>
 18     <%
 19         }
 20     %>
 21 
 22 <form action="Servlet?method=insert_user" method="post">
 23 
 24       <table id="addTable" class="table table-bordered  ">
 25       
 26       <tr class="text-center row">
 27                 <td class="col-sm-2">
 28                 账号
 29                 </td>
 30                 <td class="col-sm-4">
 31                     <input type="text" class="form-control"  name="uid" id="uid" >
 32                 </td>
 33        </tr>         
 34                   
 35       
 36        <tr class="text-center row">
 37                 <td class="col-sm-2">
 38                 姓名
 39                 </td>
 40                 <td class="col-sm-4">
 41                        <input type="text" class="form-control"  name="name" id="name" >
 42                 </td>
 43        </tr>
 44        
 45        <tr class="text-center row">
 46                 <td class="col-sm-2">
 47                 密码
 48                 </td>
 49                 <td class="col-sm-4">
 50                     <input type="text" class="form-control"  name="password" id="password" >
 51                 </td>
 52        </tr>           
 53                   
 54               <tr>
 55                 <td class="col-sm-2">
 56                   性别
 57                 </td>
 58                 <td class="col-sm-4">
 59                           <input type="radio"  name="sex" id="sex" value="男"> 60                           <input type="radio"  name="sex" id="sex" value="女"> 61                 </td>
 62               </tr>
 63 
 64 
 65        <tr class="text-center row">
 66                 <td class="col-sm-2">
 67                 所在学院
 68                 </td>
 69                 <td class="col-sm-4">
 70                        <input type="text" class="form-control"  name="college" id="college" >
 71                 </td>
 72        </tr> 
 73        
 74         <tr class="text-center row">
 75                 <td>
 76                   身份
 77                 </td>
 78                 <td colspan="3">
 79                   <select class="form-control" id="identity" name="identity">
 80                     <option value="读者">读者</option>
 81                     <option value="管理员">管理员</option>
 82                   </select>
 83                 </td>
 84               </tr>
 85        
 86       
 87        </table>
 88             <input type="submit" value="添加" onclick="check()">         
 89 </form> 
 90 </body>
 91 <script type="text/javascript">
 92         function check() //封装一个<body>中做成点击事件的函数
 93         {
 94             if(document.getElementById('uid').value=='') {
 95                   alert('账号不能为空!');
 96                   document.getElementById('uid').focus();
 97                   return false;
 98             }
 99             if(document.getElementById('password').value==''){
100                       alert('密码不能为空!');
101                   document.getElementById('password').focus();
102                   return false;
103               }
104             if(document.getElementById('name').value==''){
105                   alert('姓名不能为空!');
106                     document.getElementById('name').focus();
107                     return false;
108             }
109             if(document.getElementById('college').value==''){
110                   alert('所在学院不能为空!');
111                   document.getElementById('college').focus();
112                   return false;
113             }
114             if(document.getElementById('sex').value==''){
115                 alert('性别不能为空!');
116                 document.getElementById('sex').focus();
117                 return false;
118           }
119         }
120         
121     </script>
122 </html>
管理员添加用户
 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3                <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
 4 <!DOCTYPE html>
 5 <html>
 6 <head>
 7 <meta charset="UTF-8">
 8 <title>删除</title>
 9 </head>
10 <body>
11 <%
12         Object message = request.getAttribute("message");
13 
14         if (message != null && !"".equals(message)) {
15     %>
16     <script type="text/javascript">
17               alert("<%=request.getAttribute("message")%>"); //弹出对话框
18     </script>
19     <%
20         }
21     %>
22 
23     <div align="center">
24         <h1 >信息列表</h1>
25           <h1> 
26         <form action="searchServlet_admin" method="post">
27         <select name="cxfs">
28   <option  id="cxfs"value ="1">书名</option>
29   <option  id="cxfs" value ="2">作者名</option>
30 </select>
31             <input type="text" id="value" name="value" placeholder="请输入条件">
32             <input type="submit" id="select" name="select" value="查询" />
33       </form>
34         
35         </h1>
36         <table >
37             <tr>
38                 <td>图书编号</td>
39                 <td>书名</td>
40                 <td>作者名</td>
41                 <td>出版社名称</td>
42                 <td>可借阅数量</td>
43                 <td align="center" colspan="2">操作</td>
44             </tr>
45             <c:forEach items="${list}" var="item">
46                 <tr>
47                     <td>${item.id}</td>
48                     <td>${item.name}</td>
49                     <td>${item.writer}</td>
50                     <td>${item.press}</td>
51                     <td>${item.num}</td>
52                     <td><a href="Servlet?method=delete_book&id=${item.id}">删除</a></td>
53                 </tr>
54             </c:forEach>
55         </table>
56     </div>
57 </body>
58 </html>
管理员删除书籍
 1 <meta charset="UTF-8">
 2 <title>超时图书</title>
 3 </head>
 4 <body>
 5 <%
 6         Object uid = request.getAttribute("uid");
 7     %>
 8 
 9 当前用户:<%=uid%> 
10 <div align="center">
11         <h1 >催还书目</h1>
12         <h3>当前时间:<input  id="sysDate" style="font-size:18.72px;color:red;width: 320px;text-align: center;"></h3>
13 <div id="dy">
14         <table >
15             <tr>
16                 <td>图书编号</td>
17                 <td>书名</td>
18                 <td>作者名</td>
19                 <td>出版社名称</td>
20                 <td>还书日期</td>
21                 <td>借阅人</td>
22             </tr>
23             <c:forEach items="${list}" var="item">
24                 <tr>
25                     <td>${item.id}</td>
26                     <td>${item.name}</td>
27                     <td>${item.writer}</td>
28                     <td>${item.press}</td>
29                     <td>${item.date}</td>
30                     <td>${item.borrower}</td>
31                 </tr>
32             </c:forEach> 
33         </table>
34         </div>
35     </div>
36         <br>
37         <table align="center">
38         <tr><td>
39         <input type="button" value="打    印"  onclick="PrintTable(dy)">
40         </td></tr>
41            </table>
42 </body>
43 
44 <script type="text/javascript">
45 
46         /*---------- 动态获取系统当前日期方法start ------*/
47         setInterval(
48                 "document.getElementById('sysDate').value=new Date().toLocaleString()+'  星期'+'日一二三四五六'.charAt(new Date().getDay());",
49                 1000);
50         setInterval(
51                 "document.getElementById('sysSysDate').value=new Date().toLocaleString();",
52                 1000);
53  
54         /*---------- 动态获取系统当前日期方法end ------*/
55         
56         function PrintTable(Id){
57         var mStr;
58         mStr = window.document.body.innerHTML ;
59         var mWindow = window;
60         window.document.body.innerHTML =Id.innerHTML;
61         mWindow.print();
62         window.document.body.innerHTML = mStr;
63 }
64         
65 </script>
66 
67 </html>
管理员查看有哪些书籍超出借阅时间还没有归还

6.总结

1.增加了登录验证功能,账号信息在数据库中存在才能登录。

2.将账号信息进行跳转传递,在主页显示当前用户

3.完善了图书借阅相关功能,例如查询个人借阅(通过账号信息的参数传递实现)以及催还书目名单打印(JS调用浏览器打印功能)。

4.增加了更多的窗口提示,提升对用户的友好度。

7.数据库后台建表

 

posted @ 2020-12-15 21:51  Arisf  阅读(483)  评论(3编辑  收藏  举报