Please enable Javascript to view the contents

简易的图书管理系统(Java+MySQL8.0)

 ·  ☕ 5 分钟  ·  🤖 Vigilr

[toc]

简易的图书管理系统🔗

项目简介🔗

推荐视频:JAVA+MySql 图书管理系统,技术:Swing+jdbc+mysql

用到的知识:Java,MySQL

源码已上传至码云

项目整体架构🔗

源码目录如下:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
PS E:\IdeaProjects\BookManager\src> tree /f
 软件 的文件夹 PATH 列表
卷序列号为 54DD-69A2
E:.
├─dao
      BookDao.java
      BookTypeDao.java
      UserDao.java

├─entity
      Book.java
      Booktype.java
      User.java

├─images
      book.png
      susu.jpg

├─META-INF
      MANIFEST.MF

├─util
      DbUtil.java
      StringUtil.java

└─view
        About.java
        About.jfd
        BookAddFrm.java
        BookAddFrm.jfd
        BookTypeAddFrm.java
        BookTypeAddFrm.jfd
        BookTypeManagerFrm.java
        BookTypeManagerFrm.jfd
        Login.form
        Login.java
        MainFrm.java
        MainFrm.jfd

由此可见,本项目也是采用的 MVC 架构,dao包用于操作数据库,entity包包含本项目的实体类,images包是本项目用到的图片,META-INF包是生成 Jar 包时产生的文件夹,util包包含本项目用到的工具类,view包是本项目的视图层,用于设计窗口显示。

项目中各类的依赖关系如下图

BookManager

具体实现🔗

util包🔗

本项目的工具包,包含两个工具类

DbUtil.java主要是设计了数据库的驱动,地址,用户名及密码,和控制数据库的连接与断开

StringUtil.java只是用于判断字符串是否为空,只有空格也算为空


entity包🔗

此包包含了数据库表的实体对象,可以自己写,也可以通过 idea 的EasyCode插件进行代码生成,但是后边要用到的必要的构造方法要自己写。

大多数情况下都是简单类包含 Getter 和 Setter 方法,如下是EasyCode插件生成的代码

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
public class Booktype implements Serializable {
    private static final long serialVersionUID = -43707012852599825L;

    private Integer id;

    private String booktypename;

    @Override
    public String toString() {
        return this.booktypename;
    }

    private String booktypedesc;

    public Booktype(String booktypename, String booktypedesc) {
        this.booktypename = booktypename;
        this.booktypedesc = booktypedesc;
    }

    public Booktype() {
        super();
    }

    public Booktype(Integer id, String booktypename, String booktypedesc) {
        this(booktypename, booktypedesc);
        this.id = id;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getBooktypename() {
        return booktypename;
    }

    public void setBooktypename(String booktypename) {
        this.booktypename = booktypename;
    }

    public String getBooktypedesc() {
        return booktypedesc;
    }

    public void setBooktypedesc(String booktypedesc) {
        this.booktypedesc = booktypedesc;
    }

}

dao包🔗

进行数据库的访问

类中的方法定义形式基本为public 返回类型 方法名(Connection conn, 实体类 对象名) throws SQLException

例如用户登录的方法如下:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
public User login(Connection connection, User user) throws SQLException {
    User resultUser = null;
    String sql = "select * from table_user where userName=? and password=?";
    PreparedStatement psmt = connection.prepareStatement(sql);
    psmt.setString(1, user.getUserName());
    psmt.setString(2, user.getPassword());
    ResultSet resultSet = psmt.executeQuery();
    if (resultSet.next()) {
        resultUser = new User();
        resultUser.setId(resultSet.getInt("id"));
        resultUser.setUserName(resultSet.getString("userName"));
        resultUser.setPassword(resultSet.getString("password"));
    }
    return resultUser;
}

步骤总结:

  1. 其中 SQL 语句中的要查找的值要用?代替,方便后面设置

    String sql = "select * from table_user where userName=? and password=?";

  2. 然后创建 SQL 语句的游标对象

    psmtPreparedStatement psmt = connection.prepareStatement(sql);

  3. 设置 SQL 语句中的变量值,第几个?为几,索引就是几,没有 0

    psmt.setString(1, user.getUserName());

  4. 执行查询语句,并返回ResultSet对象用于保存查询结果

    ResultSet resultSet = psmt.executeQuery();

  5. 然后根据需求进行其他操作


BookDao类的设计如下,dao包下的其他类类似

添加图书

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
public static int add(Connection connection, Book book) throws SQLException {
    String sql = "insert into book values(null,?,?,?,?,?,?)";
    PreparedStatement psmt = connection.prepareStatement(sql);
    psmt.setString(1, book.getBookname());
    psmt.setString(2, book.getAuthor());
    psmt.setString(3, book.getSex());
    psmt.setInt(4, book.getBooktypeid());
    psmt.setString(5, book.getBookdesc());
    psmt.setFloat(6, book.getPrice());
    return psmt.executeUpdate();
}

添加成功返回 1,添加失败返回 0


删除图书

1
2
3
4
5
6
public static int delete(Connection conn, String id) throws SQLException {
    String sql = "delete from book where id=?";
    PreparedStatement psmt = conn.prepareStatement(sql);
    psmt.setString(1, id);
    return psmt.executeUpdate();
}

删除成功返回 1,删除失败返回 0


修改图书

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
public static int update(Connection conn, Book book) throws SQLException {
    String sql = "update book set bookName=?,author=?,sex=?,price=?,bookTypeid=?,bookDesc=? where id=?";
    PreparedStatement psmt = conn.prepareStatement(sql);
    psmt.setString(1, book.getBookname());
    psmt.setString(2, book.getAuthor());
    psmt.setString(3, book.getSex());
    psmt.setFloat(4, book.getPrice());
    psmt.setInt(5, book.getBooktypeid());
    psmt.setString(6, book.getBookdesc());
    psmt.setInt(7, book.getId());
    return psmt.executeUpdate();
}

修改成功返回 1,修改失败返回 0


查询图书

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
public static ResultSet list(Connection conn, Book book) throws SQLException {
    StringBuffer sb = new StringBuffer("select * from book b,table_booktype bt where b.bookTypeId=bt.id");
    if (!StringUtil.isEmpty(book.getBookname())) {
        sb.append(" and b.bookName like '%" + book.getBookname() + "%'");
    }
    if (!StringUtil.isEmpty(book.getAuthor())) {
        sb.append(" and b.author like '%" + book.getAuthor() + "%'");
    }
    if (book.getBooktypeid() != null && book.getBooktypeid() != -1) {
        sb.append(" and b.bookTypeId=" + book.getBooktypeid());
    }
    PreparedStatement psmt = conn.prepareStatement(sb.toString());
    return psmt.executeQuery();
}

返回查询结果


view包🔗

视图层是利用的 swing 组件设计,但这个不是本项目的重点,因为 Java 的 swing 已经很少有人用了,可以直接使用 idea 中的JFormDesigner插件进行拖拉控件,使用时一定要注意将框架的布局类型设为null Layout,大小设为Design size

我们只需设计按钮和表格的触发事件

例如图书添加事件

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
private void button1ActionPerformed(ActionEvent e) {
    String bookName = this.booknameTXT.getText();
    String author = this.bookAuthorTXT.getText();
    String price = this.bookPriceTXT.getText();
    String bookDesc = this.bookDescTXT.getText();
    if (StringUtil.isEmpty(bookName)) {
        JOptionPane.showMessageDialog(null, "图书名称不能为空");
        return;
    }
    if (StringUtil.isEmpty(author)) {
        JOptionPane.showMessageDialog(null, "图书作者不能为空");
        return;
    }
    if (StringUtil.isEmpty(price)) {
        JOptionPane.showMessageDialog(null, "图书价格不能为空");
        return;
    }
    try {
        float pricef = Float.parseFloat(price);
    } catch (NumberFormatException numberFormatException) {
        JOptionPane.showMessageDialog(null, "图书价格应为数字");
        return;
    }
    String sex = "";
    if (this.man.isSelected()) {
        sex = "男";
    } else {
        sex = "女";
    }
    Booktype booktype = (Booktype) this.booktypecb.getSelectedItem();
    assert booktype != null;
    int booktypeId = booktype.getId();
    Book book = new Book(bookName, author, sex, Float.parseFloat(price), booktypeId, bookDesc);
    Connection conn = null;
    try {
        conn = dbUtil.getConnection();
        int addNum = BookDao.add(conn, book);
        if (addNum == 1) {
            JOptionPane.showMessageDialog(null, "添加成功");
            resetValue();
        } else {
            JOptionPane.showMessageDialog(null, "添加失败");
        }
    } catch (Exception ex) {
        ex.printStackTrace();
        JOptionPane.showMessageDialog(null, "添加失败");
    } finally {
        try {
            conn.close();
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }
}

设计思想:

  1. 判断必需的输入框是否为空,以及类型是否一致
  2. 获取各输入框的数据
  3. 根据数据实例化对象
  4. 调用dao包中对应的方法进行操作

其他的删除,修改,查询的操作思想基本和添加的方法一样

项目总结🔗

项目中使用到了 Java 的 swing 技术,但是现在已经很少有人会用 Java 去写软件界面了,Java 不能像 C#那样拖拉控件设计界面,但是可以在 idea 中安装JFormDesigner插件实现 C#那样的效果了。

MVC 全名是 Model View Controller,是模型(model)-视图(view)-控制器(controller)的缩写,一种软件设计典范,用一种业务逻辑、数据、界面显示分离的方法组织代码,将业务逻辑聚集到一个部件里面,在改进和个性化定制界面及用户交互的同时,不需要重新编写业务逻辑。MVC 被独特的发展起来用于映射传统的输入、处理和输出功能在一个逻辑的图形化用户界面的结构中。

MVC 即围绕着数据库进行操作,本项目只是简单地实现了对数据库的增删改查,比较适合新手练习。

分享
您的鼓励是我最大的动力
alipay QR Code
wechat QR Code

Vigilr
作者
Vigilr
大四学生