本节概要
在上一节中我们实现了图书类别添加的功能,那么这一节主要将实现图书类别的管理,即图书类别的查询、删除和修改功能。
图书类别维护界面
由于需要在数据库表中进行查询,所以在BookTypeDao.java中新写了两个方法,代码如下:
/**
* 操作结果:根据参数sql获取数据库记录数据
*
* @param sql SQL语句
* @return List 返回包含记录Records对象的集合
*/
public List getRecordsDataBySql(String sql) {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
List list = new ArrayList();
try {
//获得数据的连接
conn = JDBCUtils.getConnection();
//获得Statement对象
stmt = conn.createStatement();
//发送SQL语句并获取结果集
rs = stmt.executeQuery(sql);
// 循环遍历结果集
while (rs.next()) {
// 实例化图书类别实体类并将结果集中的数据封装到实体类中
BookTypeBean bookTypeBean = new BookTypeBean();
bookTypeBean.setBookTypeId(rs.getInt(1));
bookTypeBean.setBookTypeName(rs.getString(2));
bookTypeBean.setBookTypeDescription(rs.getString(3));
// 将实体类添加到集合中
list.add(bookTypeBean);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCUtils.release(rs, stmt, conn);
}
return list;
}
/**
* 操作结果:将集合转换成数组
*
* @param list 集合
* @return String[][] 二维数组
*/
public String[][] ListToArray(List<BookTypeBean> list) {
String[][] array = new String[list.size()][3];
for (int i = 0; i < list.size(); i++) {
BookTypeBean l = list.get(i);
array[i][0] = String.valueOf(l.getBookTypeId());
array[i][1] = l.getBookTypeName();
array[i][2] = l.getBookTypeDescription();
}
return array;
}
这两个方法实现了通过SQL语句从数据库表中读取数据以及将List类型的数据转换成二维数组形式的。
将BookTypeManagePanel.java的内容变成如下代码,即完成了界面:
package bookManageSystem.view;
import bookManageSystem.tools.ComponentTools;
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class BookTypeManagePanel extends JPanel implements ActionListener, ListSelectionListener {
private ComponentTools componentTools = new ComponentTools();
private Box totalVBox, funcationHBox, bookTypeCheckHBox, tableHBox, idAndTypeHBox, descriptionHBox, buttonHBox;
private JButton checkButton, alterButton, deleteButton;
private JLabel bookTypeManageLabel, bookTypeNameLabel1, bookTypeNameLabel2, idLabel, descriptionLabel;
private JTextField bookTypeNameTextField1, bookTypeNameTextField2, idTextField;
private JTextArea descriptionTextArea;
private JScrollPane tableScrollPanel;
private JTable table;
private DefaultTableModel tableModel;
BookTypeManagePanel() {
// 添加控件到图书类别维护面板中
this.add(createBookTypeManageVBox());
// 为按钮批量设置图标
componentTools.setIcons(new JButton[]{alterButton, deleteButton}, new String[]{"src/bookManageSystem/images/edit.png", "src/bookManageSystem/images/delete.png"});
}
/**
* 图书类别维护面板中的控件内容
*
* @return 返回一个Box
*/
private Box createBookTypeManageVBox() {
totalVBox = Box.createVerticalBox();
funcationHBox = Box.createHorizontalBox();
bookTypeManageLabel = new JLabel("图书类别维护功能");
bookTypeManageLabel.setFont(new Font("微软雅黑", Font.BOLD, 30));
funcationHBox.add(bookTypeManageLabel);
totalVBox.add(funcationHBox);
totalVBox.add(Box.createVerticalStrut(20));
bookTypeCheckHBox = Box.createHorizontalBox();
bookTypeNameLabel1 = new JLabel("图书类别名称:");
bookTypeNameTextField1 = new JTextField(10);
checkButton = new JButton("查询");
bookTypeCheckHBox.add(bookTypeNameLabel1);
bookTypeCheckHBox.add(Box.createHorizontalStrut(30));
bookTypeCheckHBox.add(bookTypeNameTextField1);
bookTypeCheckHBox.add(Box.createHorizontalStrut(30));
bookTypeCheckHBox.add(checkButton);
totalVBox.add(bookTypeCheckHBox);
totalVBox.add(Box.createVerticalStrut(20));
tableHBox = Box.createHorizontalBox();
// 实例化一个滚动面板
tableScrollPanel = new JScrollPane();
// 设置滚动面板预定义尺寸
tableScrollPanel.setPreferredSize(new Dimension(700, 250));
// 将滚动面板添加到HBox中
tableHBox.add(tableScrollPanel);
totalVBox.add(tableHBox);
totalVBox.add(Box.createVerticalStrut(20));
idAndTypeHBox = Box.createHorizontalBox();
idLabel = new JLabel("编号:");
idTextField = new JTextField(10);
idTextField.setEnabled(false);
bookTypeNameLabel2 = new JLabel("图书类别名称:");
bookTypeNameTextField2 = new JTextField(10);
idAndTypeHBox.add(idLabel);
idAndTypeHBox.add(Box.createHorizontalStrut(20));
idAndTypeHBox.add(idTextField);
idAndTypeHBox.add(Box.createHorizontalStrut(20));
idAndTypeHBox.add(bookTypeNameLabel2);
idAndTypeHBox.add(Box.createHorizontalStrut(20));
idAndTypeHBox.add(bookTypeNameTextField2);
totalVBox.add(idAndTypeHBox);
totalVBox.add(Box.createVerticalStrut(20));
descriptionHBox = Box.createHorizontalBox();
descriptionLabel = new JLabel("描述:");
descriptionTextArea = new JTextArea(5, 40);
descriptionTextArea.setLineWrap(true);
descriptionHBox.add(descriptionLabel);
descriptionHBox.add(Box.createHorizontalStrut(20));
descriptionHBox.add(descriptionTextArea);
totalVBox.add(descriptionHBox);
totalVBox.add(Box.createVerticalStrut(20));
buttonHBox = Box.createHorizontalBox();
alterButton = new JButton("修改");
deleteButton = new JButton("删除");
buttonHBox.add(alterButton);
buttonHBox.add(Box.createHorizontalStrut(80));
buttonHBox.add(deleteButton);
totalVBox.add(buttonHBox);
return totalVBox;
}
@Override
public void actionPerformed(ActionEvent e) {
}
@Override
public void valueChanged(ListSelectionEvent e) {
}
}
运行项目,登录成功后切换到”图书类别管理“下的”图书类别维护“的界面:
初始化表格数据
在完成界面所有控件后,就需要显示表格数据,由于是第一次显示表格数据,所以需要查询所有的表格数据,即初始化表格数据。
在createBookTypeManageVBox方法内添加如下几行代码:
// 将表格设置为滚动面板内容视图
tableScrollPanel.setViewportView(this.createTable("select btId,btName,btDescription from tb_booktype"));
添加的位置如下:
并且该类中添加如下方法创建表格JTable控件:
/**
* 根据SQL查询得到的数据填充一个表格控件
*
* @param sql SQL语句
* @return 返回一个JTable控件
*/
private JTable createTable(String sql) {
// 将表格数据转换成一个二维数组
String[][] rowdatas = new BookTypeDao().ListToArray(new BookTypeDao().getRecordsDataBySql(sql));
// 表头内容
String[] headers = {"编号", "图书类别名称", "图书类别描述"};
// 实例化一个表格
table = new JTable();
// 设置行高
table.setRowHeight(30);
// 将表头数据和表格内容数据填充到默认表格模型中,使用表格模型方便获取表格数据内容和刷新表格
tableModel = new DefaultTableModel(rowdatas, headers);
// 设置表格的模型
table.setModel(tableModel);
// 返回表格控件
return table;
}
运行程序,会出现如下界面,即表格初始化数据完成:
图书类别的查询
接着是完成图书类别的查询功能。
首先需要为按钮注册事件,在这里连同其他按钮的事件一起注册了在BookTypeManagePanel()构造器方法中,代码如下:
// 为按钮注册事件监听器
checkButton.addActionListener(this);
alterButton.addActionListener(this);
deleteButton.addActionListener(this);
// 为表格注册监听器
table.getSelectionModel().addListSelectionListener(this);
然后是在actionPerformed方法内添加如下代码,即是”查询“按钮的事件处理:
// “查询”按钮的事件处理
if (e.getSource() == checkButton) {
String sql = "";
// 判断图书类别输入框是否为空并做处理
if (!new SimpleTools().isEmpty(bookTypeNameTextField1.getText())) {
// 为空即查询所有图书类别
sql = "select * from tb_booktype;";
} else {
// 不为空即按条件查询图书类别
sql = "select * from tb_booktype where btName='" + bookTypeNameTextField1.getText() + "';";
}
// 查询结果转换成二维数组
String[][] rowdatas = new BookTypeDao().ListToArray(new BookTypeDao().getRecordsDataBySql(sql));
// 表头数据
String[] headers = {"编号", "图书类别名称", "图书类别描述"};
// 填充表格模型,刷新表格
tableModel.setDataVector(rowdatas, headers);
}
运行程序,输入图书类别名称进行查询,得出如下结果:
表格行选中显示数据
当表格中的某一行被选中后,数据会出现在下面的各个文本框中。
即重写valueChanged()方法即可,该方法的代码变成如下:
@Override
public void valueChanged(ListSelectionEvent e) {
// 获取表格被选中行
int getSelectedRowIndex = table.getSelectedRow();
// 判断是否未选中行
if (getSelectedRowIndex == -1) {
refreshTable();
} else {
// 将选中的表格行数据依次填充到下面的控件中
idTextField.setText((String) table.getValueAt(getSelectedRowIndex, 0));
bookTypeNameTextField2.setText((String) table.getValueAt(getSelectedRowIndex, 1));
descriptionTextArea.setText((String) table.getValueAt(getSelectedRowIndex, 2));
}
}
并且在该类中添加私有方法refreshTable去刷新表格数据:
/**
* 刷新表格数据
*/
private void refreshTable() {
// 查询所有的图书类别数据信息
String sql = "select * from tb_booktype;";
// 将获取的表格数据转换成二维数组
String[][] rowdatas = new BookTypeDao().ListToArray(new BookTypeDao().getRecordsDataBySql(sql));
// 表头数据
String[] headers = {"编号", "图书类别名称", "图书类别描述"};
// 重新填充表格模型,即刷新表格数据
tableModel.setDataVector(rowdatas, headers);
}
运行项目,选中表格中的某一行,出现如下结果:
图书类别的修改
实现图书类别的修改,即更新数据库表的数据即可,并且刷新显示在表格中的数据,在actionPerformed添加如下代码:
// “修改”按钮的事件处理
if (e.getSource() == alterButton) {
// 获取各输入框的内容
String id = idTextField.getText();
String name = bookTypeNameTextField2.getText();
String description = descriptionTextArea.getText();
// 组装修改的SQL语句
String alterSQL = "update tb_booktype set btName='" + name + "',btDescription='" + description + "' where" +
" btId=" + id + ";";
// 执行修改操作并返回结果
boolean isOK = new BookTypeDao().dataChange(alterSQL);
// 对修改结果进行判定
if (isOK) {
// 修改成功则刷新表格
refreshTable();
// 重置各输入框
new ComponentTools().reset(idTextField, bookTypeNameTextField2, descriptionTextArea);
// 弹出提示框
JOptionPane.showMessageDialog(null, "修改成功!");
} else {
// 修改失败也弹出提示框
JOptionPane.showMessageDialog(null, "修改失败!");
}
}
运行程序,修改数据,结果如下:
图书类别的删除
删除选中的表格行,在actionPerformed方法中添加如下代码:
// “删除”按钮的事件处理
if (e.getSource() == deleteButton) {
// 获取要删除的id
String id = idTextField.getText();
// 组装删除SQL并且删除后对主键进行重置
String sql1 = "set FOREIGN_KEY_CHECKS=0;";
String deleteSQL = "delete from tb_booktype where btId=" + id + ";";
String sql2 = "set FOREIGN_KEY_CHECKS=1;";
// 弹出确认框
int isOK = JOptionPane.showConfirmDialog(null, "是否确认删除?");
// 点击确定按钮则继续操作
if (isOK == JOptionPane.OK_OPTION) {
// 执行SQL语句
new BookTypeDao().dataChange(sql1);
boolean is = new BookTypeDao().dataChange(deleteSQL);
new BookTypeDao().dataChange(sql2);
// 对删除结果进行判定
if (is) {
// 删除成功则删除表格并弹出提示框
refreshTable();
new ComponentTools().reset(idTextField, bookTypeNameTextField2, descriptionTextArea);
} else {
// 删除失败也弹出提示框
JOptionPane.showMessageDialog(null, "删除失败!");
}
} else {
return;
}
}
添加位置如下:
运行项目,执行删除操作后,结果如下:
删除成功:
可搜索微信公众号【Java实例程序】或者扫描下方二维码关注公众号获取更多。
注意:在公众号后台回复【20200204】可获取本节的源码。