Java实战之图书管理系统(JavaFX版)(6)——图书类别添加功能实现

本节概要

在上一节中实现了主界面代码,但是没有涉及到本项目的核心,也没有使用到数据库,而在本节将使用到数据库,实现图书类别记录的添加。

 

创建数据库及表

 

创建实体类

根据数据库表的列创建实体类。

即在beans目录下创建BookTypeBean.java,内容如下:

package BookManageSystem.beans;
​
public class BookTypeBean {
    private int bookTypeId;
    private String bookTypeName;
    private String bookTypeDescription;
​
    public BookTypeBean() {}
​
    public BookTypeBean(int bookTypeId, String bookTypeName, String bookTypeDescription) {
        this.bookTypeId = bookTypeId;
        this.bookTypeName = bookTypeName;
        this.bookTypeDescription = bookTypeDescription;
    }
​
    public int getBookTypeId() {
        return bookTypeId;
    }
​
    public void setBookTypeId(int bookTypeId) {
        this.bookTypeId = bookTypeId;
    }
​
    public String getBookTypeName() {
        return bookTypeName;
    }
​
    public void setBookTypeName(String bookTypeName) {
        this.bookTypeName = bookTypeName;
    }
​
    public String getBookTypeDescription() {
        return bookTypeDescription;
    }
​
    public void setBookTypeDescription(String bookTypeDescription) {
        this.bookTypeDescription = bookTypeDescription;
    }
}

 

Dao层方法

接着就是写添加记录到数据库的方法。

在dao包下创建JDBCUtils.java类,该类是dao层的公共方法类,用来连接数据库和释放数据库连接。

package BookManageSystem.dao;
​
import java.sql.*;
​
/**
 * 连接JDBC类
 * @author lck100
 */
public class JDBCUtils {
    //加载驱动,并建立数据库连接
​
    /**
     * 加载驱动建立数据库链接
     *
     * @return 返回数据库Connection连接对象
     * @throws SQLException           抛出SQLException
     * @throws ClassNotFoundException 抛出ClassNotFoundException
     */
    static Connection getConnection() throws SQLException, ClassNotFoundException {
        // 加载驱动
        Class.forName("com.mysql.jdbc.Driver");
        // 连接数据库
        String url = "jdbc:mysql://localhost:3306/db_bookSystem";
        // 数据库登录用户名
        String username = "root";
        // 数据库登录密码
        String password = "admin";
        // 获取数据库连接对象并返回Connection对象
        return DriverManager.getConnection(url, username, password);
    }
​
    /**
     * 关闭数据库连接,释放资源
     *
     * @param stmt Statement对象
     * @param conn Connection对象
     */
    static void release(Statement stmt, Connection conn) {
        if (stmt != null) {
            try {
                stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            stmt = null;
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            conn = null;
        }
    }
​
    /**
     * 关闭数据库连接,释放资源
     *
     * @param rs   ResultSet对象
     * @param stmt Statement对象
     * @param conn Connection对象
     */
    public static void release(ResultSet rs, Statement stmt, Connection conn) {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            rs = null;
        }
        release(stmt, conn);
    }
​
}

接着创建BookTypeDao.java,添加如下内容:

package BookManageSystem.dao;
​
import java.sql.Connection;
import java.sql.Statement;
​
public class BookTypeDao {
​
    /**
     * 操作结果:根据SQL语句执行数据库的增删改操作
     *
     * @param sql SQL语句
     * @return boolean 如果操作数据库成功返回true,否则返回false
     */
    public boolean dataChange(String sql) {
        Connection conn = null;
        Statement stmt = null;
        try {
            //获得数据的连接
            conn = JDBCUtils.getConnection();
            //获得Statement对象
            stmt = conn.createStatement();
            //发送SQL语句
            int num = stmt.executeUpdate(sql);
            if (num > 0) {
                return true;
            } else {
                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            new JDBCUtils();
            JDBCUtils.release(stmt, conn);
        }
        return false;
    }
}

该类里只有一个方法,就是添加图书类别记录到数据库中。

 

界面设计

接下来就是创建图书类别添加界面了,在view包下创建bookTypeAddFrame.fxml文件,使用Scene Builder进行设计,代码如下:

<?xml version="1.0" encoding="UTF-8"?>
​
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.Font?>
<AnchorPane prefHeight="700.0" prefWidth="800.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1"
            fx:controller="BookManageSystem.controller.BookTypeAddFrameController">
    <children>
        <VBox alignment="CENTER" prefHeight="700.0" prefWidth="800.0" spacing="30.0">
            <children>
                <HBox alignment="CENTER" prefHeight="130.0" prefWidth="800.0">
                    <children>
                        <Label prefHeight="51.0" prefWidth="322.0" text="图书类别添加功能">
                            <font>
                                <Font name="System Bold" size="40.0"/>
                            </font>
                        </Label>
                    </children>
                </HBox>
                <HBox alignment="CENTER" prefHeight="100.0" prefWidth="200.0" spacing="30.0">
                    <children>
                        <Label text="图书类别名称:"/>
                        <TextField fx:id="bookTypeNameTextField"/>
                    </children>
                </HBox>
                <HBox alignment="TOP_CENTER" prefHeight="144.0" prefWidth="600.0" spacing="30.0">
                    <children>
                        <Label text="图书类别描述:"/>
                        <TextArea fx:id="bookTypeDescriptionTextArea" prefHeight="200.0" prefWidth="200.0"/>
                    </children>
                </HBox>
                <HBox alignment="CENTER" prefHeight="100.0" prefWidth="200.0" spacing="80.0">
                    <children>
                        <Button fx:id="addButton" mnemonicParsing="false" onAction="#do_addButton_event" text="添加"/>
                        <Button fx:id="resetButton" mnemonicParsing="false" onAction="#do_resetButton_event" text="重置"/>
                    </children>
                </HBox>
            </children>
        </VBox>
    </children>
</AnchorPane>

并且在controller包下创建BookTypeAddFrameController.java类,从Scene Builder中复制控制器代码到该类中:

package BookManageSystem.controller;
​
import BookManageSystem.dao.BookTypeDao;
import BookManageSystem.tools.SimpleTools;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.*;
​
public class BookTypeAddFrameController {
    private SimpleTools simpleTools = new SimpleTools();
​
    @FXML
    private TextField bookTypeNameTextField;
​
    @FXML
    private Button addButton;
​
    @FXML
    private TextArea bookTypeDescriptionTextArea;
​
    @FXML
    private Button resetButton;
​
    // “添加”按钮的事件监听器方法
    public void do_addButton_event(ActionEvent event) {
       
    }
​
    // “重置”按钮的事件监听器方法
    public void do_resetButton_event(ActionEvent event) {
        
    }
}

运行项目,界面如下:

 

 

实现功能

为界面按钮添加图标,在BookTypeAddFrameController.java中添加如下方法:

    public void initialize() {
        // 初始化按钮的图标
        simpleTools.setLabeledImage(new Labeled[]{addButton, resetButton}, new String[]{"src/BookManageSystem/images/add.png",
                "src/BookManageSystem/images/reset.png"});
    }

运行后界面如下:

添加按钮的事件处理代码如下:

    // “添加”按钮的事件监听器方法
    public void do_addButton_event(ActionEvent event) {
        // 获取图书类别名称
        String bookTypeName = bookTypeNameTextField.getText();
        // 获取图书类别描述
        String bookTypeDescription = bookTypeDescriptionTextArea.getText();
        // 组装插入SQL语句
        String sql =
                "insert into tb_bookType (btName, btDescription) values ('" + bookTypeName + "','" + bookTypeDescription + "');";
        // 执行添加操作并返回操作结果
        boolean isOK = new BookTypeDao().dataChange(sql);
        // 对操作结果进行判断
        if (isOK) {
            // 添加成功则弹出提示框并清空文本框内容
            simpleTools.informationDialog(Alert.AlertType.INFORMATION, "提示", "信息", "添加成功!");
            bookTypeNameTextField.setText("");
            bookTypeDescriptionTextArea.setText("");
        } else {
            // 添加失败则弹出提示框
            simpleTools.informationDialog(Alert.AlertType.ERROR, "提示", "错误", "添加失败!");
        }
    }

运行添加如下:

img

img

重置按钮的事件处理代码如下:

    // “重置”按钮的事件监听器方法
    public void do_resetButton_event(ActionEvent event) {
        // 重置即清空用户输入的内容
        simpleTools.clearTextField(bookTypeNameTextField, bookTypeDescriptionTextArea);
    }

运行点击重置按钮将清空用户输入。

最后就是切换到图书类别添加界面,所以在MainApp.java中添加如下方法:

    /**
     * 图书类别添加界面
     *
     * @return 返回一个AnchorPane便于其他控件调用
     */
    public AnchorPane initBookTypeAddFrame() {
        try {
            // 加载FXML布局文件
            FXMLLoader loader = new FXMLLoader();
            loader.setLocation(getClass().getResource("view/bookTypeAddFrame.fxml"));
            AnchorPane root = loader.load();
            return root;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

并在MainFrameController.java中对图书类别添加菜单项做事件处理:

    /**
     * “图书类别添加”菜单项的事件处理
     *
     * @param event 事件
     */
    public void do_bookTypeAddMenuItem_event(ActionEvent event) {
        AnchorPane pane = new MainApp().initBookTypeAddFrame();
        mainFrameAnchorPane.getChildren().clear();
        mainFrameAnchorPane.getChildren().add(pane);
    }

 

 

可搜索微信公众号【Java实例程序】或者扫描下方二维码关注公众号获取更多。

注意:在公众号后台回复【20200225】获取本节源码。

  • 3
    点赞
  • 37
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值