图书管理系统(BMS) 1.0.0

BookManagementSystem.java

  1 package cn.cleverer;
  2 
  3 import cn.cleverer.tablemodel.*;
  4 import cn.cleverer.model.*;
  5 import cn.cleverer.service.*;
  6 import javax.swing.*;
  7 import java.awt.*;
  8 import java.awt.event.*;
  9 
 10 public class BookManagementSystem {
 11     // 框架
 12     private JFrame frame;
 13     private JLabel imageLabel;
 14     private ImageIcon background;
 15     private JPanel imagePanel;
 16     // 顶部
 17     private JPanel headPanel = new JPanel();
 18     private JButton button_0;
 19     private JButton button_1;
 20     private JButton button_2;
 21     private JButton button_3;
 22     private JButton button_4;
 23     private JLabel adminLabel;
 24     // 主页
 25     private JPanel homePanel = new JPanel();
 26     private int homePanelVis = 0;
 27     private JTextField textField;
 28     // 搜索后显示图书页面
 29     private JTable searchTable = new JTable();
 30     private JScrollPane searchScrollPane = new JScrollPane();
 31     private int searchTableVis = 0;
 32     private BookTable bookTable;
 33     private SearchBookTable searchBookTable;
 34     // 预览页面
 35     private JTable previewTable = new JTable();
 36     private JScrollPane previewScrollPane = new JScrollPane();
 37     private int previewTableVis = 0;
 38     // 归还界面
 39     private JPanel returnBookPanel = new JPanel();
 40     private int returnBookPanelVis = 0;
 41     // 登录页面
 42     private JPanel loginPanel = new JPanel();
 43     private int loginPanelVis = 0;
 44     // 注册界面
 45     private JPanel registerPanel = new JPanel();
 46     private int registerPanelVis = 0;
 47     // 借阅
 48     private JPopupMenu borrowPopup;
 49 
 50     // 以下为管理界面--------------------------------------------------------------------------------
 51     // 管理
 52     private JPopupMenu adminPopup;
 53     JMenuItem updateAdmin;
 54     // 显示借阅信息
 55     private BorrowBookTable borrowBookTable;
 56     private JTable borrowTable = new JTable();
 57     private JScrollPane borrowScrollPane = new JScrollPane();
 58     private int borrowTableVis = 0;
 59     // 显示读者信息
 60     private ReaderTable readerTable;
 61     private JTable allReaderTable = new JTable();
 62     private JScrollPane allReaderScrollPane = new JScrollPane();
 63     private int allReaderTableVis = 0;
 64     private JPopupMenu readerPopup;
 65     // 添加图书界面
 66     private JPanel insertBookPanel = new JPanel();
 67     private int insertBookPanelVis = 0;
 68     // 删除图书界面
 69     private JPanel deleteBookPanel = new JPanel();
 70     private int deleteBookPanelVis = 0;
 71     // 更新管理员界面
 72     private JPanel updateAdminPanel = new JPanel();
 73     private int updateAdminPanelVis = 0;
 74 
 75     public static void main(String[] args) {
 76         BookManagementSystem window = new BookManagementSystem();
 77         window.frame.setVisible(true);
 78     }
 79 
 80     public BookManagementSystem() {
 81         frame = new JFrame();
 82         frame.setTitle("图书管理系统");
 83         frame.setResizable(false);
 84         frame.setSize(1280, 720);
 85         frame.setLocationRelativeTo(null);
 86         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 87         frame.getContentPane().setLayout(null);
 88 
 89         background = new ImageIcon("background.jpg");
 90         imageLabel = new JLabel(background);
 91         imageLabel.setBounds(0, 0, 1280, 720);
 92         imagePanel = (JPanel) frame.getContentPane();
 93         imagePanel.setOpaque(false);
 94         // 把背景图片添加到分层窗格的最底层作为背景
 95         frame.getLayeredPane().add(imageLabel, new Integer(Integer.MIN_VALUE));
 96 
 97         // 每次启动系统,重新计算是否超期
 98         BorrowService.refreshBorrow();
 99         // 显示顶部
100         showHead();
101         // 显示主业
102         showHome();
103     }
104 
105     // 隐藏所有面板
106     private void hideAll() {
107         homePanel.setVisible(false);
108         searchTable.setVisible(false);
109         searchScrollPane.setVisible(false);
110         previewTable.setVisible(false);
111         previewScrollPane.setVisible(false);
112         returnBookPanel.setVisible(false);
113         loginPanel.setVisible(false);
114         registerPanel.setVisible(false);
115 
116         allReaderTable.setVisible(false);
117         allReaderScrollPane.setVisible(false);
118         borrowTable.setVisible(false);
119         borrowScrollPane.setVisible(false);
120         insertBookPanel.setVisible(false);
121         deleteBookPanel.setVisible(false);
122         updateAdminPanel.setVisible(false);
123     }
124 
125     // 主页搜索图书 && 借阅图书
126     private void search() {
127         hideAll();
128         searchTable.setVisible(true);
129         searchScrollPane.setVisible(true);
130         if (searchTableVis == 0) {
131             searchTable.setBackground(UIManager.getColor("Button.background"));
132             searchTable.getTableHeader().setBackground(Color.PINK);
133             searchTable.setRowHeight(30);
134             searchTable.setFont(new Font("宋体", Font.PLAIN, 20));
135             searchScrollPane = new JScrollPane(searchTable);
136             searchScrollPane.setBounds(150, 100, 1000, 500);
137             frame.getContentPane().add(searchScrollPane);
138             searchTableVis = 1;
139 
140             borrowPopup = new JPopupMenu();
141             JMenuItem item = new JMenuItem("借阅");
142             item.addActionListener(new ActionListener() {
143                 public void actionPerformed(ActionEvent e) {
144                     int row = searchTable.getSelectedRow();
145                     if (row == -1)
146                         return;
147                     String bookISBN = (String) searchTable.getValueAt(row, 0);
148                     String readerAccount = button_1.getText();
149                     if (readerAccount.equals("登录")) {
150                         JOptionPane.showMessageDialog(null, "请先登录账号", "出错啦", 0);
151                         return;
152                     }
153                     int flag = BorrowService.checkBorrow(readerAccount);
154                     if (flag == 2)
155                         JOptionPane.showMessageDialog(null, "借阅失败,实际借阅的数量超过图书额定的借阅数量。", "出错啦", 0);
156                     else if (flag == 1)
157                         JOptionPane.showMessageDialog(null, "借阅失败,您有超期未归还的图书。", "出错啦", 0);
158                     else {
159                         String bookCode = BorrowService.borrowBook(bookISBN, readerAccount);
160                         JOptionPane.showMessageDialog(null, "借阅成功,图书编号为" + bookCode, "提示", 0);
161                     }
162                 }
163             });
164             borrowPopup.add(item);
165             searchTable.add(borrowPopup);
166 
167             searchTable.addMouseListener(new MouseListener() {
168                 public void mouseClicked(MouseEvent e) {
169                     if (e.getButton() == MouseEvent.BUTTON3) {
170                         borrowPopup.show(e.getComponent(), e.getX(), e.getY());
171                     }
172                 }
173 
174                 public void mouseEntered(MouseEvent e) {
175                 }
176 
177                 public void mouseExited(MouseEvent e) {
178                 }
179 
180                 public void mousePressed(MouseEvent e) {
181                 }
182 
183                 public void mouseReleased(MouseEvent e) {
184                 }
185             });
186         }
187 
188         String bookInfo = new String(textField.getText());
189 
190         searchBookTable = new SearchBookTable(BookService.showBook(bookInfo));
191         searchTable.setModel(searchBookTable);
192     }
193 
194     // 显示顶部
195     private void showHead() {
196         headPanel.setBounds(10, 0, 1244, 61);
197         frame.getContentPane().add(headPanel);
198         headPanel.setLayout(null);
199         headPanel.setOpaque(false);
200 
201         button_0 = new JButton("注册");
202         button_0.addActionListener(new ActionListener() {
203             public void actionPerformed(ActionEvent e) {
204                 if (button_0.getText() == "注册") {
205                     register();
206                 } else {
207                     logout();
208                 }
209             }
210         });
211         button_0.setBounds(1163, 10, 71, 41);
212         headPanel.add(button_0);
213 
214         button_1 = new JButton("登录");
215         button_1.addActionListener(new ActionListener() {
216             public void actionPerformed(ActionEvent e) {
217                 if (button_1.getText() == "登录") {
218                     login();
219                 } else {
220                     showReaderBorrow(button_1.getText());
221                 }
222             }
223         });
224         button_1.setBounds(1094, 10, 71, 41);
225         headPanel.add(button_1);
226 
227         button_2 = new JButton("还书");
228         button_2.addActionListener(new ActionListener() {
229             public void actionPerformed(ActionEvent e) {
230                 returnBook();
231             }
232         });
233         button_2.setBounds(1025, 10, 71, 41);
234         headPanel.add(button_2);
235 
236         button_3 = new JButton("预览");
237         button_3.addActionListener(new ActionListener() {
238             public void actionPerformed(ActionEvent e) {
239                 preview();
240             }
241         });
242         button_3.setBounds(956, 10, 71, 41);
243         headPanel.add(button_3);
244 
245         button_4 = new JButton("主页");
246         button_4.addActionListener(new ActionListener() {
247             public void actionPerformed(ActionEvent e) {
248                 showHome();
249             }
250         });
251         button_4.setBounds(887, 10, 71, 41);
252         headPanel.add(button_4);
253 
254         adminPopup = new JPopupMenu();
255         JMenu showReader = new JMenu("查询读者");
256         adminPopup.add(showReader);
257 
258         JMenuItem showAllReader = new JMenuItem("查询所有读者信息");
259         showAllReader.addActionListener(new ActionListener() {
260             public void actionPerformed(ActionEvent e) {
261                 showReader();
262             }
263         });
264         showReader.add(showAllReader);
265 
266         JMenu showBorrow = new JMenu("查询借阅记录");
267         adminPopup.add(showBorrow);
268 
269         JMenuItem showAllBorrow = new JMenuItem("查询所有借阅记录");
270         showAllBorrow.addActionListener(new ActionListener() {
271             public void actionPerformed(ActionEvent e) {
272                 showBorrow();
273             }
274         });
275         showBorrow.add(showAllBorrow);
276 
277         JMenuItem insertBook = new JMenuItem("添加图书");
278         insertBook.addActionListener(new ActionListener() {
279             public void actionPerformed(ActionEvent e) {
280                 insertBook();
281             }
282         });
283         adminPopup.add(insertBook);
284 
285         JMenuItem deleteBook = new JMenuItem("删除图书");
286         deleteBook.addActionListener(new ActionListener() {
287             public void actionPerformed(ActionEvent e) {
288                 deleteBook();
289             }
290         });
291         adminPopup.add(deleteBook);
292 
293         updateAdmin = new JMenuItem("更新管理员");
294         updateAdmin.addActionListener(new ActionListener() {
295             public void actionPerformed(ActionEvent e) {
296                 updateAdmin();
297             }
298         });
299         adminPopup.add(updateAdmin);
300         updateAdmin.setVisible(false);
301 
302         adminLabel = new JLabel("管理");
303         adminLabel.add(adminPopup);
304         adminLabel.addMouseListener(new MouseListener() {
305             public void mouseClicked(MouseEvent e) {
306                 if (e.getButton() == MouseEvent.BUTTON1) {
307                     adminPopup.show(e.getComponent(), e.getX(), e.getY());
308                 }
309             }
310 
311             public void mouseEntered(MouseEvent e) {
312             }
313 
314             public void mouseExited(MouseEvent e) {
315             }
316 
317             public void mousePressed(MouseEvent e) {
318             }
319 
320             public void mouseReleased(MouseEvent e) {
321             }
322         });
323         adminLabel.setBounds(20, 10, 71, 41);
324         adminLabel.setFont(new Font("宋体", Font.BOLD, 20));
325         headPanel.add(adminLabel);
326         adminLabel.setVisible(false);
327 
328         button_0.setContentAreaFilled(false);
329         button_1.setContentAreaFilled(false);
330         button_2.setContentAreaFilled(false);
331         button_3.setContentAreaFilled(false);
332         button_4.setContentAreaFilled(false);
333     }
334 
335     // 显示主页
336     private void showHome() {
337         hideAll();
338         homePanel.setVisible(true);
339         if (homePanelVis == 1)
340             return;
341         homePanelVis = 1;
342 
343         homePanel.setBounds(10, 60, 1244, 611);
344         frame.getContentPane().add(homePanel);
345         homePanel.setLayout(null);
346         homePanel.setOpaque(false);
347 
348         textField = new JTextField();
349         textField.setBounds(200, 150, 720, 50);
350         textField.setText("请输入图书名或作者");
351         textField.setFont(new Font("宋体", Font.PLAIN, 20));
352         textField.addKeyListener(new KeyListener() {
353             public void keyTyped(KeyEvent e) {
354             }
355 
356             public void keyReleased(KeyEvent e) {
357                 if (e.getKeyChar() == KeyEvent.VK_ENTER)
358                     search();
359             }
360 
361             public void keyPressed(KeyEvent e) {
362             }
363         });
364         homePanel.add(textField);
365 
366         JButton btnNewButton = new JButton("搜索");
367         btnNewButton.setFont(new Font("宋体", Font.BOLD, 25));
368         btnNewButton.addActionListener(new ActionListener() {
369             public void actionPerformed(ActionEvent e) {
370                 search();
371             }
372         });
373         btnNewButton.setBackground(Color.PINK);
374         btnNewButton.setBounds(920, 149, 100, 50);
375         homePanel.add(btnNewButton);
376     }
377 
378     // 预览(显示所有图书信息)
379     private void preview() {
380         hideAll();
381         previewTable.setVisible(true);
382         previewScrollPane.setVisible(true);
383         if (previewTableVis == 0) {
384             previewTable.setBackground(UIManager.getColor("Button.background"));
385             previewTable.getTableHeader().setBackground(Color.PINK);
386             previewTable.setRowHeight(30);
387             previewTable.setFont(new Font("宋体", Font.PLAIN, 20));
388             previewScrollPane = new JScrollPane(previewTable);
389             previewScrollPane.setBounds(150, 100, 1000, 500);
390             frame.getContentPane().add(previewScrollPane);
391             previewTableVis = 1;
392         }
393 
394         bookTable = new BookTable(BookService.showBook());
395         previewTable.setModel(bookTable);
396     }
397 
398     // 还书
399     private void returnBook() {
400         hideAll();
401         returnBookPanel.setVisible(true);
402         if (returnBookPanelVis == 1)
403             return;
404         returnBookPanelVis = 1;
405 
406         returnBookPanel.setBackground(UIManager.getColor("Button.background"));
407         returnBookPanel.setBounds(10, 60, 1244, 611);
408         frame.getContentPane().add(returnBookPanel);
409         returnBookPanel.setLayout(null);
410         returnBookPanel.setOpaque(false);
411 
412         JLabel label_0 = new JLabel("图书号:", JLabel.RIGHT);
413         label_0.setBounds(400, 100, 100, 50);
414         returnBookPanel.add(label_0);
415 
416         JTextField text_0 = new JTextField();
417         text_0.setBounds(500, 110, 250, 30);
418         returnBookPanel.add(text_0);
419 
420         JButton confirmButton = new JButton("确定");
421         confirmButton.addActionListener(new ActionListener() {
422             public void actionPerformed(ActionEvent e) {
423                 String bookCode = text_0.getText();
424 
425                 double fine = BorrowService.returnBook(bookCode);
426                 if (fine == -1) {
427                     JOptionPane.showMessageDialog(null, "还书失败,图书不存在或图书已归还", "提示", 0);
428                 } else {
429                     JOptionPane.showMessageDialog(null, "归还成功,罚金:" + fine + "元", "提示", 0);
430                 }
431 
432             }
433         });
434         confirmButton.setBounds(500, 420, 80, 40);
435         returnBookPanel.add(confirmButton);
436         JButton cancelButton = new JButton("取消");
437         cancelButton.addActionListener(new ActionListener() {
438             public void actionPerformed(ActionEvent e) {
439                 hideAll();
440                 homePanel.setVisible(true);
441                 showHome();
442             }
443         });
444         cancelButton.setBounds(670, 420, 80, 40);
445         returnBookPanel.add(cancelButton);
446         confirmButton.setBackground(Color.PINK);
447         cancelButton.setBackground(Color.PINK);
448     }
449 
450     // 登录
451     private void login() {
452         hideAll();
453         loginPanel.setVisible(true);
454         if (loginPanelVis == 1)
455             return;
456         loginPanelVis = 1;
457 
458         loginPanel.setBackground(UIManager.getColor("Button.background"));
459         loginPanel.setBounds(10, 60, 1244, 611);
460         frame.getContentPane().add(loginPanel);
461         loginPanel.setLayout(null);
462         loginPanel.setOpaque(false);
463 
464         JLabel accountLabel = new JLabel("账号:", JLabel.RIGHT);
465         accountLabel.setBounds(400, 100, 100, 50);
466         loginPanel.add(accountLabel);
467 
468         JTextField loginAccount = new JTextField();
469         loginAccount.setBounds(500, 110, 250, 30);
470         loginPanel.add(loginAccount);
471 
472         JLabel passwordLabel = new JLabel("密码:", JLabel.RIGHT);
473         passwordLabel.setBounds(400, 150, 100, 50);
474         loginPanel.add(passwordLabel);
475 
476         JPasswordField loginPassword = new JPasswordField();
477         loginPassword.setBounds(500, 160, 250, 30);
478         loginPanel.add(loginPassword);
479         
480         /*
481         loginAccount.setText("admin");
482         loginPassword.setText("admin");
483         */
484 
485         JButton confirmButton = new JButton("确定");
486         confirmButton.addActionListener(new ActionListener() {
487             public void actionPerformed(ActionEvent e) {
488                 String account = loginAccount.getText();
489                 char[] str = loginPassword.getPassword();
490                 String password = String.valueOf(str);
491 
492                 int identity = ReaderService.checkReader(account, password);
493                 if (identity == -1)
494                     JOptionPane.showMessageDialog(null, "账号不存在或密码错误", "出错啦", 0);
495                 else {
496                     button_1.setText(account);
497                     button_0.setText("退出");
498                     showHome();
499                     if (identity > 0) {
500                         adminLabel.setVisible(true);
501                         if (account.equals("admin"))
502                             updateAdmin.setVisible(true);
503                         else
504                             updateAdmin.setVisible(false);
505                     } else
506                         adminLabel.setVisible(false);
507                 }
508             }
509         });
510         confirmButton.setBounds(500, 420, 80, 40);
511         loginPanel.add(confirmButton);
512 
513         JButton cancelButton = new JButton("取消");
514         cancelButton.addActionListener(new ActionListener() {
515             public void actionPerformed(ActionEvent e) {
516                 hideAll();
517                 homePanel.setVisible(true);
518                 showHome();
519             }
520         });
521         cancelButton.setBounds(670, 420, 80, 40);
522         loginPanel.add(cancelButton);
523         confirmButton.setBackground(Color.PINK);
524         cancelButton.setBackground(Color.PINK);
525     }
526 
527     // 退出
528     private void logout() {
529         button_0.setText("注册");
530         button_1.setText("登录");
531         adminLabel.setVisible(false);
532         showHome();
533     }
534 
535     // 注册
536     private void register() {
537         hideAll();
538         registerPanel.setVisible(true);
539         if (registerPanelVis == 1)
540             return;
541         registerPanelVis = 1;
542 
543         registerPanel.setBackground(UIManager.getColor("Button.background"));
544         registerPanel.setBounds(10, 60, 1244, 611);
545         frame.getContentPane().add(registerPanel);
546         registerPanel.setLayout(null);
547         registerPanel.setOpaque(false);
548 
549         JLabel label_0 = new JLabel("账号:", JLabel.RIGHT);
550         label_0.setBounds(400, 100, 100, 50);
551         registerPanel.add(label_0);
552         JLabel label_1 = new JLabel("密码:", JLabel.RIGHT);
553         label_1.setBounds(400, 150, 100, 50);
554         registerPanel.add(label_1);
555         JLabel label_2 = new JLabel("确认密码:", JLabel.RIGHT);
556         label_2.setBounds(400, 200, 100, 50);
557         registerPanel.add(label_2);
558         JLabel label_3 = new JLabel("姓名:", JLabel.RIGHT);
559         label_3.setBounds(400, 250, 100, 50);
560         registerPanel.add(label_3);
561         JLabel label_4 = new JLabel("性别:", JLabel.RIGHT);
562         label_4.setBounds(400, 300, 100, 50);
563         registerPanel.add(label_4);
564         JLabel label_5 = new JLabel("手机号:", JLabel.RIGHT);
565         label_5.setBounds(400, 350, 100, 50);
566         registerPanel.add(label_5);
567         JLabel label_wrong1 = new JLabel("账号已存在", JLabel.LEFT);
568         label_wrong1.setBounds(800, 100, 100, 50);
569         label_wrong1.setForeground(Color.RED);
570         registerPanel.add(label_wrong1);
571         label_wrong1.setVisible(false);
572         JLabel label_wrong2 = new JLabel("两次密码不一样", JLabel.LEFT);
573         label_wrong2.setBounds(800, 200, 100, 50);
574         label_wrong2.setForeground(Color.RED);
575         registerPanel.add(label_wrong2);
576         label_wrong2.setVisible(false);
577         JLabel label_wrong3 = new JLabel("性别反人类", JLabel.LEFT);
578         label_wrong3.setBounds(800, 300, 100, 50);
579         label_wrong3.setForeground(Color.RED);
580         registerPanel.add(label_wrong3);
581         label_wrong3.setVisible(false);
582 
583         JTextField text_0 = new JTextField();
584         text_0.setBounds(500, 110, 250, 30);
585         registerPanel.add(text_0);
586         JPasswordField text_1 = new JPasswordField();
587         text_1.setBounds(500, 160, 250, 30);
588         registerPanel.add(text_1);
589         JPasswordField text_2 = new JPasswordField();
590         text_2.setBounds(500, 210, 250, 30);
591         registerPanel.add(text_2);
592         JTextField text_3 = new JTextField();
593         text_3.setBounds(500, 260, 250, 30);
594         registerPanel.add(text_3);
595         JTextField text_4 = new JTextField();
596         text_4.setBounds(500, 310, 250, 30);
597         registerPanel.add(text_4);
598         JTextField text_5 = new JTextField();
599         text_5.setBounds(500, 360, 250, 30);
600         registerPanel.add(text_5);
601         /*
602          * text_0.setText("Flowersea"); text_1.setText("12345678");
603          * text_2.setText("12345678"); text_3.setText("花海");
604          * text_4.setText("男"); text_5.setText("110");
605          */
606         JButton confirmButton = new JButton("确定");
607         confirmButton.addActionListener(new ActionListener() {
608             public void actionPerformed(ActionEvent e) {
609                 String s0 = text_0.getText();
610                 char[] str = text_1.getPassword();
611                 String s1 = String.valueOf(str);
612                 str = text_2.getPassword();
613                 String s2 = String.valueOf(str);
614                 String s3 = text_3.getText();
615                 String s4 = text_4.getText();
616                 String s5 = text_5.getText();
617 
618                 int ret = ReaderService.register(s0, s1, s2, s3, s4, s5);
619                 if (ret == 1)
620                     label_wrong1.setVisible(true);
621                 else
622                     label_wrong1.setVisible(false);
623                 if (ret == 2)
624                     label_wrong2.setVisible(true);
625                 else
626                     label_wrong2.setVisible(false);
627                 if (ret == 3)
628                     label_wrong3.setVisible(true);
629                 else
630                     label_wrong3.setVisible(false);
631                 if (ret == 0) {
632                     JOptionPane.showMessageDialog(null, "注册成功", "提示", 0);
633                     login();
634                 }
635             }
636         });
637         confirmButton.setBounds(500, 420, 80, 40);
638         registerPanel.add(confirmButton);
639         JButton cancelButton = new JButton("取消");
640         cancelButton.addActionListener(new ActionListener() {
641             public void actionPerformed(ActionEvent e) {
642                 hideAll();
643                 homePanel.setVisible(true);
644                 showHome();
645             }
646         });
647         cancelButton.setBounds(670, 420, 80, 40);
648         registerPanel.add(cancelButton);
649         confirmButton.setBackground(Color.PINK);
650         cancelButton.setBackground(Color.PINK);
651     }
652 
653     // 显示个人信息
654     private void showReaderBorrow(String readerAccount) {
655         hideAll();
656         borrowTable.setVisible(true);
657         borrowScrollPane.setVisible(true);
658         if (borrowTableVis == 0) {
659             borrowTable.setBackground(UIManager.getColor("Button.background"));
660             borrowScrollPane = new JScrollPane(borrowTable);
661             borrowScrollPane.setBounds(150, 100, 1000, 500);
662             frame.getContentPane().add(borrowScrollPane);
663             borrowTableVis = 1;
664         }
665 
666         borrowBookTable = new BorrowBookTable(BorrowService.showReaderBorrow(readerAccount));
667         borrowTable.setModel(borrowBookTable);
668     }
669 
670     // 以下为管理界面
671     // 查询所有读者信息
672     private void showReader() {
673         hideAll();
674         allReaderTable.setVisible(true);
675         allReaderScrollPane.setVisible(true);
676         if (allReaderTableVis == 0) {
677             allReaderTable.setBackground(UIManager.getColor("Button.background"));
678             allReaderTable.getTableHeader().setBackground(Color.PINK);
679             allReaderTable.setRowHeight(30);
680             allReaderTable.setFont(new Font("宋体", Font.PLAIN, 20));
681             allReaderScrollPane = new JScrollPane(allReaderTable);
682             allReaderScrollPane.setBounds(150, 100, 1000, 500);
683             frame.getContentPane().add(allReaderScrollPane);
684             allReaderTableVis = 1;
685         }
686 
687         readerPopup = new JPopupMenu();
688         JMenuItem item = new JMenuItem("查询详细信息");
689         item.addActionListener(new ActionListener() {
690             public void actionPerformed(ActionEvent e) {
691                 int row = allReaderTable.getSelectedRow();
692                 if (row == -1)
693                     return;
694                 String readerAccount = (String) allReaderTable.getValueAt(row, 0);
695                 showReaderBorrow(readerAccount);
696             }
697         });
698         readerPopup.add(item);
699         allReaderTable.add(readerPopup);
700 
701         allReaderTable.addMouseListener(new MouseListener() {
702             public void mouseClicked(MouseEvent e) {
703                 if (e.getButton() == MouseEvent.BUTTON3) {
704                     readerPopup.show(e.getComponent(), e.getX(), e.getY());
705                 }
706             }
707 
708             public void mouseEntered(MouseEvent e) {
709             }
710 
711             public void mouseExited(MouseEvent e) {
712             }
713 
714             public void mousePressed(MouseEvent e) {
715             }
716 
717             public void mouseReleased(MouseEvent e) {
718             }
719         });
720 
721         readerTable = new ReaderTable(ReaderService.showReader());
722         allReaderTable.setModel(readerTable);
723     }
724 
725     // 查询所有借阅信息
726     private void showBorrow() {
727         hideAll();
728         borrowTable.setVisible(true);
729         borrowScrollPane.setVisible(true);
730         if (borrowTableVis == 0) {
731             borrowTable.setBackground(UIManager.getColor("Button.background"));
732             borrowTable.getTableHeader().setBackground(Color.PINK);
733             borrowTable.setRowHeight(30);
734             borrowTable.setFont(new Font("宋体", Font.PLAIN, 20));
735             borrowScrollPane = new JScrollPane(borrowTable);
736             borrowScrollPane.setBounds(150, 100, 1000, 500);
737             frame.getContentPane().add(borrowScrollPane);
738             borrowTableVis = 1;
739         }
740 
741         borrowBookTable = new BorrowBookTable(BorrowService.showBorrow());
742         borrowTable.setModel(borrowBookTable);
743     }
744 
745     // 添加图书
746     private void insertBook() {
747         hideAll();
748         insertBookPanel.setVisible(true);
749         if (insertBookPanelVis == 1)
750             return;
751         insertBookPanelVis = 1;
752 
753         insertBookPanel.setBackground(UIManager.getColor("Button.background"));
754         insertBookPanel.setBounds(10, 60, 1244, 611);
755         frame.getContentPane().add(insertBookPanel);
756         insertBookPanel.setLayout(null);
757         insertBookPanel.setOpaque(false);
758 
759         JLabel label_0 = new JLabel("图书号:", JLabel.RIGHT);
760         label_0.setBounds(400, 100, 100, 50);
761         insertBookPanel.add(label_0);
762         JLabel label_1 = new JLabel("ISBN:", JLabel.RIGHT);
763         label_1.setBounds(400, 150, 100, 50);
764         insertBookPanel.add(label_1);
765         JLabel label_2 = new JLabel("图书名:", JLabel.RIGHT);
766         label_2.setBounds(400, 200, 100, 50);
767         insertBookPanel.add(label_2);
768         JLabel label_3 = new JLabel("作者:", JLabel.RIGHT);
769         label_3.setBounds(400, 250, 100, 50);
770         insertBookPanel.add(label_3);
771         JLabel label_4 = new JLabel("出版社:", JLabel.RIGHT);
772         label_4.setBounds(400, 300, 100, 50);
773         insertBookPanel.add(label_4);
774 
775         JTextField text_0 = new JTextField();
776         text_0.setBounds(500, 110, 250, 30);
777         insertBookPanel.add(text_0);
778         JTextField text_1 = new JTextField();
779         text_1.setBounds(500, 160, 250, 30);
780         insertBookPanel.add(text_1);
781         JTextField text_2 = new JTextField();
782         text_2.setBounds(500, 210, 250, 30);
783         insertBookPanel.add(text_2);
784         JTextField text_3 = new JTextField();
785         text_3.setBounds(500, 260, 250, 30);
786         insertBookPanel.add(text_3);
787         JTextField text_4 = new JTextField();
788         text_4.setBounds(500, 310, 250, 30);
789         insertBookPanel.add(text_4);
790 
791         JButton confirmButton = new JButton("确定");
792         confirmButton.addActionListener(new ActionListener() {
793             public void actionPerformed(ActionEvent e) {
794                 Book book = new Book(text_0.getText(), text_1.getText(), text_2.getText(), text_3.getText(),
795                         text_4.getText(), 1);
796 
797                 int ret = BookService.insertBook(book);
798                 if (ret == 1) {
799                     JOptionPane.showMessageDialog(null, "添加失败,图书号已存在", "提示", 0);
800                 } else {
801                     JOptionPane.showMessageDialog(null, "添加成功", "提示", 0);
802                 }
803             }
804         });
805         confirmButton.setBounds(500, 420, 80, 40);
806         insertBookPanel.add(confirmButton);
807         JButton cancelButton = new JButton("取消");
808         cancelButton.addActionListener(new ActionListener() {
809             public void actionPerformed(ActionEvent e) {
810                 hideAll();
811                 homePanel.setVisible(true);
812                 showHome();
813             }
814         });
815         cancelButton.setBounds(670, 420, 80, 40);
816         insertBookPanel.add(cancelButton);
817         confirmButton.setBackground(Color.PINK);
818         cancelButton.setBackground(Color.PINK);
819     }
820 
821     // 删除图书
822     private void deleteBook() {
823         hideAll();
824         deleteBookPanel.setVisible(true);
825         if (deleteBookPanelVis == 1)
826             return;
827         deleteBookPanelVis = 1;
828 
829         deleteBookPanel.setBackground(UIManager.getColor("Button.background"));
830         deleteBookPanel.setBounds(10, 60, 1244, 611);
831         frame.getContentPane().add(deleteBookPanel);
832         deleteBookPanel.setLayout(null);
833         deleteBookPanel.setOpaque(false);
834 
835         JLabel label_0 = new JLabel("图书号:", JLabel.RIGHT);
836         label_0.setBounds(400, 100, 100, 50);
837         deleteBookPanel.add(label_0);
838 
839         JTextField text_0 = new JTextField();
840         text_0.setBounds(500, 110, 250, 30);
841         deleteBookPanel.add(text_0);
842 
843         JButton confirmButton = new JButton("确定");
844         confirmButton.addActionListener(new ActionListener() {
845             public void actionPerformed(ActionEvent e) {
846                 String bookCode = text_0.getText();
847 
848                 int ret = BookService.deleteBook(bookCode);
849                 if (ret == 1) {
850                     JOptionPane.showMessageDialog(null, "删除失败,图书号不存在", "提示", 0);
851                 } else {
852                     JOptionPane.showMessageDialog(null, "删除成功", "提示", 0);
853                 }
854             }
855         });
856         confirmButton.setBounds(500, 420, 80, 40);
857         deleteBookPanel.add(confirmButton);
858         JButton cancelButton = new JButton("取消");
859         cancelButton.addActionListener(new ActionListener() {
860             public void actionPerformed(ActionEvent e) {
861                 hideAll();
862                 homePanel.setVisible(true);
863                 showHome();
864             }
865         });
866         cancelButton.setBounds(670, 420, 80, 40);
867         deleteBookPanel.add(cancelButton);
868         confirmButton.setBackground(Color.PINK);
869         cancelButton.setBackground(Color.PINK);
870     }
871 
872     private void updateAdmin() {
873         hideAll();
874         updateAdminPanel.setVisible(true);
875         if (updateAdminPanelVis == 1)
876             return;
877         updateAdminPanelVis = 1;
878 
879         updateAdminPanel.setBounds(10, 60, 1244, 611);
880         frame.getContentPane().add(updateAdminPanel);
881         updateAdminPanel.setLayout(null);
882         updateAdminPanel.setOpaque(false);
883 
884         JLabel accountLabel = new JLabel("账号:", JLabel.RIGHT);
885         accountLabel.setBounds(400, 100, 100, 50);
886         updateAdminPanel.add(accountLabel);
887 
888         JTextField readerAccount = new JTextField();
889         readerAccount.setBounds(500, 110, 250, 30);
890         updateAdminPanel.add(readerAccount);
891 
892         JLabel identityLabel = new JLabel("权限:", JLabel.RIGHT);
893         identityLabel.setBounds(400, 150, 100, 50);
894         updateAdminPanel.add(identityLabel);
895 
896         JTextField readerIdentity = new JTextField();
897         readerIdentity.setBounds(500, 160, 250, 30);
898         updateAdminPanel.add(readerIdentity);
899 
900         JButton confirmButton = new JButton("确定");
901         confirmButton.addActionListener(new ActionListener() {
902             public void actionPerformed(ActionEvent e) {
903                 String account = readerAccount.getText();
904                 String identity = readerIdentity.getText();
905                 int exist = ReaderService.checkReader(account);
906                 if (exist == 0) {
907                     JOptionPane.showMessageDialog(null, "账号不存在", "出错啦", 0);
908                     return;
909                 }
910                 if (!identity.equals("0") && !identity.equals("1")) {
911                     JOptionPane.showMessageDialog(null, "权限格式不正确,0代表普通读者,1代表管理员", "出错啦", 0);
912                     return;
913                 }
914                 int id = Integer.parseInt(identity);
915                 ReaderService.updateIdentity(account, id);
916                 JOptionPane.showMessageDialog(null, "更新成功", "提示", 0);
917             }
918         });
919         confirmButton.setBounds(500, 420, 80, 40);
920         updateAdminPanel.add(confirmButton);
921 
922         JButton cancelButton = new JButton("取消");
923         cancelButton.addActionListener(new ActionListener() {
924             public void actionPerformed(ActionEvent e) {
925                 hideAll();
926                 homePanel.setVisible(true);
927                 showHome();
928             }
929         });
930         cancelButton.setBounds(670, 420, 80, 40);
931         updateAdminPanel.add(cancelButton);
932         confirmButton.setBackground(Color.PINK);
933         cancelButton.setBackground(Color.PINK);
934     }
935 }
View Code

DataBaseConnection.java

 1 package cn.cleverer.databaseconnection;
 2 
 3 import java.sql.*;
 4 
 5 public class DataBaseConnection {
 6 
 7     public static Connection getConnection() {
 8         Connection con;
 9 
10         try {
11             Class.forName("com.mysql.jdbc.Driver").newInstance();
12         } catch (Exception e) {
13             System.err.print("ClassNotFoundException: ");
14             System.err.println(e.getMessage());
15             return null;
16         }
17 
18         try {
19             con = java.sql.DriverManager.getConnection("jdbc:mysql://localhost:3306/bms", "root", "root");
20             return con;
21         } catch (Exception e) {
22             System.err.print("Connection failed! ");
23             System.err.println(e.getMessage());
24             return null;
25         }
26     }
27 }
View Code

Book.java

 1 package cn.cleverer.model;
 2 
 3 public class Book {
 4     private String bookCode;
 5     private String bookISBN;
 6     private String bookName;
 7     private String bookPress;
 8     private String bookAuther;
 9     private int bookState;
10 
11     public String getBookCode() {
12         return bookCode;
13     }
14 
15     public void setBookCode(String bookCode) {
16         this.bookCode = bookCode;
17     }
18 
19     public String getBookISBN() {
20         return bookISBN;
21     }
22 
23     public void setBookISBN(String bookISBN) {
24         this.bookISBN = bookISBN;
25     }
26 
27     public String getBookName() {
28         return bookName;
29     }
30 
31     public void setBookName(String bookName) {
32         this.bookName = bookName;
33     }
34 
35     public String getBookPress() {
36         return bookPress;
37     }
38 
39     public void setBookPress(String bookPress) {
40         this.bookPress = bookPress;
41     }
42 
43     public String getBookAuther() {
44         return bookAuther;
45     }
46 
47     public void setBookAuther(String bookAuther) {
48         this.bookAuther = bookAuther;
49     }
50 
51     public int isBookState() {
52         return bookState;
53     }
54 
55     public void setBookState(int bookState) {
56         this.bookState = bookState;
57     }
58 
59     public Book(String bookCode, String bookISBN, String bookName, String bookPress, String bookAuther, int bookState) {
60         super();
61         this.bookCode = bookCode;
62         this.bookISBN = bookISBN;
63         this.bookName = bookName;
64         this.bookPress = bookPress;
65         this.bookAuther = bookAuther;
66         this.bookState = bookState;
67     }
68 }
View Code

Reader.java

 1 package cn.cleverer.model;
 2 
 3 public class Reader {
 4     private String readerAccount;
 5     private String readerPassword;
 6     private String readerName;
 7     private String readerSex;
 8     private String readerPhoneNumber;
 9     private int readerIdentity;
10 
11     public String getReaderAccount() {
12         return readerAccount;
13     }
14 
15     public void setReaderAccount(String readerAccount) {
16         this.readerAccount = readerAccount;
17     }
18 
19     public String getReaderPassword() {
20         return readerPassword;
21     }
22 
23     public void setReaderPassword(String readerPassword) {
24         this.readerPassword = readerPassword;
25     }
26 
27     public String getReaderName() {
28         return readerName;
29     }
30 
31     public void setReaderName(String readerName) {
32         this.readerName = readerName;
33     }
34 
35     public String getReaderSex() {
36         return readerSex;
37     }
38 
39     public void setReaderSex(String readerSex) {
40         this.readerSex = readerSex;
41     }
42 
43     public String getReaderPhoneNumber() {
44         return readerPhoneNumber;
45     }
46 
47     public void setReaderPhoneNumber(String readerPhoneNumber) {
48         this.readerPhoneNumber = readerPhoneNumber;
49     }
50 
51     public int getReaderIdentity() {
52         return readerIdentity;
53     }
54 
55     public void setReaderIdentity(int readerIdentity) {
56         this.readerIdentity = readerIdentity;
57     }
58 
59     public Reader(String readerAccount, String readerPassword, String readerName, String readerSex,
60             String readerPhoneNumber, int readerIdentity) {
61         super();
62         this.readerAccount = readerAccount;
63         this.readerPassword = readerPassword;
64         this.readerName = readerName;
65         this.readerSex = readerSex;
66         this.readerPhoneNumber = readerPhoneNumber;
67         this.readerIdentity = readerIdentity;
68     }
69 }
View Code

BookTable.java

 1 package cn.cleverer.tablemodel;
 2 
 3 import javax.swing.table.AbstractTableModel;
 4 import java.sql.*;
 5 
 6 public class BookTable extends AbstractTableModel {
 7     final String[] columnNames = { "书号", "ISBN", "书名", "作者", "出版社", "状态" };
 8 
 9     Object[][] data;
10 
11     public BookTable(ResultSet rs) {
12         int i = 0;
13         try {
14             while (rs.next()) {
15                 i++;
16             }
17             data = new Object[i][];
18             rs.beforeFirst();
19             i = 0;
20 
21             while (rs.next()) {
22                 data[i] = new Object[6];
23                 data[i][0] = new Object();
24                 data[i][0] = rs.getString("bookCode");
25                 data[i][1] = new Object();
26                 data[i][1] = rs.getString("bookISBN");
27                 data[i][2] = new Object();
28                 data[i][2] = rs.getString("bookName");
29                 data[i][3] = new Object();
30                 data[i][3] = rs.getString("bookAuther");
31                 data[i][4] = new Object();
32                 data[i][4] = rs.getString("bookPress");
33                 data[i][5] = new Object();
34                 data[i][5] = rs.getInt("bookState");
35                 i++;
36             }
37 
38         } catch (Exception e) {
39         }
40     }
41 
42     public int getColumnCount() {
43         return columnNames.length;
44     }
45 
46     public int getRowCount() {
47         return data.length;
48     }
49 
50     public String getColumnName(int col) {
51         return columnNames[col];
52     }
53 
54     public Object getValueAt(int row, int col) {
55         return data[row][col];
56     }
57 
58     public boolean isCellEditable(int row, int col) {
59         return false;
60     }
61 
62     // 获取列的数据类型,JTable使用该方法确定数据的显示格式。
63     public Class getColumnClass(int c) {
64         return getValueAt(0, c).getClass();
65     }
66 
67     // 改变网格单元的值。
68     public void setValueAt(Object value, int row, int col) {
69         data[row][col] = value;
70         fireTableCellUpdated(row, col);
71     }
72 }
View Code

BorrowBookTable.java

 1 package cn.cleverer.tablemodel;
 2 
 3 import javax.swing.table.AbstractTableModel;
 4 import java.sql.*;
 5 
 6 public class BorrowBookTable extends AbstractTableModel {
 7     final String[] columnNames = { "借阅号", "书号", "读者号", "借阅日期", "归还日期", "罚款" };
 8 
 9     Object[][] data;
10 
11     public BorrowBookTable(ResultSet rs) {
12         int i = 0;
13         try {
14             while (rs.next()) {
15                 i++;
16             }
17             data = new Object[i][];
18             rs.beforeFirst();
19             i = 0;
20 
21             while (rs.next()) {
22                 data[i] = new Object[6];
23                 data[i][0] = new Object();
24                 data[i][0] = rs.getString("borrowNumber");
25                 data[i][1] = new Object();
26                 data[i][1] = rs.getString("bookCode");
27                 data[i][2] = new Object();
28                 data[i][2] = rs.getString("readerAccount");
29                 data[i][3] = new Object();
30                 data[i][3] = rs.getString("borrowDate");
31                 data[i][4] = new Object();
32                 data[i][4] = rs.getString("returnDate");
33                 data[i][5] = new Object();
34                 data[i][5] = rs.getDouble("fine");
35                 i++;
36             }
37 
38         } catch (Exception e) {
39         }
40     }
41 
42     public int getColumnCount() {
43         return columnNames.length;
44     }
45 
46     public int getRowCount() {
47         return data.length;
48     }
49 
50     public String getColumnName(int col) {
51         return columnNames[col];
52     }
53 
54     public Object getValueAt(int row, int col) {
55         return data[row][col];
56     }
57 
58     public boolean isCellEditable(int row, int col) {
59         return false;
60     }
61 
62     // 获取列的数据类型,JTable使用该方法确定数据的显示格式。
63     public Class getColumnClass(int c) {
64         return getValueAt(0, c).getClass();
65     }
66 
67     // 改变网格单元的值。
68     public void setValueAt(Object value, int row, int col) {
69         data[row][col] = value;
70         fireTableCellUpdated(row, col);
71     }
72 }
View Code

ReaderTable.java

 1 package cn.cleverer.tablemodel;
 2 
 3 import javax.swing.table.AbstractTableModel;
 4 import java.sql.*;
 5 
 6 public class ReaderTable extends AbstractTableModel {
 7     final String[] columnNames = { "账号", "密码", "姓名", "性别", "手机号", "身份" };
 8 
 9     Object[][] data;
10 
11     public ReaderTable(ResultSet rs) {
12         int i = 0;
13         try {
14             while (rs.next()) {
15                 i++;
16             }
17             data = new Object[i][];
18             rs.beforeFirst();
19             i = 0;
20 
21             while (rs.next()) {
22                 data[i] = new Object[6];
23                 data[i][0] = new Object();
24                 data[i][0] = rs.getString("readerAccount");
25                 data[i][1] = new Object();
26                 data[i][1] = rs.getString("readerPassword");
27                 data[i][2] = new Object();
28                 data[i][2] = rs.getString("readerName");
29                 data[i][3] = new Object();
30                 data[i][3] = rs.getString("readerSex");
31                 data[i][4] = new Object();
32                 data[i][4] = rs.getString("readerPhoneNumber");
33                 data[i][5] = new Object();
34                 data[i][5] = rs.getInt("readerIdentity");
35                 i++;
36             }
37 
38         } catch (Exception e) {
39         }
40     }
41 
42     public int getColumnCount() {
43         return columnNames.length;
44     }
45 
46     public int getRowCount() {
47         return data.length;
48     }
49 
50     public String getColumnName(int col) {
51         return columnNames[col];
52     }
53 
54     public Object getValueAt(int row, int col) {
55         return data[row][col];
56     }
57 
58     public boolean isCellEditable(int row, int col) {
59         return false;
60     }
61 
62     // 获取列的数据类型,JTable使用该方法确定数据的显示格式。
63     public Class getColumnClass(int c) {
64         return getValueAt(0, c).getClass();
65     }
66 
67     // 改变网格单元的值。
68     public void setValueAt(Object value, int row, int col) {
69         data[row][col] = value;
70         fireTableCellUpdated(row, col);
71 
72     }
73 
74 }
View Code

SearchBookTable.java

 1 package cn.cleverer.tablemodel;
 2 
 3 import javax.swing.table.AbstractTableModel;
 4 import java.sql.*;
 5 
 6 public class SearchBookTable extends AbstractTableModel {
 7     final String[] columnNames = { "ISBN", "书名", "作者", "出版社", "剩余数量" };
 8 
 9     Object[][] data;
10 
11     public SearchBookTable(ResultSet rs) {
12         int i = 0;
13         try {
14             while (rs.next()) {
15                 i++;
16             }
17             data = new Object[i][];
18             rs.beforeFirst();
19             i = 0;
20 
21             while (rs.next()) {
22                 data[i] = new Object[5];
23                 data[i][0] = new Object();
24                 data[i][0] = rs.getString("bookISBN");
25                 data[i][1] = new Object();
26                 data[i][1] = rs.getString("bookName");
27                 data[i][2] = new Object();
28                 data[i][2] = rs.getString("bookAuther");
29                 data[i][3] = new Object();
30                 data[i][3] = rs.getString("bookPress");
31                 data[i][4] = new Object();
32                 data[i][4] = rs.getInt("sum(bookState)");
33                 i++;
34             }
35 
36         } catch (Exception e) {
37         }
38     }
39 
40     public int getColumnCount() {
41         return columnNames.length;
42     }
43 
44     public int getRowCount() {
45         return data.length;
46     }
47 
48     public String getColumnName(int col) {
49         return columnNames[col];
50     }
51 
52     public Object getValueAt(int row, int col) {
53         return data[row][col];
54     }
55 
56     public boolean isCellEditable(int row, int col) {
57         return false;
58     }
59 
60     // 获取列的数据类型,JTable使用该方法确定数据的显示格式。
61     public Class getColumnClass(int c) {
62         return getValueAt(0, c).getClass();
63     }
64 
65     // 改变网格单元的值。
66     public void setValueAt(Object value, int row, int col) {
67         data[row][col] = value;
68         fireTableCellUpdated(row, col);
69 
70     }
71 
72 }
View Code

BookService.java

  1 package cn.cleverer.service;
  2 
  3 import java.sql.Connection;
  4 import java.sql.ResultSet;
  5 import java.sql.Statement;
  6 import cn.cleverer.databaseconnection.DataBaseConnection;
  7 import cn.cleverer.model.Book;
  8 
  9 public class BookService {
 10     public static ResultSet showBook() {
 11         Connection con;
 12         String sql;
 13         Statement stmt;
 14         con = DataBaseConnection.getConnection();
 15         sql = "select * from book";
 16         ResultSet rs = null;
 17         try {
 18             stmt = con.createStatement();
 19             rs = stmt.executeQuery(sql);
 20         } catch (Exception e) {
 21         }
 22         return rs;
 23     }
 24 
 25     public static ResultSet showBook(String bookInfo) {
 26         Connection con;
 27         String sql;
 28         Statement stmt;
 29         con = DataBaseConnection.getConnection();
 30         sql = "select bookISBN,bookName,bookAuther,bookPress,sum(bookState) " + "from book " + "where bookName = " + "'"
 31                 + bookInfo + "'" + "or bookAuther = " + "'" + bookInfo + "'" + "group by bookISBN";
 32         ResultSet rs = null;
 33         try {
 34             stmt = con.createStatement();
 35             rs = stmt.executeQuery(sql);
 36         } catch (Exception e) {
 37         }
 38         return rs;
 39     }
 40 
 41     public static int insertBook(Book book) {
 42         Connection con;
 43         String sql;
 44         Statement stmt;
 45         con = DataBaseConnection.getConnection();
 46         String bookCode = book.getBookCode();
 47         sql = "select count(bookCode) from book where bookCode = " + "'" + bookCode + "'";
 48         ResultSet rs = null;
 49         try {
 50             stmt = con.createStatement();
 51             rs = stmt.executeQuery(sql);
 52         } catch (Exception e) {
 53         }
 54         int exist = 0;
 55         try {
 56             rs.next();
 57             exist = rs.getInt("count(bookCode)");
 58 
 59         } catch (Exception ee) {
 60         }
 61         if (exist == 1)
 62             return 1;
 63 
 64         con = DataBaseConnection.getConnection();
 65         sql = "insert into book values('" + book.getBookCode() + "','" + book.getBookISBN() + "','" + book.getBookName()
 66                 + "','" + book.getBookAuther() + "','" + book.getBookAuther() + "',1)";
 67         rs = null;
 68         try {
 69             stmt = con.createStatement();
 70             stmt.execute(sql);
 71         } catch (Exception ee) {
 72         }
 73 
 74         return 0;
 75     }
 76 
 77     public static int deleteBook(String bookCode) {
 78         Connection con;
 79         String sql;
 80         Statement stmt;
 81         con = DataBaseConnection.getConnection();
 82         sql = "select count(bookCode) from book where bookCode = " + "'" + bookCode + "'";
 83         ResultSet rs = null;
 84         try {
 85             stmt = con.createStatement();
 86             rs = stmt.executeQuery(sql);
 87         } catch (Exception e) {
 88         }
 89         int exist = 0;
 90         try {
 91             rs.next();
 92             exist = rs.getInt("count(bookCode)");
 93 
 94         } catch (Exception e) {
 95         }
 96         if (exist == 0)
 97             return 1;
 98 
 99         con = DataBaseConnection.getConnection();
100         sql = "delete from book where bookCode = " + "'" + bookCode + "'";
101         rs = null;
102         try {
103             stmt = con.createStatement();
104             stmt.execute(sql);
105         } catch (Exception ee) {
106         }
107 
108         return 0;
109     }
110 }
View Code

BorrowService.java

  1 package cn.cleverer.service;
  2 
  3 import java.sql.Connection;
  4 import java.sql.ResultSet;
  5 import java.sql.Statement;
  6 import cn.cleverer.databaseconnection.DataBaseConnection;
  7 import java.util.*;
  8 import java.text.SimpleDateFormat;
  9 
 10 public class BorrowService {
 11     public static ResultSet showBorrow() {
 12         Connection con;
 13         String sql;
 14         Statement stmt;
 15         con = DataBaseConnection.getConnection();
 16         sql = "select * from borrowrecord";
 17         ResultSet rs = null;
 18         try {
 19             stmt = con.createStatement();
 20             rs = stmt.executeQuery(sql);
 21         } catch (Exception e) {
 22         }
 23         return rs;
 24     }
 25 
 26     public static ResultSet showReaderBorrow(String readerAccount) {
 27         Connection con;
 28         String sql;
 29         Statement stmt;
 30         con = DataBaseConnection.getConnection();
 31         sql = "select * from borrowrecord where readerAccount = " + "'" + readerAccount + "'";
 32         ResultSet rs = null;
 33         try {
 34             stmt = con.createStatement();
 35             rs = stmt.executeQuery(sql);
 36         } catch (Exception e) {
 37         }
 38         return rs;
 39     }
 40 
 41     public static int checkBorrow(String readerAccount) {
 42         Connection con;
 43         String sql;
 44         Statement stmt;
 45         con = DataBaseConnection.getConnection();
 46         // 找出这个人已经借了多少本书
 47         sql = "select count(*) from borrowrecord where readerAccount = " + "'" + readerAccount + "'"
 48                 + "and returnDate = '未归还'";
 49         ResultSet rs = null;
 50         try {
 51             stmt = con.createStatement();
 52             rs = stmt.executeQuery(sql);
 53         } catch (Exception e) {
 54         }
 55         int number = 0;
 56         try {
 57             rs.next();
 58             number = rs.getInt("count(*)");
 59         } catch (Exception e) {
 60         }
 61         // 这里假设最多只能借3本
 62         if (number >= 3)
 63             return 2;
 64 
 65         // 获得这个人的罚款
 66         con = DataBaseConnection.getConnection();
 67         sql = "select fine from borrowrecord where readerAccount = " + "'" + readerAccount + "'";
 68         rs = null;
 69         try {
 70             stmt = con.createStatement();
 71             rs = stmt.executeQuery(sql);
 72         } catch (Exception e) {
 73         }
 74         double fine = 0;
 75         try {
 76             rs.next();
 77             fine = rs.getDouble("fine");
 78         } catch (Exception e) {
 79         }
 80         if (fine != 0)
 81             return 1;
 82         return 0;
 83     }
 84 
 85     public static String borrowBook(String bookISBN, String readerAccount) {
 86         Connection con;
 87         String sql;
 88         Statement stmt;
 89         con = DataBaseConnection.getConnection();
 90 
 91         // 获得ISBN的这种书所有没有被借的
 92         sql = "select bookCode from book where bookISBN = '" + bookISBN + "' and bookState = 1";
 93         ResultSet rs = null;
 94         try {
 95             stmt = con.createStatement();
 96             rs = stmt.executeQuery(sql);
 97         } catch (Exception e) {
 98         }
 99 
100         String bookCode = "";
101         try {
102             rs.next();
103             bookCode = rs.getString("bookCode");
104         } catch (Exception e) {
105         }
106 
107         // 把book表中这本书的状态置为0
108         sql = "update book set bookState = 0 where bookcode =" + "'" + bookCode + "'";
109         try {
110             stmt = con.createStatement();
111             stmt.execute(sql);
112         } catch (Exception e) {
113         }
114 
115         // 获得当前最大的借阅号,+1即为本次借阅的借阅号
116         sql = "select max(borrowNumber) from borrowrecord";
117         rs = null;
118         try {
119             stmt = con.createStatement();
120             rs = stmt.executeQuery(sql);
121         } catch (Exception e) {
122         }
123 
124         String borrowNumber = "";
125         try {
126             rs.next();
127             borrowNumber = rs.getString("max(borrowNumber)");
128         } catch (Exception e) {
129         }
130         int num = Integer.parseInt(borrowNumber);
131         num++;
132         borrowNumber = String.valueOf(num);
133         while (borrowNumber.length() < 5)
134             borrowNumber = 0 + borrowNumber; // 不够前面补0
135 
136         // 插入借阅信息
137         Date now = new Date();
138         SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
139         String borrowTime = dateFormat.format(now);
140         sql = "insert into borrowrecord values('" + borrowNumber + "','" + bookCode + "','" + readerAccount + "','"
141                 + borrowTime + "','未归还',0)";
142         try {
143             stmt = con.createStatement();
144             stmt.execute(sql);
145         } catch (Exception e) {
146         }
147 
148         return bookCode;
149     }
150 
151     public static double returnBook(String bookCode) {
152         Connection con;
153         String sql;
154         Statement stmt;
155         con = DataBaseConnection.getConnection();
156         // 判断这本书是否被借,如果没有被借,fine = -1,如果有罚款,fine = 罚款
157         sql = "select fine from borrowrecord where bookcode = " + "'" + bookCode + "'" + " and returndate = '未归还'";
158         ResultSet rs = null;
159         try {
160             stmt = con.createStatement();
161             rs = stmt.executeQuery(sql);
162         } catch (Exception e) {
163         }
164         double fine = -1;
165         try {
166             rs.next();
167             fine = rs.getDouble("fine");
168 
169         } catch (Exception e) {
170         }
171         if (fine == -1)
172             return -1;
173 
174         Date now = new Date();
175         SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
176         String returnTime = dateFormat.format(now);
177 
178         // 更新borrowrecord表
179         sql = "update borrowrecord set returndate = " + "'" + returnTime + "'" + ",fine = 0.0 where bookcode =" + "'"
180                 + bookCode + "'" + "and returndate = '未归还'";
181         try {
182             stmt = con.createStatement();
183             stmt.execute(sql);
184         } catch (Exception e) {
185         }
186 
187         // 把book表中这本书的状态置为1
188         sql = "update book set bookState = 1 where bookcode =" + "'" + bookCode + "'";
189         try {
190             stmt = con.createStatement();
191             stmt.execute(sql);
192         } catch (Exception e) {
193         }
194         return fine;
195     }
196 
197     // 重新计算所有在借的书是否超期,如果超期就更新罚款,罚款的计算规则为最长借一个月,超期罚款100元,设定在每次启动系统的时候执行
198     public static Date dateAdd(int days) {
199         // 日期处理模块 (将日期加上某些天或减去天数)返回字符串
200         Calendar canlendar = Calendar.getInstance(); // java.util包
201         canlendar.add(Calendar.DATE, days); // 日期减 如果不够减会将月变动
202         return canlendar.getTime();
203     }
204 
205     public static void refreshBorrow() {
206         Date date = BorrowService.dateAdd(-30);
207         SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
208         // 计算今天的前30天是哪一天
209         String extendedTime = dateFormat.format(date);
210         Connection con;
211         String sql;
212         Statement stmt;
213         con = DataBaseConnection.getConnection();
214         // 如果借阅的时间比前30天还早,就说明超期了
215         sql = "update borrowrecord set fine = 100 where borrowDate < '" + extendedTime + "'";
216         try {
217             stmt = con.createStatement();
218             stmt.execute(sql);
219         } catch (Exception e) {
220         }
221     }
222 }
View Code

ReaderService.java

  1 package cn.cleverer.service;
  2 
  3 import java.sql.Connection;
  4 import java.sql.ResultSet;
  5 import java.sql.Statement;
  6 import cn.cleverer.databaseconnection.DataBaseConnection;
  7 
  8 public class ReaderService {
  9     public static int checkReader(String account) {
 10         Connection con;
 11         String sql;
 12         Statement stmt;
 13         con = DataBaseConnection.getConnection();
 14         sql = "select count(*) from reader where readerAccount = " + "'" + account + "'";
 15         ResultSet rs = null;
 16         try {
 17             stmt = con.createStatement();
 18             rs = stmt.executeQuery(sql);
 19         } catch (Exception ee) {
 20         }
 21         int number = 0;
 22         try {
 23             rs.next();
 24             number = rs.getInt("count(*)");
 25         } catch (Exception e) {
 26         }
 27         return number;
 28     }
 29 
 30     public static int checkReader(String account, String password) {
 31         Connection con;
 32         String sql;
 33         Statement stmt;
 34         con = DataBaseConnection.getConnection();
 35         sql = "select readerAccount,readerPassword,readerIdentity from reader where readerAccount = " + "'" + account
 36                 + "'";
 37         ResultSet rs = null;
 38         try {
 39             stmt = con.createStatement();
 40             rs = stmt.executeQuery(sql);
 41         } catch (Exception ee) {
 42         }
 43 
 44         try {
 45             rs.next();
 46             String readerPassword = rs.getString("readerPassword");
 47             if (password.equals(readerPassword))
 48                 return rs.getInt("readerIdentity");
 49         } catch (Exception ee) {
 50         }
 51         return -1;
 52     }
 53 
 54     public static void updateIdentity(String account, int identity) {
 55         Connection con;
 56         String sql;
 57         Statement stmt;
 58         con = DataBaseConnection.getConnection();
 59         sql = "update reader set readerIdentity = " + identity + " where readerAccount = '" + account + "'";
 60         try {
 61             stmt = con.createStatement();
 62             stmt.execute(sql);
 63         } catch (Exception e) {
 64         }
 65     }
 66 
 67     public static int register(String s0, String s1, String s2, String s3, String s4, String s5) {
 68         Connection con;
 69         String sql;
 70         Statement stmt;
 71         con = DataBaseConnection.getConnection();
 72         sql = "select count(readerAccount) from reader where readerAccount = " + "'" + s0 + "'";
 73         ResultSet rs = null;
 74         try {
 75             stmt = con.createStatement();
 76             rs = stmt.executeQuery(sql);
 77         } catch (Exception ee) {
 78         }
 79         int exist = 0;
 80         try {
 81             rs.next();
 82             exist = rs.getInt("count(readerAccount)");
 83 
 84         } catch (Exception e) {
 85         }
 86 
 87         if (exist == 1)
 88             return 1;
 89         if (!s1.equals(s2))
 90             return 2;
 91         if (!s4.equals("男") && !s4.equals("女"))
 92             return 3;
 93 
 94         con = DataBaseConnection.getConnection();
 95         sql = "insert into reader values('" + s0 + "','" + s1 + "','" + s3 + "','" + s4 + "','" + s5 + "',0)";
 96         rs = null;
 97         try {
 98             stmt = con.createStatement();
 99             stmt.execute(sql);
100         } catch (Exception ee) {
101         }
102         return 0;
103     }
104 
105     public static ResultSet showReader() {
106         Connection con;
107         String sql;
108         Statement stmt;
109         con = DataBaseConnection.getConnection();
110         sql = "select * from reader";
111         ResultSet rs = null;
112         try {
113             stmt = con.createStatement();
114             rs = stmt.executeQuery(sql);
115         } catch (Exception e) {
116         }
117         return rs;
118     }
119 }
View Code

 

posted @ 2017-06-07 16:21  Flowersea  阅读(550)  评论(0)    收藏  举报