本节概要
在上一节中实现了图书类别的维护管理,而在本节将实现图书界面及图书记录的添加。
创建实体类
根据数据库表tb_book创建实体类。
在beans包创建BookBean.java类,其内容如下:
package BookManageSystem.beans;
public class BookBean {
private int bookId;
private String bookName;
private String bookAuthor;
private String bookAuthorSex;
private float bookPrice;
private String bookDescription;
private String bookTypeId;
public BookBean() { }
public BookBean(int bookId, String bookName, String bookAuthor, String bookAuthorSex, float bookPrice, String bookDescription, String bookTypeId) {
this.bookId = bookId;
this.bookName = bookName;
this.bookAuthor = bookAuthor;
this.bookAuthorSex = bookAuthorSex;
this.bookPrice = bookPrice;
this.bookDescription = bookDescription;
this.bookTypeId = bookTypeId;
}
public int getBookId() {
return bookId;
}
public void setBookId(int bookId) {
this.bookId = bookId;
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public String getBookAuthor() {
return bookAuthor;
}
public void setBookAuthor(String bookAuthor) {
this.bookAuthor = bookAuthor;
}
public String getBookAuthorSex() {
return bookAuthorSex;
}
public void setBookAuthorSex(String bookAuthorSex) {
this.bookAuthorSex = bookAuthorSex;
}
public float getBookPrice() {
return bookPrice;
}
public void setBookPrice(float bookPrice) {
this.bookPrice = bookPrice;
}
public String getBookDescription() {
return bookDescription;
}
public void setBookDescription(String bookDescription) {
this.bookDescription = bookDescription;
}
public String getBookTypeId() {
return bookTypeId;
}
public void setBookTypeId(String bookTypeId) {
this.bookTypeId = bookTypeId;
}
}
Dao层方法
接着就是写添加记录到数据库的方法。
在dao包下创建BookDao.java类,其中代码如下:
package BookManageSystem.dao;
import java.sql.Connection;
import java.sql.Statement;
public class BookDao {
/**
* 操作结果:根据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);
return num > 0;
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCUtils.release(stmt, conn);
}
return false;
}
}
里面只有一个方法即是添加图书记录到数据库表中。
界面设计
接下来就是创建图书添加界面了,在view包下创建bookAddFrame.fxml文件,使用Scene Builder进行设计,代码如下:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.*?>
<?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.BookAddFrameController">
<children>
<VBox alignment="CENTER" prefHeight="700.0" prefWidth="800.0">
<children>
<HBox alignment="CENTER" prefHeight="93.0" prefWidth="800.0">
<children>
<Label text="图书添加功能">
<font>
<Font name="System Bold" size="40.0"/>
</font>
</Label>
</children>
</HBox>
<HBox alignment="CENTER_LEFT" prefHeight="100.0" prefWidth="200.0" spacing="32.0">
<children>
<Label text="图书名称:"/>
<TextField fx:id="bookNameTextField" prefHeight="30.0" prefWidth="185.0"/>
<Label text="图书作者:"/>
<TextField fx:id="bookAuthorTextField" prefHeight="30.0" prefWidth="120.0"/>
</children>
<padding>
<Insets left="80.0"/>
</padding>
<opaqueInsets>
<Insets/>
</opaqueInsets>
</HBox>
<HBox alignment="CENTER_LEFT" prefHeight="100.0" prefWidth="200.0" spacing="20.0">
<children>
<Label text="作者性别:"/>
<HBox alignment="CENTER" prefHeight="100.0" prefWidth="208.0" spacing="60.0">
<children>
<RadioButton fx:id="maleRadioButton" mnemonicParsing="false" text="男">
<toggleGroup>
<ToggleGroup fx:id="sex"/>
</toggleGroup>
</RadioButton>
<RadioButton fx:id="femaleRadioButton" mnemonicParsing="false" text="女"
toggleGroup="$sex"/>
</children>
</HBox>
<Label text="图书价格:"/>
<TextField fx:id="bookPriceTextField" prefHeight="30.0" prefWidth="122.0">
<HBox.margin>
<Insets left="10.0"/>
</HBox.margin>
</TextField>
</children>
<padding>
<Insets left="80.0"/>
</padding>
<opaqueInsets>
<Insets/>
</opaqueInsets>
</HBox>
<HBox alignment="CENTER_LEFT" prefHeight="100.0" prefWidth="200.0" spacing="40.0">
<children>
<Label text="图书类别:"/>
<ComboBox fx:id="bookTypeComboBox" prefHeight="30.0" prefWidth="174.0"/>
</children>
<padding>
<Insets left="80.0"/>
</padding>
<opaqueInsets>
<Insets/>
</opaqueInsets>
</HBox>
<HBox prefHeight="100.0" prefWidth="200.0" spacing="40.0">
<children>
<Label text="图书描述:"/>
<TextArea fx:id="bookDescriptionTextArea" prefHeight="100.0" prefWidth="436.0"/>
</children>
<padding>
<Insets left="80.0"/>
</padding>
<opaqueInsets>
<Insets/>
</opaqueInsets>
</HBox>
<HBox alignment="CENTER" prefHeight="100.0" prefWidth="200.0" spacing="100.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>
<padding>
<Insets left="20.0"/>
</padding>
</HBox>
</children>
</VBox>
</children>
</AnchorPane>
并且在controller包下创建BookAddFrameController.java类,从Scene Builder中复制控制器代码到该类中,并实例化SimpleTools类:
package BookManageSystem.controller;
import BookManageSystem.beans.BookTypeBean;
import BookManageSystem.dao.BookDao;
import BookManageSystem.dao.BookTypeDao;
import BookManageSystem.tools.SimpleTools;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import java.util.List;
public class BookAddFrameController {
private SimpleTools simpleTools = new SimpleTools();
@FXML
private TextField bookAuthorTextField;
@FXML
private RadioButton femaleRadioButton;
@FXML
private TextField bookPriceTextField;
@FXML
private ComboBox<?> bookTypeComboBox;
@FXML
private RadioButton maleRadioButton;
@FXML
private Button addButton;
@FXML
private Button resetButton;
@FXML
private TextField bookNameTextField;
@FXML
private TextArea bookDescriptionTextArea;
// 【添加】按钮的事件监听器
public void do_addButton_event(ActionEvent event) {
}
// 【重置】按钮的事件监听器
public void do_resetButton_event(ActionEvent event) {
}
}
界面设计完毕后就是加载该FXML所生成的界面。
在MainApp.java中添加如下方法加载FXML文件:
/**
* 图书添加界面
* @return 返回一个AnchorPane便于其他控件调用
*/
public AnchorPane initBookAddFrame() {
try {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("view/bookAddFrame.fxml"));
AnchorPane pane = loader.load();
return pane;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
并在MainFrameController.java处理“图书添加”菜单项事件中进行调用:
/**
* “图书添加”菜单项的事件处理
* @param event 事件
*/
public void do_bookAddMenuItem_event(ActionEvent event) {
// 当点击“图书添加”菜单项后,加载图书添加面板
AnchorPane pane = new MainApp().initBookAddFrame();
// 清空界面上原有的控件
mainFrameAnchorPane.getChildren().clear();
// 将图书添加面板添加到界面上
mainFrameAnchorPane.getChildren().add(pane);
}
运行项目,界面如下:
实现功能
为界面按钮添加图标和初始化下拉列表框的选项,在BookAddFrameController.java中添加initialize方法来完成。
/**
* 初始化界面控件数据
*/
public void initialize() {
// 批量为按钮添加图标
simpleTools.setLabeledImage(new Labeled[]{addButton, resetButton}, new String[]{"src/BookManageSystem/images/add.png",
"src/BookManageSystem/images/reset.png"});
// 查询所有图书类别的SQL语句
String getBookTypeSQL = "select * from tb_booktype";
// 获取所有图书类别的数据
List bookTypeList = new BookTypeDao().getRecordsDataBySql(getBookTypeSQL);
// 获取所有的图书类别名称
String[] typeNames = new String[bookTypeList.size()];
for (int i = 0; i < bookTypeList.size(); i++) {
BookTypeBean bookTypeBean = (BookTypeBean) bookTypeList.get(i);
typeNames[i] = bookTypeBean.getBookTypeName();
}
// 初始化下拉列表框的选项
simpleTools.addComboBoxItems(bookTypeComboBox, typeNames);
}
运行效果如下:
添加按钮的事件处理代码如下:
// 【添加】按钮的事件监听器
public void do_addButton_event(ActionEvent event) {
// 获取用户输入的图书名称
String name = bookNameTextField.getText();
// 获取用户输入的图书作者
String author = bookAuthorTextField.getText();
// 获取用户选中的图书作者性别
String sex = "";
if (maleRadioButton.isSelected()) {
sex = maleRadioButton.getText();
} else if (femaleRadioButton.isSelected()) {
sex = femaleRadioButton.getText();
}
// 获取用户输入的图书价格
String price = bookPriceTextField.getText();
// 获取用户选择的图书类别
String type = (String) bookTypeComboBox.getSelectionModel().selectedItemProperty().getValue();
// 获取用户输入的图书描述
String description = bookDescriptionTextArea.getText();
// 条件查询图书类别
String bookTypeSQL = "select * from tb_booktype where btName='" + type + "';";
List bookTypeList = new BookTypeDao().getRecordsDataBySql(bookTypeSQL);
BookTypeBean bookTypeBean = (BookTypeBean) bookTypeList.get(0);
// 获取图书类别的id号
int bookTypeId = bookTypeBean.getBookTypeId();
// 组装添加数据的SQL语句
String sql =
"insert into tb_book(bBookName, bAuthor, bSex, bPrice, bBookDescription, btId) values('" + name + "'," +
"'" + author + "','" + sex + "'," + price + ",'" + description + "'," + bookTypeId + ");";
// 执行添加操作并获取返回结果
boolean isOK = new BookDao().dataChange(sql);
// 对结果进行判断
if (isOK) {
// 添加成功则重置用户输入并弹出提示框
resetContent();
simpleTools.informationDialog(Alert.AlertType.INFORMATION, "提示", "信息", "添加成功!");
} else {
// 添加失败也弹出提示框
simpleTools.informationDialog(Alert.AlertType.ERROR, "提示", "错误", "添加失败!");
}
}
重置按钮的事件处理代码如下:
// 【重置】按钮的事件监听器
public void do_resetButton_event(ActionEvent event) {
// 重置用户输入及选择
resetContent();
}
/**
* 重置文本框、单选按钮和下拉列表框
*/
private void resetContent() {
simpleTools.clearTextField(bookNameTextField, bookAuthorTextField, bookPriceTextField, bookDescriptionTextArea);
simpleTools.clearSelectedRadioButton(maleRadioButton, femaleRadioButton);
simpleTools.clearSelectedComboBox(bookTypeComboBox);
}
即清空用户输入和选择。
可搜索微信公众号【Java实例程序】或者扫描下方二维码关注公众号获取更多。
注意:在公众号后台回复【20200229】获取本节源码。
来源:CSDN
作者:二木成林
链接:https://blog.csdn.net/cnds123321/article/details/103980999