├── .classpath ├── .project ├── .settings ├── org.eclipse.core.resources.prefs └── org.eclipse.jdt.core.prefs ├── README.md ├── bin ├── dao │ ├── AdminDao.class │ ├── RegistDao.class │ └── UserDao.class ├── images │ ├── background.jpg │ └── logo.png ├── lib │ └── mysql-connector-java-5.1.25-bin.jar ├── model │ ├── Admin.class │ ├── AdminCode.class │ ├── Book.class │ ├── Register.class │ └── User.class ├── service │ ├── AdminService.class │ ├── RegistService.class │ └── UserService.class ├── test │ ├── Test01.class │ ├── Test02.class │ ├── TestAdminView.class │ └── TestUserDao.class ├── ui │ ├── AdminView$1.class │ ├── AdminView$10.class │ ├── AdminView$11.class │ ├── AdminView$2.class │ ├── AdminView$3.class │ ├── AdminView$4.class │ ├── AdminView$5.class │ ├── AdminView$6.class │ ├── AdminView$7.class │ ├── AdminView$8.class │ ├── AdminView$9.class │ ├── AdminView.class │ ├── Index.class │ ├── Login$1.class │ ├── Login$2.class │ ├── Login.class │ ├── RegisterDialog$1.class │ ├── RegisterDialog$2.class │ ├── RegisterDialog.class │ ├── UserView$1.class │ └── UserView.class └── utils │ ├── JDBCUtil.class │ └── Tools.class ├── library.sql └── src ├── dao ├── AdminDao.java ├── RegistDao.java └── UserDao.java ├── images ├── background.jpg └── logo.png ├── lib └── mysql-connector-java-5.1.25-bin.jar ├── model ├── Admin.java ├── AdminCode.java ├── Book.java ├── Register.java └── User.java ├── service ├── AdminService.java ├── RegistService.java └── UserService.java ├── test ├── Test01.java ├── Test02.java ├── TestAdminView.java └── TestUserDao.java ├── ui ├── AdminView.java ├── Index.java ├── Login.java ├── RegisterDialog.java └── UserView.java └── utils ├── JDBCUtil.java └── Tools.java /.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | LibrarySys 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | 15 | org.eclipse.jdt.core.javanature 16 | 17 | 18 | -------------------------------------------------------------------------------- /.settings/org.eclipse.core.resources.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | encoding/=UTF-8 3 | -------------------------------------------------------------------------------- /.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled 3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve 5 | org.eclipse.jdt.core.compiler.compliance=1.8 6 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate 7 | org.eclipse.jdt.core.compiler.debug.localVariable=generate 8 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate 9 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error 10 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error 11 | org.eclipse.jdt.core.compiler.source=1.8 12 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # java-swing-LibrarySystem 2 | 图书管理系统 3 | 应用:JAVA SWING、MYSQL 4 | 5 | ## 数据库信息和文件 6 | 修改数据库连接信息:utils包下的JDBCUtils.java文件。 7 | 数据库文件:library.sql 8 | 9 | ## 程序入口 10 | ui包下的Index.java文件 11 | 12 | -------------------------------------------------------------------------------- /bin/dao/AdminDao.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/narcissus1024/java-swing-LibrarySystem/cda412b234ebdb226e13eed8e0e213d77c0e6cc4/bin/dao/AdminDao.class -------------------------------------------------------------------------------- /bin/dao/RegistDao.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/narcissus1024/java-swing-LibrarySystem/cda412b234ebdb226e13eed8e0e213d77c0e6cc4/bin/dao/RegistDao.class -------------------------------------------------------------------------------- /bin/dao/UserDao.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/narcissus1024/java-swing-LibrarySystem/cda412b234ebdb226e13eed8e0e213d77c0e6cc4/bin/dao/UserDao.class -------------------------------------------------------------------------------- /bin/images/background.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/narcissus1024/java-swing-LibrarySystem/cda412b234ebdb226e13eed8e0e213d77c0e6cc4/bin/images/background.jpg -------------------------------------------------------------------------------- /bin/images/logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/narcissus1024/java-swing-LibrarySystem/cda412b234ebdb226e13eed8e0e213d77c0e6cc4/bin/images/logo.png -------------------------------------------------------------------------------- /bin/lib/mysql-connector-java-5.1.25-bin.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/narcissus1024/java-swing-LibrarySystem/cda412b234ebdb226e13eed8e0e213d77c0e6cc4/bin/lib/mysql-connector-java-5.1.25-bin.jar -------------------------------------------------------------------------------- /bin/model/Admin.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/narcissus1024/java-swing-LibrarySystem/cda412b234ebdb226e13eed8e0e213d77c0e6cc4/bin/model/Admin.class -------------------------------------------------------------------------------- /bin/model/AdminCode.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/narcissus1024/java-swing-LibrarySystem/cda412b234ebdb226e13eed8e0e213d77c0e6cc4/bin/model/AdminCode.class -------------------------------------------------------------------------------- /bin/model/Book.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/narcissus1024/java-swing-LibrarySystem/cda412b234ebdb226e13eed8e0e213d77c0e6cc4/bin/model/Book.class -------------------------------------------------------------------------------- /bin/model/Register.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/narcissus1024/java-swing-LibrarySystem/cda412b234ebdb226e13eed8e0e213d77c0e6cc4/bin/model/Register.class -------------------------------------------------------------------------------- /bin/model/User.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/narcissus1024/java-swing-LibrarySystem/cda412b234ebdb226e13eed8e0e213d77c0e6cc4/bin/model/User.class -------------------------------------------------------------------------------- /bin/service/AdminService.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/narcissus1024/java-swing-LibrarySystem/cda412b234ebdb226e13eed8e0e213d77c0e6cc4/bin/service/AdminService.class -------------------------------------------------------------------------------- /bin/service/RegistService.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/narcissus1024/java-swing-LibrarySystem/cda412b234ebdb226e13eed8e0e213d77c0e6cc4/bin/service/RegistService.class -------------------------------------------------------------------------------- /bin/service/UserService.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/narcissus1024/java-swing-LibrarySystem/cda412b234ebdb226e13eed8e0e213d77c0e6cc4/bin/service/UserService.class -------------------------------------------------------------------------------- /bin/test/Test01.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/narcissus1024/java-swing-LibrarySystem/cda412b234ebdb226e13eed8e0e213d77c0e6cc4/bin/test/Test01.class -------------------------------------------------------------------------------- /bin/test/Test02.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/narcissus1024/java-swing-LibrarySystem/cda412b234ebdb226e13eed8e0e213d77c0e6cc4/bin/test/Test02.class -------------------------------------------------------------------------------- /bin/test/TestAdminView.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/narcissus1024/java-swing-LibrarySystem/cda412b234ebdb226e13eed8e0e213d77c0e6cc4/bin/test/TestAdminView.class -------------------------------------------------------------------------------- /bin/test/TestUserDao.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/narcissus1024/java-swing-LibrarySystem/cda412b234ebdb226e13eed8e0e213d77c0e6cc4/bin/test/TestUserDao.class -------------------------------------------------------------------------------- /bin/ui/AdminView$1.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/narcissus1024/java-swing-LibrarySystem/cda412b234ebdb226e13eed8e0e213d77c0e6cc4/bin/ui/AdminView$1.class -------------------------------------------------------------------------------- /bin/ui/AdminView$10.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/narcissus1024/java-swing-LibrarySystem/cda412b234ebdb226e13eed8e0e213d77c0e6cc4/bin/ui/AdminView$10.class -------------------------------------------------------------------------------- /bin/ui/AdminView$11.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/narcissus1024/java-swing-LibrarySystem/cda412b234ebdb226e13eed8e0e213d77c0e6cc4/bin/ui/AdminView$11.class -------------------------------------------------------------------------------- /bin/ui/AdminView$2.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/narcissus1024/java-swing-LibrarySystem/cda412b234ebdb226e13eed8e0e213d77c0e6cc4/bin/ui/AdminView$2.class -------------------------------------------------------------------------------- /bin/ui/AdminView$3.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/narcissus1024/java-swing-LibrarySystem/cda412b234ebdb226e13eed8e0e213d77c0e6cc4/bin/ui/AdminView$3.class -------------------------------------------------------------------------------- /bin/ui/AdminView$4.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/narcissus1024/java-swing-LibrarySystem/cda412b234ebdb226e13eed8e0e213d77c0e6cc4/bin/ui/AdminView$4.class -------------------------------------------------------------------------------- /bin/ui/AdminView$5.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/narcissus1024/java-swing-LibrarySystem/cda412b234ebdb226e13eed8e0e213d77c0e6cc4/bin/ui/AdminView$5.class -------------------------------------------------------------------------------- /bin/ui/AdminView$6.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/narcissus1024/java-swing-LibrarySystem/cda412b234ebdb226e13eed8e0e213d77c0e6cc4/bin/ui/AdminView$6.class -------------------------------------------------------------------------------- /bin/ui/AdminView$7.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/narcissus1024/java-swing-LibrarySystem/cda412b234ebdb226e13eed8e0e213d77c0e6cc4/bin/ui/AdminView$7.class -------------------------------------------------------------------------------- /bin/ui/AdminView$8.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/narcissus1024/java-swing-LibrarySystem/cda412b234ebdb226e13eed8e0e213d77c0e6cc4/bin/ui/AdminView$8.class -------------------------------------------------------------------------------- /bin/ui/AdminView$9.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/narcissus1024/java-swing-LibrarySystem/cda412b234ebdb226e13eed8e0e213d77c0e6cc4/bin/ui/AdminView$9.class -------------------------------------------------------------------------------- /bin/ui/AdminView.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/narcissus1024/java-swing-LibrarySystem/cda412b234ebdb226e13eed8e0e213d77c0e6cc4/bin/ui/AdminView.class -------------------------------------------------------------------------------- /bin/ui/Index.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/narcissus1024/java-swing-LibrarySystem/cda412b234ebdb226e13eed8e0e213d77c0e6cc4/bin/ui/Index.class -------------------------------------------------------------------------------- /bin/ui/Login$1.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/narcissus1024/java-swing-LibrarySystem/cda412b234ebdb226e13eed8e0e213d77c0e6cc4/bin/ui/Login$1.class -------------------------------------------------------------------------------- /bin/ui/Login$2.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/narcissus1024/java-swing-LibrarySystem/cda412b234ebdb226e13eed8e0e213d77c0e6cc4/bin/ui/Login$2.class -------------------------------------------------------------------------------- /bin/ui/Login.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/narcissus1024/java-swing-LibrarySystem/cda412b234ebdb226e13eed8e0e213d77c0e6cc4/bin/ui/Login.class -------------------------------------------------------------------------------- /bin/ui/RegisterDialog$1.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/narcissus1024/java-swing-LibrarySystem/cda412b234ebdb226e13eed8e0e213d77c0e6cc4/bin/ui/RegisterDialog$1.class -------------------------------------------------------------------------------- /bin/ui/RegisterDialog$2.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/narcissus1024/java-swing-LibrarySystem/cda412b234ebdb226e13eed8e0e213d77c0e6cc4/bin/ui/RegisterDialog$2.class -------------------------------------------------------------------------------- /bin/ui/RegisterDialog.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/narcissus1024/java-swing-LibrarySystem/cda412b234ebdb226e13eed8e0e213d77c0e6cc4/bin/ui/RegisterDialog.class -------------------------------------------------------------------------------- /bin/ui/UserView$1.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/narcissus1024/java-swing-LibrarySystem/cda412b234ebdb226e13eed8e0e213d77c0e6cc4/bin/ui/UserView$1.class -------------------------------------------------------------------------------- /bin/ui/UserView.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/narcissus1024/java-swing-LibrarySystem/cda412b234ebdb226e13eed8e0e213d77c0e6cc4/bin/ui/UserView.class -------------------------------------------------------------------------------- /bin/utils/JDBCUtil.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/narcissus1024/java-swing-LibrarySystem/cda412b234ebdb226e13eed8e0e213d77c0e6cc4/bin/utils/JDBCUtil.class -------------------------------------------------------------------------------- /bin/utils/Tools.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/narcissus1024/java-swing-LibrarySystem/cda412b234ebdb226e13eed8e0e213d77c0e6cc4/bin/utils/Tools.class -------------------------------------------------------------------------------- /library.sql: -------------------------------------------------------------------------------- 1 | /* 2 | Navicat MySQL Data Transfer 3 | 4 | Source Server : MySql 5 | Source Server Version : 50520 6 | Source Host : localhost:3306 7 | Source Database : library 8 | 9 | Target Server Type : MYSQL 10 | Target Server Version : 50520 11 | File Encoding : 65001 12 | 13 | Date: 2019-12-24 13:50:39 14 | */ 15 | 16 | SET FOREIGN_KEY_CHECKS=0; 17 | 18 | -- ---------------------------- 19 | -- Table structure for `admin_code` 20 | -- ---------------------------- 21 | DROP TABLE IF EXISTS `admin_code`; 22 | CREATE TABLE `admin_code` ( 23 | `id` int(11) NOT NULL AUTO_INCREMENT, 24 | `code` varchar(50) NOT NULL, 25 | `count` int(11) NOT NULL DEFAULT '2', 26 | PRIMARY KEY (`id`), 27 | UNIQUE KEY `unique` (`code`) 28 | ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8; 29 | 30 | -- ---------------------------- 31 | -- Records of admin_code 32 | -- ---------------------------- 33 | INSERT INTO `admin_code` VALUES ('1', '12345678', '0'); 34 | INSERT INTO `admin_code` VALUES ('2', '输入这个神奇的密钥你就可以注册了', '1'); 35 | 36 | -- ---------------------------- 37 | -- Table structure for `t_admin` 38 | -- ---------------------------- 39 | DROP TABLE IF EXISTS `t_admin`; 40 | CREATE TABLE `t_admin` ( 41 | `aid` int(11) NOT NULL AUTO_INCREMENT, 42 | `username` varchar(30) NOT NULL, 43 | `password` varchar(30) NOT NULL, 44 | `name` varchar(30) NOT NULL, 45 | PRIMARY KEY (`aid`), 46 | UNIQUE KEY `unique` (`username`) 47 | ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8; 48 | 49 | -- ---------------------------- 50 | -- Records of t_admin 51 | -- ---------------------------- 52 | INSERT INTO `t_admin` VALUES ('1', 'admin', 'admin', '花花'); 53 | INSERT INTO `t_admin` VALUES ('2', 'demaxiya', 'demaxiya', '德玛西亚之力'); 54 | INSERT INTO `t_admin` VALUES ('3', 'guoqilin', 'guoqilin', '郭麒麟'); 55 | 56 | -- ---------------------------- 57 | -- Table structure for `t_books` 58 | -- ---------------------------- 59 | DROP TABLE IF EXISTS `t_books`; 60 | CREATE TABLE `t_books` ( 61 | `id` int(11) NOT NULL AUTO_INCREMENT, 62 | `bookname` varchar(30) NOT NULL, 63 | `author` varchar(30) NOT NULL, 64 | `number` bigint(20) NOT NULL, 65 | `borrow` varchar(10) NOT NULL, 66 | `location` varchar(30) NOT NULL, 67 | PRIMARY KEY (`id`), 68 | UNIQUE KEY `unique` (`number`) 69 | ) ENGINE=InnoDB AUTO_INCREMENT=18 DEFAULT CHARSET=utf8; 70 | 71 | -- ---------------------------- 72 | -- Records of t_books 73 | -- ---------------------------- 74 | INSERT INTO `t_books` VALUES ('1', '单片机原理及应用--基于Proteus和KeilC', '林立、张俊亮', '1234567891', '可以借阅', '二楼203'); 75 | INSERT INTO `t_books` VALUES ('2', 'Java语言程序设计', '胡森、李润荣', '8546564956', '已被借出', '三楼305'); 76 | INSERT INTO `t_books` VALUES ('3', '语文(大学)', '王花花、李莫愁', '154685123456', '已被借出', '四楼408'); 77 | INSERT INTO `t_books` VALUES ('7', '数据库原理及应用教程', '陈志泊', '987998956564', '可以借阅', '109'); 78 | INSERT INTO `t_books` VALUES ('8', '高等数学(上)', '同济大学教学系', '1212121212', '可以借阅', '图书馆301'); 79 | INSERT INTO `t_books` VALUES ('9', '高等数学(下)', '同济大学教学系', '1212121213', '可以借阅', '图书馆301'); 80 | INSERT INTO `t_books` VALUES ('10', '母猪的产后护理', '母猪出版社编', '797979797979', '可以借阅', '四楼的犄角旮旯'); 81 | INSERT INTO `t_books` VALUES ('12', '守塔与补刀', 'theshy', '5464686465456', '可以借阅', '505'); 82 | INSERT INTO `t_books` VALUES ('13', '郭麒麟奋斗史', '郭麒麟', '998998998', '可以借阅', '333'); 83 | INSERT INTO `t_books` VALUES ('14', '我为什么这么强?', 'god', '68686868', '已被借出', '465'); 84 | INSERT INTO `t_books` VALUES ('15', '英语', 'LiHua', '4948156456', '已被借出', '图书馆三楼的犄角旮旯'); 85 | INSERT INTO `t_books` VALUES ('17', '混子的素养', '混', '2222', '已被借出', '323'); 86 | 87 | -- ---------------------------- 88 | -- Table structure for `t_user` 89 | -- ---------------------------- 90 | DROP TABLE IF EXISTS `t_user`; 91 | CREATE TABLE `t_user` ( 92 | `uid` int(11) NOT NULL AUTO_INCREMENT, 93 | `username` varchar(30) NOT NULL, 94 | `password` varchar(30) NOT NULL, 95 | `name` varchar(30) NOT NULL, 96 | PRIMARY KEY (`uid`), 97 | UNIQUE KEY `unique` (`username`) 98 | ) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8; 99 | 100 | -- ---------------------------- 101 | -- Records of t_user 102 | -- ---------------------------- 103 | INSERT INTO `t_user` VALUES ('1', '12345', '12345', '作妖'); 104 | INSERT INTO `t_user` VALUES ('2', 'demaxiya', 'demaxiya', '德玛西亚之力'); 105 | INSERT INTO `t_user` VALUES ('3', 'aiouniya', 'aiouniya', '艾欧尼亚'); 106 | INSERT INTO `t_user` VALUES ('5', 'demaxiyaa', 'demaxiya', 'sadasd'); 107 | INSERT INTO `t_user` VALUES ('6', 'guodegang', 'guodegang', '郭德纲'); 108 | -------------------------------------------------------------------------------- /src/dao/AdminDao.java: -------------------------------------------------------------------------------- 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 | 8 | import model.Admin; 9 | import model.Book; 10 | import utils.JDBCUtil; 11 | 12 | public class AdminDao extends UserDao { 13 | private Connection conn = null; 14 | private PreparedStatement ps = null; 15 | private ResultSet rs = null; 16 | 17 | 18 | 19 | /* 20 | * 通过账号、密码查询管理员 21 | */ 22 | public Admin findAllByAdminAndPasswd(String userName, String userPassword) { 23 | Admin admin = null; 24 | try { 25 | String sql = "select * from t_admin where username=? and password=?"; 26 | conn = JDBCUtil.getConnection(); 27 | ps = conn.prepareStatement(sql); 28 | ps.setString(1, userName); 29 | ps.setString(2, userPassword); 30 | rs = ps.executeQuery(); 31 | 32 | if(rs.next()) { 33 | Integer id = rs.getInt("aid"); 34 | String username = rs.getString("username"); 35 | String password = rs.getString("password"); 36 | String name = rs.getString("name"); 37 | 38 | admin = new Admin(id, username, password, name); 39 | } 40 | } catch (SQLException e) { 41 | e.printStackTrace(); 42 | } catch (ClassNotFoundException e) { 43 | // TODO Auto-generated catch block 44 | e.printStackTrace(); 45 | }finally { 46 | JDBCUtil.release(rs, ps, conn); 47 | } 48 | 49 | return admin; 50 | } 51 | 52 | 53 | /** 54 | * - 添加图书 55 | * @param book 56 | */ 57 | public String addBook(Book book) { 58 | String msg = ""; 59 | try { 60 | String sql = "insert into t_books (bookname,author,number,borrow,location) values (?,?,?,?,?)"; 61 | conn = JDBCUtil.getConnection(); 62 | ps = conn.prepareStatement(sql); 63 | ps.setString(1, book.getBookname()); 64 | ps.setString(2, book.getAuthor()); 65 | ps.setLong(3, book.getNum()); 66 | ps.setString(4, book.getBorrow()); 67 | ps.setString(5, book.getLocation()); 68 | 69 | int flag = ps.executeUpdate(); 70 | if(flag == 1) { 71 | msg = "添加成功!"; 72 | }else { 73 | msg = "添加失败!"; 74 | } 75 | } catch (SQLException e) { 76 | e.printStackTrace(); 77 | } catch (ClassNotFoundException e) { 78 | // TODO Auto-generated catch block 79 | e.printStackTrace(); 80 | }finally { 81 | JDBCUtil.release(ps, conn); 82 | } 83 | return msg; 84 | } 85 | 86 | /** 87 | * - 根据图书编号删除图书 88 | */ 89 | public String deleteByNum(String num) { 90 | String msg = ""; 91 | try { 92 | String sql = "delete from t_books where number = ?"; 93 | conn = JDBCUtil.getConnection(); 94 | ps = conn.prepareStatement(sql); 95 | ps.setString(1, num); 96 | int flag = ps.executeUpdate(); 97 | if (flag == 1) { 98 | msg = "删除成功"; 99 | }else { 100 | msg = "删除失败"; 101 | } 102 | } catch (SQLException e) { 103 | // TODO Auto-generated catch block 104 | e.printStackTrace(); 105 | } catch (ClassNotFoundException e) { 106 | // TODO Auto-generated catch block 107 | e.printStackTrace(); 108 | }finally { 109 | JDBCUtil.release(ps, conn); 110 | } 111 | return msg; 112 | } 113 | 114 | /** 115 | * - 修改图书 116 | * @param book 117 | */ 118 | public String updateBook(Book book) { 119 | String msg = ""; 120 | try { 121 | String sql = "update t_books set bookname=?, author=?, number=?, borrow=?, location=? where id=?"; 122 | conn = JDBCUtil.getConnection(); 123 | ps = conn.prepareStatement(sql); 124 | ps.setString(1, book.getBookname()); 125 | ps.setString(2, book.getAuthor()); 126 | ps.setString(3, book.getNum().toString()); 127 | ps.setString(4, book.getBorrow()); 128 | ps.setString(5, book.getLocation()); 129 | ps.setString(6, book.getId().toString()); 130 | 131 | int flag = ps.executeUpdate(); 132 | if (flag == 1) { 133 | msg = "修改成功!"; 134 | }else { 135 | msg = "修改失败!"; 136 | } 137 | } catch (SQLException e) { 138 | e.printStackTrace(); 139 | } catch (ClassNotFoundException e) { 140 | // TODO Auto-generated catch block 141 | e.printStackTrace(); 142 | }finally { 143 | JDBCUtil.release(ps, conn); 144 | } 145 | return msg; 146 | } 147 | } 148 | -------------------------------------------------------------------------------- /src/dao/RegistDao.java: -------------------------------------------------------------------------------- 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 | 8 | import model.Admin; 9 | import model.AdminCode; 10 | import model.Register; 11 | import model.User; 12 | import utils.JDBCUtil; 13 | 14 | public class RegistDao { 15 | 16 | private Connection conn = null; 17 | private PreparedStatement ps = null; 18 | private ResultSet rs = null; 19 | 20 | // public RegistDao() { 21 | // try { 22 | // conn = JDBCUtil.getConnection(); 23 | // } catch (ClassNotFoundException | SQLException e) { 24 | // e.printStackTrace(); 25 | // } 26 | // } 27 | 28 | /** 29 | * - 检查密钥是否存在 30 | * @param adminCode 31 | * @return 32 | */ 33 | public AdminCode checkAdminCode(String adminCode) { 34 | AdminCode adminCodeMold = new AdminCode(); 35 | try { 36 | String sql = "select * from admin_code where code = ?"; 37 | conn = JDBCUtil.getConnection(); 38 | ps = conn.prepareStatement(sql); 39 | ps.setString(1, adminCode); 40 | 41 | rs = ps.executeQuery(); 42 | 43 | if (rs.next()) { 44 | adminCodeMold.setId(rs.getInt("id")); 45 | adminCodeMold.setCode(rs.getString("code")); 46 | adminCodeMold.setCount(rs.getInt("count")); 47 | }else { 48 | return null; 49 | } 50 | 51 | } catch (SQLException e) { 52 | // TODO Auto-generated catch block 53 | e.printStackTrace(); 54 | } catch (ClassNotFoundException e) { 55 | // TODO Auto-generated catch block 56 | e.printStackTrace(); 57 | }finally { 58 | JDBCUtil.release(rs, ps, conn); 59 | } 60 | return adminCodeMold; 61 | } 62 | 63 | /** 64 | * - 修改管理员密钥使用次数 65 | * @param adminCodeMold 66 | */ 67 | public void updateAdminCode(AdminCode adminCodeMold) { 68 | try { 69 | String sql = "update admin_code set count = ? where code = ?"; 70 | conn = JDBCUtil.getConnection(); 71 | ps = conn.prepareStatement(sql); 72 | ps.setInt(1, adminCodeMold.getCount()-1); 73 | ps.setString(2, adminCodeMold.getCode()); 74 | 75 | ps.executeUpdate(); 76 | } catch (SQLException e) { 77 | e.printStackTrace(); 78 | } catch (ClassNotFoundException e) { 79 | // TODO Auto-generated catch block 80 | e.printStackTrace(); 81 | }finally { 82 | JDBCUtil.release( ps, conn); 83 | } 84 | } 85 | 86 | /** 87 | * - 普通用户注册 88 | * @param register 89 | */ 90 | public String userRegist(Register register) { 91 | String msg = ""; 92 | try { 93 | String sql = "insert into t_user (username,password,name) values (?,?,?)"; 94 | conn = JDBCUtil.getConnection(); 95 | ps = conn.prepareStatement(sql); 96 | ps.setString(1, register.getCode()); 97 | ps.setString(2, register.getPassword()); 98 | ps.setString(3, register.getName()); 99 | 100 | int flag = ps.executeUpdate(); 101 | if (flag == 1) { 102 | msg = "注册成功!"; 103 | }else { 104 | msg = "注册失败!"; 105 | } 106 | } catch (SQLException e) { 107 | e.printStackTrace(); 108 | } catch (ClassNotFoundException e) { 109 | // TODO Auto-generated catch block 110 | e.printStackTrace(); 111 | }finally { 112 | JDBCUtil.release(ps, conn); 113 | } 114 | return msg; 115 | } 116 | 117 | /** 118 | * - 管理员注册 119 | * @param register 120 | * @return 121 | */ 122 | public String adminRegist(Register register) { 123 | String msg = ""; 124 | try { 125 | String sql = "insert into t_admin (username,password,name) values (?,?,?)"; 126 | conn = JDBCUtil.getConnection(); 127 | ps = conn.prepareStatement(sql); 128 | ps.setString(1, register.getCode()); 129 | ps.setString(2, register.getPassword()); 130 | ps.setString(3, register.getName()); 131 | 132 | int flag = ps.executeUpdate(); 133 | if (flag == 1) { 134 | msg = "注册成功!"; 135 | }else { 136 | msg = "注册失败!"; 137 | } 138 | } catch (SQLException e) { 139 | e.printStackTrace(); 140 | } catch (ClassNotFoundException e) { 141 | // TODO Auto-generated catch block 142 | e.printStackTrace(); 143 | }finally { 144 | JDBCUtil.release(ps, conn); 145 | } 146 | return msg; 147 | } 148 | 149 | /** 150 | * 通过账号查找普通用户 151 | */ 152 | public User findUserByCode(String code) { 153 | User user = null; 154 | try { 155 | String sql = "select * from t_user where username = ?"; 156 | conn = JDBCUtil.getConnection(); 157 | ps = conn.prepareStatement(sql); 158 | ps.setString(1, code); 159 | rs = ps.executeQuery(); 160 | 161 | while (rs.next()) { 162 | Integer uid = rs.getInt("uid"); 163 | String username = rs.getString("username"); 164 | String password = rs.getString("password"); 165 | String name = rs.getString("name"); 166 | user = new User(uid, username, password, name); 167 | } 168 | } catch (SQLException e) { 169 | e.printStackTrace(); 170 | } catch (ClassNotFoundException e) { 171 | // TODO Auto-generated catch block 172 | e.printStackTrace(); 173 | }finally { 174 | JDBCUtil.release(rs, ps, conn); 175 | } 176 | return user; 177 | } 178 | 179 | /** 180 | * 通过账号查找管理员 181 | * @param code 182 | * @return 183 | */ 184 | public Admin findAdminByCode(String code) { 185 | Admin admin = null; 186 | try { 187 | String sql = "select * from t_admin where username = ?"; 188 | conn = JDBCUtil.getConnection(); 189 | ps = conn.prepareStatement(sql); 190 | ps.setString(1, code); 191 | rs = ps.executeQuery(); 192 | 193 | while (rs.next()) { 194 | Integer aid = rs.getInt("aid"); 195 | String name = rs.getString("name"); 196 | String username = rs.getString("username"); 197 | String password = rs.getString("password"); 198 | admin = new Admin(aid, username, password, name); 199 | } 200 | } catch (SQLException e) { 201 | e.printStackTrace(); 202 | } catch (ClassNotFoundException e) { 203 | // TODO Auto-generated catch block 204 | e.printStackTrace(); 205 | }finally { 206 | JDBCUtil.release(rs, ps, conn); 207 | } 208 | return admin; 209 | } 210 | 211 | } 212 | -------------------------------------------------------------------------------- /src/dao/UserDao.java: -------------------------------------------------------------------------------- 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.util.ArrayList; 8 | import java.util.List; 9 | 10 | import model.Book; 11 | import model.User; 12 | import utils.JDBCUtil; 13 | 14 | public class UserDao { 15 | private Connection conn = null; 16 | private PreparedStatement ps = null; 17 | private ResultSet rs = null; 18 | 19 | 20 | 21 | /* 22 | * 通过账号、密码查询用户 23 | */ 24 | public User findAllByUserAndPasswd(String userName, String userPassword) { 25 | User user = null; 26 | Integer id = null; 27 | String username = null; 28 | String password = null; 29 | String name = null; 30 | try { 31 | String sql = "select * from t_user where username=? and password=?"; 32 | conn = JDBCUtil.getConnection(); 33 | ps = conn.prepareStatement(sql); 34 | ps.setString(1, userName); 35 | ps.setString(2, userPassword); 36 | rs = ps.executeQuery();//执行sql查询,返回结果集 37 | 38 | //在使用rs.getxxx()之前,必须使用rs.next(),否则会发生错误 39 | if(rs.next()) { 40 | id = rs.getInt("uid"); 41 | username = rs.getString("username"); 42 | password = rs.getString("password"); 43 | name = rs.getString("name"); 44 | 45 | user = new User(id, username,password, name); 46 | } 47 | } catch (SQLException e) { 48 | e.printStackTrace(); 49 | } catch (ClassNotFoundException e) { 50 | // TODO Auto-generated catch block 51 | e.printStackTrace(); 52 | }finally { 53 | JDBCUtil.release(rs, ps, conn); 54 | } 55 | return user; 56 | } 57 | 58 | /** 59 | * 通过书名检索 60 | */ 61 | public List findBookByBookname(String bookname) { 62 | List books = new ArrayList<>(); 63 | try { 64 | String sql = "select * from t_books where bookname like ?"; 65 | conn = JDBCUtil.getConnection(); 66 | ps = conn.prepareStatement(sql); 67 | ps.setString(1, "%"+bookname+"%"); 68 | rs = ps.executeQuery(); 69 | 70 | while(rs.next()) { 71 | books.add(new Book(rs.getInt("id"),rs.getString("bookname"),rs.getString("author"), 72 | rs.getLong("number"),rs.getString("borrow"),rs.getString("location"))); 73 | } 74 | } catch (SQLException | ClassNotFoundException e) { 75 | e.printStackTrace(); 76 | }finally { 77 | JDBCUtil.release(rs, ps, conn); 78 | } 79 | return books; 80 | } 81 | 82 | /** 83 | * 通过作者检索 84 | * 85 | */ 86 | public List findBookByAuthor(String author) { 87 | List books = new ArrayList<>(); 88 | try { 89 | String sql = "select * from t_books where author like ?"; 90 | conn = JDBCUtil.getConnection(); 91 | ps = conn.prepareStatement(sql); 92 | ps.setString(1, "%"+author+"%"); 93 | rs = ps.executeQuery(); 94 | 95 | while(rs.next()) { 96 | books.add(new Book(rs.getInt("id"),rs.getString("bookname"),rs.getString("author"), 97 | rs.getLong("number"),rs.getString("borrow"),rs.getString("location"))); 98 | } 99 | } catch (SQLException | ClassNotFoundException e) { 100 | e.printStackTrace(); 101 | }finally { 102 | JDBCUtil.release(rs, ps, conn); 103 | } 104 | return books; 105 | } 106 | 107 | /** 108 | * 通过编号检索 109 | */ 110 | public List findBookByNum(String num) { 111 | List books = new ArrayList<>(); 112 | try { 113 | String sql = "select * from t_books where number = ?"; 114 | conn = JDBCUtil.getConnection(); 115 | ps = conn.prepareStatement(sql); 116 | ps.setString(1, num); 117 | rs = ps.executeQuery(); 118 | 119 | while(rs.next()) { 120 | books.add(new Book(rs.getInt("id"),rs.getString("bookname"),rs.getString("author"), 121 | rs.getLong("number"),rs.getString("borrow"),rs.getString("location"))); 122 | } 123 | } catch (SQLException | ClassNotFoundException e) { 124 | e.printStackTrace(); 125 | }finally { 126 | JDBCUtil.release(rs, ps, conn); 127 | } 128 | return books; 129 | } 130 | 131 | public List findAllBook() { 132 | List books = new ArrayList<>(); 133 | try { 134 | String sql = "select * from t_books"; 135 | conn = JDBCUtil.getConnection(); 136 | ps = conn.prepareStatement(sql); 137 | rs = ps.executeQuery(); 138 | 139 | while(rs.next()) { 140 | books.add(new Book(rs.getInt("id"),rs.getString("bookname"),rs.getString("author"), 141 | rs.getLong("number"),rs.getString("borrow"),rs.getString("location"))); 142 | } 143 | } catch (SQLException | ClassNotFoundException e) { 144 | e.printStackTrace(); 145 | }finally { 146 | JDBCUtil.release(rs, ps, conn); 147 | } 148 | return books; 149 | } 150 | } 151 | -------------------------------------------------------------------------------- /src/images/background.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/narcissus1024/java-swing-LibrarySystem/cda412b234ebdb226e13eed8e0e213d77c0e6cc4/src/images/background.jpg -------------------------------------------------------------------------------- /src/images/logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/narcissus1024/java-swing-LibrarySystem/cda412b234ebdb226e13eed8e0e213d77c0e6cc4/src/images/logo.png -------------------------------------------------------------------------------- /src/lib/mysql-connector-java-5.1.25-bin.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/narcissus1024/java-swing-LibrarySystem/cda412b234ebdb226e13eed8e0e213d77c0e6cc4/src/lib/mysql-connector-java-5.1.25-bin.jar -------------------------------------------------------------------------------- /src/model/Admin.java: -------------------------------------------------------------------------------- 1 | package model; 2 | 3 | 4 | public class Admin { 5 | private Integer id; 6 | private String adminName; 7 | private String adminPassword; 8 | private String name; 9 | 10 | public Admin(Integer id, String adminName, String adminPassword, String name) { 11 | super(); 12 | this.id = id; 13 | this.adminName = adminName; 14 | this.adminPassword = adminPassword; 15 | this.name = name; 16 | } 17 | public Admin() { 18 | super(); 19 | } 20 | public Integer getId() { 21 | return id; 22 | } 23 | public void setId(Integer id) { 24 | this.id = id; 25 | } 26 | public String getAdminName() { 27 | return adminName; 28 | } 29 | public void setAdminName(String adminName) { 30 | this.adminName = adminName; 31 | } 32 | public String getAdminPassword() { 33 | return adminPassword; 34 | } 35 | public void setAdminPassword(String adminPassword) { 36 | this.adminPassword = adminPassword; 37 | } 38 | public String getName() { 39 | return name; 40 | } 41 | public void setName(String name) { 42 | this.name = name; 43 | } 44 | 45 | 46 | } 47 | -------------------------------------------------------------------------------- /src/model/AdminCode.java: -------------------------------------------------------------------------------- 1 | package model; 2 | 3 | /** 4 | * - 密钥类 5 | * @author Administrator 6 | * 7 | */ 8 | public class AdminCode { 9 | private int id; 10 | private String code; 11 | private int count; 12 | 13 | public AdminCode() { 14 | super(); 15 | } 16 | 17 | public AdminCode(String code, int count) { 18 | super(); 19 | this.code = code; 20 | this.count = count; 21 | } 22 | 23 | public AdminCode(int id, String code, int count) { 24 | this.id = id; 25 | this.code = code; 26 | this.count = count; 27 | } 28 | 29 | public int getId() { 30 | return id; 31 | } 32 | public void setId(int id) { 33 | this.id = id; 34 | } 35 | public String getCode() { 36 | return code; 37 | } 38 | public void setCode(String code) { 39 | this.code = code; 40 | } 41 | public int getCount() { 42 | return count; 43 | } 44 | public void setCount(int count) { 45 | this.count = count; 46 | } 47 | 48 | } 49 | -------------------------------------------------------------------------------- /src/model/Book.java: -------------------------------------------------------------------------------- 1 | package model; 2 | 3 | public class Book { 4 | private Integer id; 5 | private String bookname; 6 | private String author; 7 | private Long num; 8 | private String borrow; //是否可以借阅 9 | private String location;//存放位置 10 | 11 | public Book() { 12 | } 13 | 14 | public Book(Integer id, String bookname, String author, Long num, String borrow, String location) { 15 | super(); 16 | this.id = id; 17 | this.bookname = bookname; 18 | this.author = author; 19 | this.num = num; 20 | this.borrow = borrow; 21 | this.location = location; 22 | } 23 | 24 | public Book(String bookname, String author, Long num, String borrow, String location) { 25 | super(); 26 | this.bookname = bookname; 27 | this.author = author; 28 | this.num = num; 29 | this.borrow = borrow; 30 | this.location = location; 31 | } 32 | 33 | public Integer getId() { 34 | return id; 35 | } 36 | 37 | public void setId(Integer id) { 38 | this.id = id; 39 | } 40 | 41 | public String getBookname() { 42 | return bookname; 43 | } 44 | 45 | public void setBookname(String bookname) { 46 | this.bookname = bookname; 47 | } 48 | 49 | public String getAuthor() { 50 | return author; 51 | } 52 | 53 | public void setAuthor(String author) { 54 | this.author = author; 55 | } 56 | 57 | public Long getNum() { 58 | return num; 59 | } 60 | 61 | public void setNum(Long num) { 62 | this.num = num; 63 | } 64 | 65 | public String getBorrow() { 66 | return borrow; 67 | } 68 | 69 | public void setBorrow(String borrow) { 70 | this.borrow = borrow; 71 | } 72 | 73 | public String getLocation() { 74 | return location; 75 | } 76 | 77 | public void setLocation(String location) { 78 | this.location = location; 79 | } 80 | 81 | 82 | } 83 | -------------------------------------------------------------------------------- /src/model/Register.java: -------------------------------------------------------------------------------- 1 | package model; 2 | 3 | public class Register { 4 | private String name; //姓名 5 | private String code; //账号 6 | private String password; //密码 7 | private String adminCode; //管理员密钥 8 | private String mold; //注册类型 9 | 10 | public Register(String name, String code, String password, String adminCode, String mold) { 11 | super(); 12 | this.name = name; 13 | this.code = code; 14 | this.password = password; 15 | this.adminCode = adminCode; 16 | this.mold = mold; 17 | } 18 | 19 | public Register() { 20 | } 21 | 22 | public String getName() { 23 | return name; 24 | } 25 | 26 | public void setName(String name) { 27 | this.name = name; 28 | } 29 | 30 | public String getCode() { 31 | return code; 32 | } 33 | 34 | public void setCode(String code) { 35 | this.code = code; 36 | } 37 | 38 | public String getPassword() { 39 | return password; 40 | } 41 | 42 | public void setPassword(String password) { 43 | this.password = password; 44 | } 45 | 46 | public String getAdminCode() { 47 | return adminCode; 48 | } 49 | 50 | public void setAdminCode(String adminCode) { 51 | this.adminCode = adminCode; 52 | } 53 | 54 | public String getMold() { 55 | return mold; 56 | } 57 | 58 | public void setMold(String mold) { 59 | this.mold = mold; 60 | } 61 | 62 | 63 | 64 | 65 | 66 | } 67 | -------------------------------------------------------------------------------- /src/model/User.java: -------------------------------------------------------------------------------- 1 | package model; 2 | 3 | public class User { 4 | private Integer id; 5 | private String userName; 6 | private String userPassword; 7 | private String name; 8 | 9 | 10 | 11 | public User() { 12 | super(); 13 | } 14 | 15 | 16 | public User(Integer id, String userName, String userPassword, String name) { 17 | super(); 18 | this.id = id; 19 | this.userName = userName; 20 | this.userPassword = userPassword; 21 | this.name = name; 22 | } 23 | 24 | 25 | public Integer getId() { 26 | return id; 27 | } 28 | 29 | 30 | public void setId(Integer id) { 31 | this.id = id; 32 | } 33 | 34 | 35 | public String getUserName() { 36 | return userName; 37 | } 38 | 39 | 40 | public void setUserName(String userName) { 41 | this.userName = userName; 42 | } 43 | 44 | 45 | public String getUserPassword() { 46 | return userPassword; 47 | } 48 | 49 | 50 | public void setUserPassword(String userPassword) { 51 | this.userPassword = userPassword; 52 | } 53 | 54 | 55 | public String getName() { 56 | return name; 57 | } 58 | 59 | 60 | public void setName(String name) { 61 | this.name = name; 62 | } 63 | 64 | 65 | 66 | 67 | } 68 | -------------------------------------------------------------------------------- /src/service/AdminService.java: -------------------------------------------------------------------------------- 1 | package service; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | import dao.AdminDao; 7 | import model.Book; 8 | 9 | public class AdminService extends UserService { 10 | AdminDao adminDao = new AdminDao(); 11 | 12 | /** 13 | * - 添加图书 14 | * @param book 15 | * @return 16 | */ 17 | public String addBook(Book book) { 18 | List books = new ArrayList(); 19 | String msg = ""; 20 | //首先,判断输入的数据合法性 21 | if(book.getAuthor().trim().equals("") || book.getBookname().trim().equals("") || 22 | book.getBorrow().trim().equals("") || book.getLocation().trim().equals("") || 23 | book.getNum()<0 || book.getNum().toString().trim().equals("")) { 24 | msg = "请输入正确信息!"; 25 | }else {//其次,判断该图书是否存在 26 | books = adminDao.findBookByNum(book.getNum().toString()); 27 | if (books.isEmpty()) {//不存在 28 | msg = adminDao.addBook(book); 29 | }else {//存在 30 | msg = "该图书已存在!"; 31 | } 32 | } 33 | return msg; 34 | } 35 | 36 | /** 37 | * - 根据图书编号查找图书 38 | */ 39 | public Book findByNumber(String number) { 40 | List books = new ArrayList(); 41 | if (number.trim().equals("")) { 42 | return null; 43 | }else { 44 | books = adminDao.findBookByNum(number); 45 | if (books.isEmpty()) { 46 | return null; 47 | } 48 | } 49 | return books.get(0); 50 | } 51 | 52 | /** 53 | * - 根据图书编号删除图书 54 | * @param string 55 | */ 56 | public String deleteByNum(String num) { 57 | return adminDao.deleteByNum(num); 58 | } 59 | 60 | /** 61 | * - 修改图书 62 | * @param book 63 | */ 64 | public String update(Book book) { 65 | String msg = ""; 66 | if(book.getAuthor().trim().equals("") || book.getBookname().trim().equals("") || 67 | book.getBorrow().trim().equals("") || book.getLocation().trim().equals("") || 68 | book.getNum()<0 || book.getNum().toString().trim().equals("")) { 69 | msg = "请输入正确信息!"; 70 | }else { 71 | msg = adminDao.updateBook(book); 72 | } 73 | return msg; 74 | } 75 | 76 | } 77 | -------------------------------------------------------------------------------- /src/service/RegistService.java: -------------------------------------------------------------------------------- 1 | package service; 2 | 3 | import dao.RegistDao; 4 | import model.Admin; 5 | import model.AdminCode; 6 | import model.Register; 7 | import model.User; 8 | 9 | public class RegistService { 10 | 11 | private RegistDao registDao = new RegistDao(); 12 | 13 | /** 14 | * - 注册 15 | * - 逻辑:首先验证姓名、账号、密码是否包含空格或者为空。是-->返回错误信息 16 | * - 否-->判断是否为普通用户注册。是-->注册 17 | * - 否-->判断密钥是否正确。是->注册 18 | * 否->结束注册,返回错误信息 19 | * @param register 20 | * @return 21 | */ 22 | public String regist(Register register) { 23 | String msg = ""; 24 | if(register.getName().contains(" ") || register.getName().equals("") || 25 | register.getCode().contains(" ") || register.getCode().equals("") || 26 | register.getPassword().contains(" ") || register.getPassword().equals("")) { 27 | msg = "输入信息不能包含空格或者空着不填!"; 28 | }else if(register.getName().length() < 2 || register.getCode().length() < 5|| register.getPassword().length()<5){ 29 | //姓名长度大于等于2,账号大于等于5,密码大于等于5 30 | msg = "账号密码最少5位,姓名最少2位哦"; 31 | }else if (isExist(register)) { 32 | //用户已经存在(注册) 33 | msg = "该账号已存在"; 34 | }else if(register.getMold().equals("普通用户")) { 35 | //普通用户注册 36 | msg = registDao.userRegist(register); 37 | }else if(checkAdminCode(register.getAdminCode())){//管理员注册,首先验证密钥是否正确 38 | //密钥正确,管理员注册 39 | msg = registDao.adminRegist(register); 40 | }else { 41 | //密钥不正确 42 | msg = "密钥不对或者密钥使用过多哦~"; 43 | } 44 | return msg; 45 | } 46 | 47 | /** 48 | * - 验证密钥 49 | * @param adminCode 50 | * @return 51 | */ 52 | private boolean checkAdminCode(String adminCode) { 53 | AdminCode adminCodeMold = new AdminCode(); 54 | adminCodeMold = registDao.checkAdminCode(adminCode); 55 | //密钥是否存在 56 | if (adminCodeMold != null) { 57 | //检查密钥使用次数是否使用完 58 | if (adminCodeMold.getCount() != 0) { 59 | //没有使用完,有效次数减1 60 | registDao.updateAdminCode(adminCodeMold); 61 | return true; 62 | }else { 63 | //使用完 64 | return false; 65 | } 66 | }else { 67 | //不存在 68 | return false; 69 | } 70 | } 71 | 72 | /** 73 | * 判断用户或管理员是否注册 74 | * @param register 75 | * @return 76 | */ 77 | private boolean isExist(Register register) { 78 | User user = registDao.findUserByCode(register.getCode()); 79 | if (user != null) { 80 | //用户存在 81 | return true; 82 | }else { 83 | //用户不存在,查找是否是管理员 84 | Admin admin = registDao.findAdminByCode(register.getCode()); 85 | if (admin != null) { 86 | //管理员 87 | return true; 88 | } 89 | } 90 | //既不是普通用户,也不是管理员=不存在 91 | return false; 92 | } 93 | } 94 | -------------------------------------------------------------------------------- /src/service/UserService.java: -------------------------------------------------------------------------------- 1 | package service; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | import dao.AdminDao; 7 | import dao.UserDao; 8 | import model.Admin; 9 | import model.Book; 10 | import model.User; 11 | 12 | /** 13 | * - 对普通用户数据进行操作的一层 14 | * @author Administrator 15 | * 16 | */ 17 | public class UserService { 18 | UserDao userDao = new UserDao(); 19 | AdminDao adminDao = new AdminDao(); 20 | 21 | //验证账号密码格式 22 | private boolean checkCode(String userName, String userPassword) { 23 | //账号正确性的判断、管理员与普通用户的判断 24 | if(userName.length()<5 || userName.contains(" ")) { 25 | //账号输入错误 26 | // System.out.println("账号输入错误"); 27 | return false; 28 | }else if(userPassword.length()<5 || userPassword.contains(" ")){ 29 | //密码输入错误 30 | // System.out.println("密码输入错误"); 31 | return false; 32 | }else { 33 | return true; 34 | } 35 | } 36 | 37 | //普通用户登录 38 | public User login(String userName, String userPassword) { 39 | boolean flag = checkCode(userName, userPassword); 40 | if (flag) { 41 | //密码格式正确,检查是否注册 42 | User user = userDao.findAllByUserAndPasswd(userName, userPassword); 43 | if (user!=null) { 44 | return user; 45 | }else { 46 | return null; 47 | } 48 | }else { 49 | //密码格式错误,返回空 50 | return null; 51 | } 52 | } 53 | 54 | //管理员登录 55 | public Admin adminLogin(String userName, String userPassword) { 56 | boolean flag = checkCode(userName, userPassword); 57 | if (flag) { 58 | //密码格式正确,检查是否注册 59 | Admin admin = adminDao.findAllByAdminAndPasswd(userName, userPassword); 60 | if (admin!=null) { 61 | return admin; 62 | }else { 63 | return null; 64 | } 65 | }else { 66 | //密码格式错误,返回空 67 | return null; 68 | } 69 | } 70 | 71 | /** 72 | * 检索图书 73 | */ 74 | public List findBooks(String findMsg, int flag) { 75 | List books = new ArrayList<>(); 76 | if(findMsg.equals("") && flag != 3) { 77 | return books; 78 | }else { 79 | switch(flag) { 80 | case 0: 81 | books = userDao.findBookByBookname(findMsg); 82 | break; 83 | case 1: 84 | books = userDao.findBookByAuthor(findMsg); 85 | break; 86 | case 2: 87 | books = userDao.findBookByNum(findMsg); 88 | break; 89 | case 3: 90 | books = userDao.findAllBook(); 91 | break; 92 | } 93 | } 94 | 95 | return books; 96 | } 97 | } 98 | -------------------------------------------------------------------------------- /src/test/Test01.java: -------------------------------------------------------------------------------- 1 | package test; 2 | 3 | import model.User; 4 | import ui.UserView; 5 | 6 | /** 7 | * - 普通用户窗口的测试 8 | * @author Administrator 9 | * 10 | */ 11 | public class Test01 { 12 | public static void main(String[] args) { 13 | new UserView(new User(1,"9956a","12345","快乐压缩")); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/test/Test02.java: -------------------------------------------------------------------------------- 1 | package test; 2 | 3 | public class Test02 { 4 | public static void main(String[] args) { 5 | int[][] a = new int[4][]; 6 | a[1][1] = 1; 7 | System.out.println(a); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /src/test/TestAdminView.java: -------------------------------------------------------------------------------- 1 | package test; 2 | 3 | import ui.AdminView; 4 | 5 | public class TestAdminView { 6 | public static void main(String[] args) { 7 | new AdminView(); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /src/test/TestUserDao.java: -------------------------------------------------------------------------------- 1 | package test; 2 | 3 | import java.sql.Connection; 4 | import java.sql.PreparedStatement; 5 | import java.sql.ResultSet; 6 | import java.sql.SQLException; 7 | import java.util.ArrayList; 8 | import java.util.List; 9 | 10 | import dao.UserDao; 11 | import model.Book; 12 | 13 | public class TestUserDao { 14 | public static void main(String[] args) { 15 | 16 | UserDao userdao = new UserDao(); 17 | List books = null; 18 | 19 | // books = userdao.findBookByBookname("b");//成功 20 | // books = userdao.findBookByAuthor("胡森");//ok 21 | books = userdao.findBookByNum("1234567891");//ok 22 | 23 | System.out.println(books.get(0).getBookname()); 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /src/ui/AdminView.java: -------------------------------------------------------------------------------- 1 | package ui; 2 | 3 | import java.awt.Color; 4 | import java.awt.Container; 5 | import java.awt.Dimension; 6 | import java.awt.FlowLayout; 7 | import java.awt.Font; 8 | import java.awt.event.ActionEvent; 9 | import java.awt.event.ActionListener; 10 | import java.util.ArrayList; 11 | import java.util.List; 12 | 13 | import javax.swing.ComboBoxModel; 14 | import javax.swing.DefaultComboBoxModel; 15 | import javax.swing.JButton; 16 | import javax.swing.JComboBox; 17 | import javax.swing.JDialog; 18 | import javax.swing.JFrame; 19 | import javax.swing.JLabel; 20 | import javax.swing.JPanel; 21 | import javax.swing.JScrollBar; 22 | import javax.swing.JScrollPane; 23 | import javax.swing.JTable; 24 | import javax.swing.JTextField; 25 | import javax.swing.table.DefaultTableModel; 26 | import javax.swing.table.TableModel; 27 | 28 | import model.Admin; 29 | import model.Book; 30 | import service.AdminService; 31 | import utils.Tools; 32 | 33 | /** 34 | * - 管理员用户界面 35 | * - 高:500 36 | * - 宽:800 37 | * - 快捷键:alt+shift+r:批量修改变量名 38 | * @author Administrator 39 | * 40 | */ 41 | public class AdminView { 42 | //service和admin 43 | AdminService adminService = new AdminService(); 44 | private Admin admin = new Admin(); 45 | //主窗口 46 | private JFrame adminView = new JFrame("图书管理系统(管理员)"); 47 | private Container c = adminView.getContentPane(); 48 | //顶部面板组件 49 | private JPanel topPanel = new JPanel(); //顶部面板 50 | private JLabel topAdminMsg = new JLabel(); //管理员信息 51 | private JComboBox topFindBy = new JComboBox(); //下拉列表 52 | private String items[] = {"通过书名检索", "通过作者检索", "通过编号检索","检索所有图书"}; 53 | private JTextField topInput = new JTextField(); //检索输入框 54 | private JLabel topTime = new JLabel(); //用来显示登录时间 55 | //侧边面板和组件 56 | private JPanel sidePanel = new JPanel(); //侧边面板 57 | private JButton sideSubmit = new JButton("检索"); //查 58 | private JButton sideAddBtn = new JButton("添加图书"); //增 59 | private JButton sideDeleteBtn = new JButton("删除图书"); //删 60 | private JButton sideUpdateBtn = new JButton("修改图书"); //改 61 | //功能面板 62 | private JPanel addPanel = new JPanel(); 63 | private JPanel deletePanel = new JPanel(); 64 | 65 | private JScrollPane findPanel = new JScrollPane(); //检索图书表格面板 66 | private JPanel updataPanel = new JPanel(); 67 | private JLabel functionMsg = new JLabel(); //当前正在使用的什么功能? 68 | //添加功能组件 69 | private JLabel addName = new JLabel("图书名称"); 70 | private JLabel addAuthor = new JLabel("图书作者"); 71 | private JLabel addNumber = new JLabel("图书编号"); 72 | private JLabel addLocation = new JLabel("图书位置"); 73 | private JLabel addBorrow = new JLabel("借阅信息"); 74 | private JComboBox addBorrowText = new JComboBox(); 75 | private String[] addBorrowList = {"可以借阅","已被借出"}; 76 | private JTextField addNameText = new JTextField(30); 77 | private JTextField addAuthorText = new JTextField(30); 78 | private JTextField addNumberText = new JTextField(30); 79 | private JTextField addLocationText = new JTextField(30); 80 | private JButton addCommitBtn = new JButton("添加"); //提交按钮 81 | private JButton addCleanBtn = new JButton("清空"); //清空按钮 82 | //查找功能组件 83 | private JLabel findTitle = new JLabel("图书信息"); //表格标题 84 | private JTable findTable = new JTable(); 85 | String[] findTableTitle = new String[]{"图书名称","图书作者","图书编号","借阅信息","存放位置"};//表格头字段 86 | /** 87 | * 如何实现动态获取图书列表,使二维数组大小与获取图书列表的数量一致??? 88 | */ 89 | // private String[][] findTableData = new String[20][5];//表格内容 90 | private String[][] findTableData;;//表格内容 91 | //删除功能组件 92 | private JLabel deletenum = new JLabel("请输入删除图书的编号:"); 93 | private JTextField deletenumText = new JTextField(30); //获得图书编号 94 | private JButton deletenumBtn = new JButton("删除"); 95 | //修改功能组件 96 | private JLabel updateNum = new JLabel("请输入修改图书的编号:"); 97 | private JTextField updateNumText0 = new JTextField(30); 98 | private JButton updateFindBtn = new JButton("搜索"); 99 | private JButton updateBtn = new JButton("修改"); 100 | private JTextField updateNameText = new JTextField(30); 101 | private JTextField updateAuthorText = new JTextField(30); 102 | private JTextField updateNumText = new JTextField(30); 103 | private JTextField updateLocationText = new JTextField(30); 104 | private JComboBox updateBorrowText = new JComboBox(); 105 | private int id; 106 | //实例 107 | private Book book = null; 108 | private List books = new ArrayList(); 109 | //删除的dialog 110 | private JDialog bookDialog = new JDialog(adminView, "图书信息", true); //展示删除图书信息的窗口 111 | private JButton sure = new JButton("确定删除"); 112 | private JButton nosure = new JButton("取消"); 113 | //错误信息 114 | private String msg = ""; 115 | 116 | public AdminView() { 117 | init(); 118 | adminView.setVisible(true); 119 | } 120 | 121 | public AdminView(Admin admin) { 122 | this.admin = admin; 123 | init(); 124 | adminView.setVisible(true); 125 | } 126 | 127 | private void init() { 128 | c.setLayout(null); 129 | adminView.setSize(800, 500); 130 | adminView.setLocationRelativeTo(null); 131 | adminView.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 132 | adminView.setResizable(false); 133 | 134 | createTopPanel(); //生成顶部面板 135 | createSidePanel(); //生成侧边面板 136 | selectFunctionListener(); //侧边按钮监听:功能选择加载不同面板 137 | createListener(); //监听器 138 | } 139 | 140 | /** 141 | * 设置功能提示字体 142 | * @param msg 143 | */ 144 | private void setFunctionMsg(String msg) { 145 | 146 | functionMsg.setBounds(200, 10, 700, 30); 147 | functionMsg.setText(msg); 148 | functionMsg.setFont(new Font("微软雅黑", Font.BOLD, 25)); 149 | functionMsg.setForeground(Color.orange); 150 | } 151 | 152 | /** 153 | * 顶部面板:800*100 154 | */ 155 | private void createTopPanel() { 156 | topPanel.setLayout(null); 157 | topPanel.setBounds(0,0,800,100); 158 | topPanel.setBackground(Color.GRAY); 159 | 160 | //显示登录用户的名字 161 | topAdminMsg.setBounds(10,5,200,20); 162 | topAdminMsg.setFont(new Font("微软雅黑",Font.PLAIN,15)); 163 | topAdminMsg.setText("欢迎您(管理员):"+admin.getName()); 164 | 165 | //检索方式按钮 166 | ComboBoxModel model = new DefaultComboBoxModel<>(items); 167 | topFindBy.setModel(model); 168 | topFindBy.setFont(new Font("黑体", Font.PLAIN, 9)); 169 | topFindBy.setBounds(120,40,80,25); 170 | 171 | //检索输入框 172 | topInput.setBounds(220, 40, 300, 25); 173 | 174 | //检索按钮 175 | sideSubmit.setBounds(550, 40, 80, 25); 176 | 177 | //当前时间 178 | topTime.setBounds(600, 70, 200, 25); 179 | topTime.setFont(new Font("宋体", Font.ITALIC, 11)); 180 | topTime.setText("登录时间:"+Tools.getTime()); 181 | 182 | topPanel.add(topAdminMsg); 183 | topPanel.add(topFindBy); 184 | topPanel.add(topInput); 185 | topPanel.add(sideSubmit); 186 | topPanel.add(topTime); 187 | c.add(topPanel); 188 | topPanel.setVisible(true); 189 | } 190 | 191 | /** 192 | * 侧边面板:增删查改按钮(100*400) 193 | */ 194 | private void createSidePanel() { 195 | sidePanel.setLayout(new FlowLayout()); 196 | sidePanel.setBounds(0, 100, 100, 400); 197 | sidePanel.setBackground(Color.lightGray); 198 | 199 | sidePanel.add(sideAddBtn); 200 | sidePanel.add(sideDeleteBtn); 201 | sidePanel.add(sideUpdateBtn); 202 | c.add(sidePanel); 203 | sidePanel.setVisible(true); 204 | } 205 | 206 | /** 207 | * 添加功能的面板:700*700 208 | */ 209 | private JPanel createAddPanel() { 210 | addPanel.setLayout(null); 211 | addPanel.setBounds(100, 100, 700, 700); 212 | 213 | //设置功能提示字体 214 | setFunctionMsg("您当前正在使用添加功能哦..."); 215 | 216 | //添加 217 | addName.setBounds(150, 60, 100, 30); 218 | addAuthor.setBounds(150, 90, 100, 30); 219 | addNumber.setBounds(150, 120, 100, 30); 220 | addLocation.setBounds(150, 150, 100, 30); 221 | addBorrow.setBounds(150, 180, 100, 30); 222 | addNameText.setBounds(220, 62, 300, 25); 223 | addAuthorText.setBounds(220, 92, 300, 25); 224 | addNumberText.setBounds(220, 122, 300, 25); 225 | addLocationText.setBounds(220, 152, 300, 25); 226 | ComboBoxModel model = new DefaultComboBoxModel(addBorrowList); 227 | addBorrowText.setModel(model); 228 | addBorrowText.setBounds(220, 182, 80, 25); 229 | 230 | //按钮 231 | addCommitBtn.setBounds(280, 240, 60, 25); 232 | addCleanBtn.setBounds(350, 240, 60, 25); 233 | 234 | addPanel.add(functionMsg); 235 | addPanel.add(addName); 236 | addPanel.add(addAuthor); 237 | addPanel.add(addNumber); 238 | addPanel.add(addLocation); 239 | addPanel.add(addBorrow); 240 | addPanel.add(addNameText); 241 | addPanel.add(addAuthorText); 242 | addPanel.add(addNumberText); 243 | addPanel.add(addLocationText); 244 | addPanel.add(addBorrowText); 245 | addPanel.add(addCommitBtn); 246 | addPanel.add(addCleanBtn); 247 | return addPanel; 248 | } 249 | 250 | /** 251 | * 查找功能面板:700*300 252 | */ 253 | private JScrollPane createFindPanel() { 254 | //设置功能提示字体 255 | setFunctionMsg("您当前正在使用查找功能哦..."); 256 | functionMsg.setBounds(300, 110, 700, 30); 257 | //标题 258 | findTitle.setBounds(400, 150, 100, 25); 259 | 260 | //表格的设置 261 | findTable.getTableHeader().setReorderingAllowed(false); //列不能移动 262 | findTable.getTableHeader().setResizingAllowed(false); //不可拉动表格 263 | findTable.setEnabled(false); //不可更改数据 264 | findTable.setRowHeight(20); 265 | findPanel.setBounds(150, 190, 600, 250); 266 | findPanel.setViewportView(findTable); //视口装入表格,暂时不知道什么用,但是放在后面,第二次点击检索时,排版会出现问题! 267 | 268 | c.add(functionMsg); 269 | c.add(findTitle); 270 | 271 | return findPanel; 272 | } 273 | 274 | /** 275 | * 删除功能面板:700*700 276 | */ 277 | private JPanel createDeletePanel() { 278 | deletePanel.setLayout(null); 279 | deletePanel.setBounds(100, 100, 700, 700); 280 | 281 | //设置功能提示字体 282 | setFunctionMsg("您当前正在使用删除功能哦..."); 283 | 284 | //首先搜索 285 | deletenum.setBounds(50, 70, 200, 30); 286 | deletenumText.setBounds(200, 70, 300, 25); 287 | deletenumBtn.setBounds(520, 70, 60, 25); 288 | 289 | deletePanel.add(functionMsg); 290 | deletePanel.add(deletenum); 291 | deletePanel.add(deletenumText); 292 | deletePanel.add(deletenumBtn); 293 | return deletePanel; 294 | } 295 | 296 | /** 297 | * 修改功能面板:700*700 298 | */ 299 | private JPanel createUpdataPanel() { 300 | 301 | updataPanel.setLayout(null); 302 | updataPanel.setBounds(100, 100, 700, 700); 303 | 304 | //功能提示字体 305 | setFunctionMsg("您当前正在使用修改功能哦..."); 306 | 307 | //搜索第一 308 | updateNum.setBounds(50, 70, 200, 30); 309 | updateNumText0.setBounds(200, 70, 300, 25); 310 | updateFindBtn.setBounds(520, 70, 60, 25); 311 | 312 | updataPanel.add(functionMsg); 313 | updataPanel.add(updateNum); 314 | updataPanel.add(updateNumText0); 315 | updataPanel.add(updateFindBtn); 316 | return updataPanel; 317 | } 318 | 319 | /** 320 | * 主要面板:功能实现区 321 | * 根据点击不同按钮,实现传入不同面板 322 | */ 323 | private void createMainPanel(JPanel jp) { 324 | c.remove(findTitle); //在检索面板中,主窗口加入了findTitle,在其他面板中应该删除。 325 | c.add(jp); 326 | jp.setVisible(true); 327 | } 328 | private void createMainPanel(JScrollPane jp) { 329 | c.add(jp); 330 | jp.setVisible(true); 331 | } 332 | 333 | //------------监听器------------------------------------------------------------------ 334 | /** 335 | * - 侧边面板 功能选择 监听器 336 | */ 337 | private void selectFunctionListener() { 338 | //添加功能 339 | sideAddBtn.addActionListener(new ActionListener() { 340 | @Override 341 | public void actionPerformed(ActionEvent e) { 342 | //首先,取消其他面板 343 | //如果不讲所有的面板设为不可见,那么在第一次点击按钮时,面板显示会出现问题 344 | //这是为什么呢??? 345 | //还好被我发现了QAQ 346 | setVisibleFalse(); 347 | 348 | //创建添加功能面板 349 | createMainPanel(createAddPanel()); 350 | } 351 | }); 352 | 353 | //删除功能 354 | sideDeleteBtn.addActionListener(new ActionListener() { 355 | @Override 356 | public void actionPerformed(ActionEvent e) { 357 | //首先,取消其他面板 358 | setVisibleFalse(); 359 | 360 | //创建删除功能面板 361 | createMainPanel(createDeletePanel()); 362 | } 363 | }); 364 | 365 | //修改功能 366 | sideUpdateBtn.addActionListener(new ActionListener() { 367 | @Override 368 | public void actionPerformed(ActionEvent e) { 369 | //首先,取消其他面板 370 | setVisibleFalse(); 371 | 372 | //创建删除功能面板 373 | createMainPanel(createUpdataPanel()); 374 | } 375 | }); 376 | } 377 | 378 | /** 379 | *- 监听器 380 | *- emmm这个方法,可能有点乱... 381 | *- 这段代码,只有我和上帝知道是什么意思 382 | *- 再过几个月,只有上帝能看懂 383 | */ 384 | private void createListener() { 385 | //检索按钮,复制的UserView的... 386 | //userService改成了adminService,因为俺让adminService继承了userService。 387 | //可以吗?QAQ 388 | sideSubmit.addActionListener(new ActionListener() { 389 | @Override 390 | public void actionPerformed(ActionEvent e) { 391 | //首先,将所有面板设置为不可见 392 | setVisibleFalse(); 393 | 394 | //然后,创建检索面板 395 | createMainPanel(createFindPanel()); 396 | 397 | // Tools.cleanTwoArray(findTableData); //首先清空表格内容 398 | String findMsg = ""; //用来获得检索书目使输入的内容 399 | findMsg = topInput.getText(); 400 | 401 | //查询书目列表 402 | if(topFindBy.getSelectedItem().equals(items[0])) { 403 | //通过书名检索,用0代表 404 | books = adminService.findBooks(findMsg, 0);//qaq我也不知道这种模式用的对不对 405 | System.out.println("通过书名检索"); 406 | } 407 | if(topFindBy.getSelectedItem().equals(items[1])) { 408 | //通过作者检索,用1代表 409 | books = adminService.findBooks(findMsg, 1); 410 | System.out.println("通过作者检索"); 411 | } 412 | if(topFindBy.getSelectedItem().equals(items[2])) { 413 | //通过编号检索,用2代表 414 | books = adminService.findBooks(findMsg, 2); 415 | System.out.println("通过编号检索"); 416 | } 417 | if (topFindBy.getSelectedItem().equals(items[3])) { 418 | //检索所有图书,3 419 | books = adminService.findBooks(findMsg, 3); 420 | System.out.println("检索所有图书"); 421 | } 422 | 423 | if(books != null) { 424 | //将检索的书目列表展示到表格中 425 | findTableData = new String[books.size()][5]; 426 | for(int i=0; i model = new DefaultComboBoxModel(addBorrowList); 625 | updateBorrowText.setModel(model); 626 | 627 | //设置搜索的图书信息 628 | id = book.getId(); 629 | updateNameText.setText(book.getBookname()); 630 | updateAuthorText.setText(book.getAuthor()); 631 | updateNumText.setText(book.getNum().toString()); 632 | updateLocationText.setText(book.getLocation()); 633 | if (book.getBorrow().equals("已被借出")) { 634 | updateBorrowText.setSelectedIndex(1); 635 | } 636 | 637 | name.setBounds(200, 130, 120, 30); 638 | author.setBounds(200, 160, 120, 30); 639 | num.setBounds(200, 190, 120, 30); 640 | location.setBounds(200, 220, 120, 30); 641 | borrow.setBounds(200, 250, 120, 30); 642 | 643 | updateNameText.setBounds(300, 132, 200, 25); 644 | updateAuthorText.setBounds(300, 162, 200, 25); 645 | updateNumText.setBounds(300, 192, 200, 25); 646 | updateLocationText.setBounds(300, 222, 200, 25); 647 | updateBorrowText.setBounds(300, 252, 80, 25); 648 | updateBtn.setBounds(200, 290, 300, 25); 649 | 650 | updataPanel.add(name); 651 | updataPanel.add(author); 652 | updataPanel.add(num); 653 | updataPanel.add(location); 654 | updataPanel.add(borrow); 655 | updataPanel.add(updateNameText); 656 | updataPanel.add(updateAuthorText); 657 | updataPanel.add(updateNumText); 658 | updataPanel.add(updateLocationText); 659 | updataPanel.add(updateBorrowText); 660 | updataPanel.add(updateBtn); 661 | //实现刷新! 662 | updataPanel.setVisible(false); 663 | updataPanel.setVisible(true); 664 | } 665 | 666 | /** 667 | * 在修改图书功能中:清除搜索到的图书内容 668 | */ 669 | private void cleanBookMsgOnUppdataPanel() { 670 | updateNameText.setText(""); 671 | updateAuthorText.setText(""); 672 | updateNumText.setText(""); 673 | updateLocationText.setText(""); 674 | updateBorrowText.setSelectedIndex(0); 675 | } 676 | 677 | } 678 | -------------------------------------------------------------------------------- /src/ui/Index.java: -------------------------------------------------------------------------------- 1 | package ui; 2 | 3 | /** 4 | * - 图书管理系统的登录入口 5 | * 存在缺陷: 6 | * 1. 无法添加重复图书,或者可以修改为每本图书的数量:数据库中添加字段count来表示图书数量 7 | * 2. 添加图书时,图书编号是中文怎么办? //解决 8 | * 3. 添加图书时,图书信心乱写怎么办? 比如过多空格,比如... 9 | * 4. 【急】如何动态设置二维数组的行、列,用来设置表格数据??? 10 | * 5. 表格长度无法显示完整信息 11 | * 代码缺陷: 12 | * 1. 变量命名...呃... 13 | * 2. 一个类中的代码有点多和繁琐 14 | * @author Administrator 15 | * 16 | */ 17 | public class Index { 18 | public static void main(String[] args) { 19 | new Login(); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/ui/Login.java: -------------------------------------------------------------------------------- 1 | package ui; 2 | 3 | import java.awt.BorderLayout; 4 | import java.awt.Color; 5 | import java.awt.Container; 6 | import java.awt.Font; 7 | import java.awt.Image; 8 | import java.awt.event.ActionEvent; 9 | import java.awt.event.ActionListener; 10 | 11 | import javax.swing.ButtonGroup; 12 | import javax.swing.ImageIcon; 13 | import javax.swing.JButton; 14 | import javax.swing.JDialog; 15 | import javax.swing.JFrame; 16 | import javax.swing.JLabel; 17 | import javax.swing.JPanel; 18 | import javax.swing.JPasswordField; 19 | import javax.swing.JRadioButton; 20 | import javax.swing.JTextField; 21 | 22 | import model.Admin; 23 | import model.User; 24 | import service.UserService; 25 | 26 | /** 27 | * 登录 28 | * 数据库中设置了密钥、账号的索引类型为UNIQUE 29 | * @author Administrator 30 | * 31 | */ 32 | public class Login { 33 | private JFrame jf = new JFrame(); 34 | private Container c = jf.getContentPane(); 35 | private JPanel topPanel = new JPanel(); //top 36 | private JLabel userLabel = new JLabel("用户名:"); //用户名 37 | private JLabel passwordLabel = new JLabel("密 码:");//密码 38 | private JLabel titleLabel = new JLabel("欢迎进入图书管理系统"); 39 | 40 | private ImageIcon image = new ImageIcon("src//images//logo.png"); //以class文件问基准,寻找地址的 41 | private JLabel logo = new JLabel();//logo 42 | 43 | private JTextField userText = new JTextField();//用户名输入文本框 44 | private JPasswordField passwordText = new JPasswordField();//密码输入文本框 45 | private JButton loginBtn = new JButton("登录");//登录按钮 46 | private JButton registerBtn = new JButton("注册");//注册按钮 47 | 48 | private ButtonGroup btnGroup = new ButtonGroup(); 49 | private JRadioButton jr1 = new JRadioButton("普通用户",true); 50 | private JRadioButton jr2 = new JRadioButton("管理员",false); 51 | 52 | private Font userAndPasswdFont = new Font("楷体",Font.BOLD,15); 53 | 54 | private String userName = ""; 55 | private String userPassword = ""; 56 | 57 | 58 | public Login() { 59 | init(); 60 | 61 | jf.setVisible(true); 62 | } 63 | 64 | private void init(){ 65 | c.setLayout(null); 66 | jf.setSize(400,300); 67 | jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 68 | jf.setLocationRelativeTo(null); 69 | jf.setResizable(false); 70 | 71 | /* 72 | * 登录窗口顶部 73 | */ 74 | topPanel.setLayout(null); 75 | topPanel.setBackground(Color.GRAY); 76 | topPanel.setSize(400,100); 77 | 78 | //设置标题 79 | titleLabel.setFont(new Font("微软雅黑",Font.BOLD,20));//字体 80 | titleLabel.setForeground(Color.BLACK);//字体颜色 81 | titleLabel.setBounds(120,0,400,100); 82 | topPanel.add(titleLabel); 83 | 84 | //设置图片大小、位置,并添加到面板中 85 | image.setImage(image.getImage().getScaledInstance(55, 55, Image.SCALE_DEFAULT)); 86 | logo.setIcon(image); 87 | logo.setBounds(50,20,55,55); 88 | topPanel.add(logo); 89 | 90 | 91 | 92 | /* 93 | * 登录窗口文本框 94 | */ 95 | userLabel.setBounds(70,20,200,200); 96 | userLabel.setFont(userAndPasswdFont); 97 | passwordLabel.setBounds(70,60,200,200); 98 | passwordLabel.setFont(userAndPasswdFont); 99 | userText.setColumns(9); 100 | userText.setBounds(140,110,150,20); 101 | passwordText.setColumns(11); 102 | passwordText.setBounds(140, 150, 150, 20); 103 | 104 | //登录按钮 105 | loginBtn.setBounds(190,220,100,30); 106 | registerBtn.setBounds(70,220,100,30); 107 | 108 | 109 | 110 | /* 111 | * 普通用户和管理员 112 | */ 113 | jr1.setBounds(100,180,100,15); 114 | jr2.setBounds(200,180,100,15); 115 | btnGroup.add(jr1); 116 | btnGroup.add(jr2); 117 | 118 | 119 | c.add(topPanel); 120 | c.add(userLabel); 121 | c.add(passwordLabel); 122 | c.add(userText); 123 | c.add(passwordText); 124 | c.add(registerBtn); 125 | c.add(loginBtn); 126 | c.add(jr1); 127 | c.add(jr2); 128 | 129 | createBtnListener(); 130 | } 131 | 132 | private void createBtnListener() { 133 | /** 134 | * 注册按钮的监听事件 135 | */ 136 | registerBtn.addActionListener(new ActionListener() { 137 | @Override 138 | public void actionPerformed(ActionEvent e) { 139 | new RegisterDialog(jf, "欢迎注册"); 140 | } 141 | }); 142 | 143 | /** 144 | * 登录按钮的监听事件 145 | */ 146 | loginBtn.addActionListener(new ActionListener() { 147 | @Override 148 | public void actionPerformed(ActionEvent e) { 149 | //获取用户输入的用户名的密码 150 | userName = userText.getText(); 151 | userPassword = String.valueOf(passwordText.getPassword());//String.valueOf()->字符数组转换为字符串 152 | 153 | UserService userService = new UserService(); 154 | 155 | if(jr1.isSelected()) { 156 | //普通用户登录 157 | User user = userService.login(userName,userPassword); 158 | if (user!=null) { 159 | //切换普通用户界面 160 | System.out.println("登录成功"); 161 | jf.dispose();//销毁登录窗口 162 | new UserView(user); 163 | }else { 164 | //提示账号或密码错误 165 | System.out.println("账号或密码错误"); 166 | createDialog(jf); 167 | } 168 | }else { 169 | //管理员登录 170 | Admin admin = userService.adminLogin(userName,userPassword); 171 | if(admin!=null) { 172 | //切换至管理员页面 173 | System.out.println("管理员登录成功!"); 174 | jf.dispose();//销毁登录窗口 175 | new AdminView(admin); 176 | }else { 177 | createDialog(jf); 178 | } 179 | } 180 | } 181 | }); 182 | 183 | } 184 | 185 | 186 | private void createDialog(JFrame frame) { 187 | JDialog msgDialog = new JDialog(frame, "登录失败!", true); 188 | JLabel msgLabel = new JLabel(); 189 | Container con = msgDialog.getContentPane(); 190 | 191 | msgDialog.setSize(250,150); 192 | msgDialog.setLocationRelativeTo(null); 193 | 194 | msgLabel.setText("账号或密码错误!"); 195 | msgLabel.setFont(new Font("黑体", Font.PLAIN, 15)); 196 | 197 | con.add(msgLabel,BorderLayout.WEST); 198 | 199 | msgDialog.setVisible(true); //必须放在最后!! 200 | } 201 | } 202 | -------------------------------------------------------------------------------- /src/ui/RegisterDialog.java: -------------------------------------------------------------------------------- 1 | package ui; 2 | 3 | import java.awt.Color; 4 | import java.awt.Container; 5 | import java.awt.event.ActionEvent; 6 | import java.awt.event.ActionListener; 7 | 8 | import javax.swing.ComboBoxModel; 9 | import javax.swing.DefaultBoundedRangeModel; 10 | import javax.swing.DefaultComboBoxModel; 11 | import javax.swing.JButton; 12 | import javax.swing.JComboBox; 13 | import javax.swing.JDialog; 14 | import javax.swing.JFrame; 15 | import javax.swing.JLabel; 16 | import javax.swing.JPasswordField; 17 | import javax.swing.JTextField; 18 | 19 | import model.Register; 20 | import service.RegistService; 21 | import utils.Tools; 22 | 23 | /** 24 | * 注册页面 25 | * 管理员注册时,加入了密钥 26 | * 注册条件:姓名长度>2 账号密码长度>5 ,账号密码在后端逻辑上暂且无法过滤中文 27 | * 注册类别:分为普通用户和管理员 28 | * @author Administrator 29 | * 30 | */ 31 | public class RegisterDialog { 32 | private RegistService registService = new RegistService(); 33 | private Register register = null; 34 | private JDialog registerView; 35 | private Container c; 36 | private JFrame jf; 37 | 38 | //字段文本 39 | private JLabel registerName = new JLabel("姓名:"); 40 | private JLabel registerCode = new JLabel("账号:"); 41 | private JLabel registerPassword = new JLabel("密码:"); 42 | private JLabel registerPassword2 = new JLabel("重复密码:"); 43 | private JLabel registerAdminCode = new JLabel("管理员密钥:"); //注册为管理员时,需要输入密钥! 44 | private JLabel adminCodeNotice = new JLabel("注意:管理员密钥仅在注册管理员时有效..."); 45 | private JLabel registMold = new JLabel("注册类型:"); 46 | 47 | //字段输入框 48 | private JTextField nameField = new JTextField(); 49 | private JTextField codeField = new JTextField(); 50 | private JPasswordField passwordField = new JPasswordField(); 51 | private JPasswordField passwordField2 = new JPasswordField(); 52 | private JTextField adminCodeField = new JTextField(); 53 | private JComboBox registMoldField = new JComboBox(); 54 | private String[] registMoldList = {"普通用户","管理员"}; 55 | private ComboBoxModel model = new DefaultComboBoxModel(registMoldList); 56 | 57 | //按钮 58 | private JButton registBtn = new JButton("注册"); 59 | private JButton resetBtn = new JButton("重置"); 60 | 61 | public RegisterDialog(JFrame jf, String title) { 62 | this.jf = jf; 63 | init(title); 64 | registerView.setVisible(true); 65 | } 66 | 67 | //初始化窗口 68 | private void init(String title) { 69 | //窗口 70 | registerView = new JDialog(jf, title, true); 71 | c = registerView.getContentPane(); 72 | c.setLayout(null); 73 | registerView.setSize(500, 300); 74 | registerView.setLocationRelativeTo(null); 75 | 76 | //组件 77 | registerName.setBounds(120, 20, 100, 30); 78 | registerCode.setBounds(120, 50, 100, 30); 79 | registerPassword.setBounds(120,80, 100, 30); 80 | registerPassword2.setBounds(120, 110, 100, 30); 81 | registerAdminCode.setBounds(120, 140, 100, 30);//密钥 82 | adminCodeNotice.setBounds(120, 160, 400, 30);//"注意" 83 | adminCodeNotice.setForeground(Color.RED);;//"注意"字体 84 | registMold.setBounds(120, 190, 100, 30); //注册类型 85 | 86 | nameField.setBounds(200, 22, 200, 25); 87 | codeField.setBounds(200, 52, 200, 25); 88 | passwordField.setBounds(200, 82, 200, 25); 89 | passwordField2.setBounds(200, 112, 200, 25); 90 | adminCodeField.setBounds(200, 142, 200, 25); //密钥字段 91 | registMoldField.setBounds(200, 192, 80, 25); 92 | 93 | registBtn.setBounds(180, 230, 60, 25); 94 | resetBtn.setBounds(260, 230, 60, 25); 95 | 96 | registMoldField.setModel(model); 97 | 98 | c.add(registerName); 99 | c.add(registerCode); 100 | c.add(registerPassword); 101 | c.add(registerPassword2); 102 | c.add(registerAdminCode); 103 | c.add(registMold); 104 | c.add(adminCodeNotice); 105 | c.add(nameField); 106 | c.add(codeField); 107 | c.add(passwordField); 108 | c.add(passwordField2); 109 | c.add(adminCodeField); 110 | c.add(registMoldField); 111 | c.add(registBtn); 112 | c.add(resetBtn); 113 | 114 | //监听器 115 | createListener(); 116 | } 117 | 118 | /** 119 | * - 监听器 120 | * - 还未解决问题: 121 | * 2.注册信息合法问题,中文如何排除 122 | */ 123 | private void createListener() { 124 | //注册按钮 125 | registBtn.addActionListener(new ActionListener() { 126 | @Override 127 | public void actionPerformed(ActionEvent e) { 128 | System.out.println("开始注册..."); 129 | String msg = ""; 130 | //判断两次密码输入是否一致 131 | if (passwordField.getText().equals(passwordField2.getText())) { 132 | //一致 133 | register = new Register(nameField.getText(),codeField.getText(),passwordField.getText(), 134 | adminCodeField.getText(),registMoldField.getSelectedItem().toString()); 135 | msg = registService.regist(register); 136 | }else { 137 | //不一致 138 | msg = "两次密码输入不一样啊大哥!";//0.0 139 | passwordField.setText(""); //清空密码 140 | passwordField2.setText(""); 141 | } 142 | Tools.createMsgDialog(jf, msg); 143 | } 144 | }); 145 | 146 | //重置按钮 147 | resetBtn.addActionListener(new ActionListener() { 148 | @Override 149 | public void actionPerformed(ActionEvent e) { 150 | nameField.setText(""); 151 | codeField.setText(""); 152 | passwordField.setText(""); 153 | passwordField2.setText(""); 154 | adminCodeField.setText(""); 155 | registMoldField.setSelectedIndex(0); //设置为普通用户注册 156 | } 157 | }); 158 | } 159 | } 160 | -------------------------------------------------------------------------------- /src/ui/UserView.java: -------------------------------------------------------------------------------- 1 | package ui; 2 | 3 | import java.awt.Color; 4 | import java.awt.Container; 5 | import java.awt.Dimension; 6 | import java.awt.Font; 7 | import java.awt.event.ActionEvent; 8 | import java.awt.event.ActionListener; 9 | import java.text.DateFormat; 10 | import java.text.SimpleDateFormat; 11 | import java.util.Date; 12 | import java.util.List; 13 | 14 | import javax.swing.ComboBoxModel; 15 | import javax.swing.DefaultComboBoxModel; 16 | import javax.swing.JButton; 17 | import javax.swing.JComboBox; 18 | import javax.swing.JFrame; 19 | import javax.swing.JLabel; 20 | import javax.swing.JPanel; 21 | import javax.swing.JScrollPane; 22 | import javax.swing.JTable; 23 | import javax.swing.JTextField; 24 | import javax.swing.table.DefaultTableModel; 25 | import javax.swing.table.TableModel; 26 | 27 | import model.Book; 28 | import model.User; 29 | import service.UserService; 30 | import utils.Tools; 31 | 32 | /** 33 | * 普通用户界面 34 | * 宽:800 35 | * 高:500 36 | * @author Administrator 37 | * 38 | */ 39 | public class UserView{ 40 | User user = null; 41 | UserService userService = new UserService(); 42 | private JFrame userView = new JFrame("图书管理系统"); 43 | private Container c = userView.getContentPane(); 44 | private JPanel topPanel = new JPanel(); //顶部面板 45 | private JLabel userMsg = new JLabel(); //用来显示用户姓名 46 | private JLabel time = new JLabel(); //用来显示登录时间 47 | private JLabel title = new JLabel("图书信息"); //表格标题 48 | private JScrollPane mainPanel = new JScrollPane(); //图书信息展览面板 49 | private JTable table = new JTable(); 50 | private JComboBox findBy = new JComboBox<>(); //检索方式下拉列表 51 | private String items[] = {"通过书名检索", "通过作者检索", "通过编号检索","检索所有图书"}; 52 | private JTextField input = new JTextField(); //检索输入框 53 | private JButton submit = new JButton("检索"); 54 | 55 | private List books = null; //检索到的图书列表 56 | String[] tableTitle = new String[]{"图书名称","图书作者","图书编号","借阅信息","存放位置"};//表格头字段 57 | private String[][] tableData = new String[1][5];//表格内容 58 | 59 | 60 | public UserView(User loginUser) { 61 | this.user = loginUser; 62 | init(); 63 | userView.setVisible(true); 64 | } 65 | 66 | /* 67 | * 初始化界面 68 | */ 69 | private void init() { 70 | c.setLayout(null); 71 | userView.setSize(800,500);//先size在location,否则会乱 72 | userView.setLocationRelativeTo(null); 73 | userView.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 74 | userView.setResizable(false); 75 | 76 | topPanel();//加载顶部面板 77 | mainPanel();//加载图书目录列表的面板 78 | 79 | createListener();//创建监听器,监听检索事件... 80 | } 81 | 82 | /** 83 | * -顶部面板 84 | */ 85 | private void topPanel() { 86 | topPanel.setLayout(null); 87 | topPanel.setBounds(0,0,800,100); 88 | topPanel.setBackground(Color.GRAY); 89 | 90 | 91 | //显示登录用户的名字 92 | userMsg.setBounds(10,5,200,20); 93 | userMsg.setFont(new Font("微软雅黑",Font.PLAIN,15)); 94 | userMsg.setText("欢迎您:"+user.getName()); 95 | 96 | //检索方式按钮 97 | 98 | ComboBoxModel model = new DefaultComboBoxModel<>(items); 99 | findBy.setModel(model); 100 | findBy.setFont(new Font("黑体", Font.PLAIN, 9)); 101 | findBy.setBounds(120,40,80,25); 102 | 103 | //检索输入框 104 | input.setBounds(220, 40, 300, 25); 105 | 106 | //检索按钮 107 | submit.setBounds(550, 40, 80, 25); 108 | 109 | //当前时间 110 | time.setBounds(600, 70, 200, 25); 111 | time.setFont(new Font("宋体", Font.ITALIC, 11)); 112 | time.setText("登录时间:"+Tools.getTime()); 113 | 114 | topPanel.add(userMsg); 115 | topPanel.add(findBy); 116 | topPanel.add(input); 117 | topPanel.add(submit); 118 | topPanel.add(time); 119 | c.add(topPanel); 120 | topPanel.setVisible(true); 121 | } 122 | 123 | /** 124 | * -图书信息展览面板 125 | */ 126 | private void mainPanel() { 127 | /** 128 | * - 存在问题:表格内容如何自动换行? 129 | */ 130 | // mainPanel.setLayout(null); 131 | 132 | //表格的设置 133 | table.getTableHeader().setReorderingAllowed(false); //列不能移动 134 | table.getTableHeader().setResizingAllowed(false); //不可拉动表格 135 | table.setEnabled(false); //不可更改数据 136 | table.setRowHeight(20); 137 | mainPanel.setBounds(45, 140, 700, 300); 138 | mainPanel.setViewportView(table); //视口装入表格 139 | 140 | //标题 141 | title.setBounds(350, 110, 100, 25); 142 | 143 | c.add(mainPanel); 144 | c.add(title); 145 | mainPanel.setVisible(true); 146 | } 147 | 148 | 149 | 150 | /** 151 | * - 给按钮添加监听事件 152 | */ 153 | private void createListener() { 154 | //检索按钮 155 | //这段代码,只有我和上帝知道是什么意思 156 | //再过几个月,只有上帝能看懂 157 | submit.addActionListener(new ActionListener() { 158 | @Override 159 | public void actionPerformed(ActionEvent e) { 160 | // Tools.cleanTwoArray(tableData); //首先清空表格内容 161 | String findMsg = ""; //用来获得检索书目使输入的内容 162 | findMsg = input.getText(); 163 | 164 | //查询书目列表 165 | if(findBy.getSelectedItem().equals(items[0])) { 166 | //通过书名检索,用0代表 167 | books = userService.findBooks(findMsg, 0);//qaq我也不知道这种模式用的对不对 168 | System.out.println("通过书名检索"); 169 | } 170 | if(findBy.getSelectedItem().equals(items[1])) { 171 | //通过作者检索,用1代表 172 | books = userService.findBooks(findMsg, 1); 173 | System.out.println("通过作者检索"); 174 | } 175 | if(findBy.getSelectedItem().equals(items[2])) { 176 | //通过编号检索,用2代表 177 | books = userService.findBooks(findMsg, 2); 178 | System.out.println("通过编号检索"); 179 | } 180 | if (findBy.getSelectedItem().equals(items[3])) { 181 | //检索所有图书,3 182 | books = userService.findBooks(findMsg, 3); 183 | System.out.println("检索所有图书"); 184 | } 185 | 186 | if(books != null) { 187 | //将检索的书目列表展示到表格中 188 | tableData = new String[books.size()][5]; 189 | for(int i=0; i=0;){ 66 | if (!Character.isDigit(str.charAt(i))){ 67 | return false; 68 | } 69 | } 70 | return true; 71 | } 72 | } 73 | --------------------------------------------------------------------------------