管家婆财务软件做账原理

原创 用友财务软件  2022-08-26 08:30:11  阅读 460 次 评论 0 条
摘要:

本节概要 本节将实现数据库记录的备份、恢复功能和软件的退出。 备份功能 备份功能同样是在MainPageController.java中的backupMenuItemEvent()方法所触发的,即

本文主要分享【管家婆财务软件做账原理】,技术文章【Java实战之管家婆记账系统(7)——备份、恢复和退出功能实现】为【二木成林】投稿,如果你遇到相关问题,请看以下文章。

管家婆财务软件做账原理

本节概要

本节将实现数据库记录的备份、恢复功能和软件的退出。

?

备份功能

备份功能同样是在MainPageController.java中的backupMenuItemEvent()方法所触发的,即事件处理代码写在该方法内。

注意,需要在db.properties中添加一对键值对,即是数据库名称,你要备份的数据库名称。

 ? ?/**
 ? ? * “备份”菜单项的事件监听器
 ? ? *
 ? ? * @param actionEvent 事件
 ? ? */
 ? ?@FXML
 ? ?public void backupMenuItemEvent(ActionEvent actionEvent) throws IOException {
 ? ? ? ?//实例化文件选择器
 ? ? ? ?FileChooser fileChooser = new FileChooser();
 ? ? ? ?//设置打开文件选择框默认输入的文件名
 ? ? ? ?fileChooser.setInitialFileName("Database_Backup_" + dateTools.dateFormat(new Date(), "yyyy-MM-dd") + ".sql");
 ? ? ? ?//打开文件选择框
 ? ? ? ?File result = fileChooser.showSaveDialog(null);
 ? ? ? ?if (result != null) {
 ? ? ? ? ? ?String savePath = result.getAbsolutePath();
 ? ? ? ? ? ?// 实例化Properties对象
 ? ? ? ? ? ?Properties properties = new Properties();
 ? ? ? ? ? ?// 加载properties配置文件
 ? ? ? ? ? ?FileInputStream fis = new FileInputStream(new File("src\\AccountSystem\\properties\\db.properties"));
 ? ? ? ? ? ?properties.load(fis);
 ? ? ? ? ? ?// 通过键名获取对应的值
 ? ? ? ? ? ?String databaseName = properties.get("databaseName").toString();
 ? ? ? ? ? ?String user = properties.get("user").toString();
 ? ? ? ? ? ?String password = properties.get("password").toString();
 ? ? ? ? ? ?// 调用备份方法需要提供MySQL的用户名、密码和数据库名,这些数据从properties文件中读取
 ? ? ? ? ? ?boolean b = JDBCUtils.backup(user, password, savePath, databaseName);
 ? ? ? ? ? ?if (b) {
 ? ? ? ? ? ? ? ?SimpleTools.informationDialog(Alert.AlertType.INFORMATION, "提示", "信息", "备份数据库成功!");
 ? ? ? ? ?  } else {
 ? ? ? ? ? ? ? ?SimpleTools.informationDialog(Alert.AlertType.ERROR, "提示", "错误", "备份数据库失败!");
 ? ? ? ? ?  }
 ? ? ? ? ? ?// 关闭流
 ? ? ? ? ? ?fis.close();
 ? ? ?  }
 ?  }

通过FileChooser选择要备份的路径,然后读取db.properties中的数据库名称以及用户和密码,然后将其作为参数传递到JDBCUtils的backup方法中,执行备份操作。该backup()本质上是执行的DOS命令,只不过是封装到方法内而已。

运行程序,测试功能:

备份成功后,用记事本打开,可以看到如下信息:

如果输出backup()方法的stmt,即可在控制台看到输出的信息:

mysqldump -uroot -padmin db_bookkeepingSystem > C:\Users\Administrator\Music\Database_Backup_2020-02-12.sql

其中root是用户名,admin是数据库登录密码,db_bookkeepingSystem是要备份的数据库名称,而后面的路径是要保存的sql文件地址。

把上面的这个语句在DOS窗口也可以得到执行:

?

恢复功能

同样是写在recoverMenuItemEvent()方法内的事件处理,代码如下:

 ? ?/**
 ? ? * “恢复”菜单项的事件监听器
 ? ? *
 ? ? * @param actionEvent 事件
 ? ? */
 ? ?@FXML
 ? ?public void recoverMenuItemEvent(ActionEvent actionEvent) throws IOException {
 ? ? ? ?//实例化文件选择器
 ? ? ? ?FileChooser fileChooser = new FileChooser();
 ? ? ? ?//设置默认文件过滤器
 ? ? ? ?fileChooser.setSelectedExtensionFilter(new FileChooser.ExtensionFilter("sql(*.sql)", "sql"));
 ? ? ? ?//打开文件选择框
 ? ? ? ?File result = fileChooser.showOpenDialog(null);
 ? ? ? ?if (result != null) {
 ? ? ? ? ? ?// 恢复文件的路径
 ? ? ? ? ? ?String recoverPath = result.getAbsolutePath();
 ? ? ? ? ? ?// 实例化Properties对象
 ? ? ? ? ? ?Properties properties = new Properties();
 ? ? ? ? ? ?// 加载properties配置文件
 ? ? ? ? ? ?FileInputStream fis = new FileInputStream(new File("src\\AccountSystem\\properties\\db.properties"));
 ? ? ? ? ? ?properties.load(fis);
 ? ? ? ? ? ?// 通过键名获取对应的值
 ? ? ? ? ? ?String databaseName = properties.get("databaseName").toString();
 ? ? ? ? ? ?String user = properties.get("user").toString();
 ? ? ? ? ? ?String password = properties.get("password").toString();
 ? ? ? ? ? ?boolean b = JDBCUtils.recover(user, password, databaseName, recoverPath);
 ? ? ? ? ? ?if (b) {
 ? ? ? ? ? ? ? ?// 刷新界面显示的数据
 ? ? ? ? ? ? ? ?initialize();
 ? ? ? ? ? ? ? ?SimpleTools.informationDialog(Alert.AlertType.INFORMATION, "提示", "信息", "数据库恢复成功!");
 ? ? ? ? ?  } else {
 ? ? ? ? ? ? ? ?SimpleTools.informationDialog(Alert.AlertType.ERROR, "提示", "错误", "数据库恢复失败!");
 ? ? ? ? ?  }
 ? ? ? ? ? ?// 关闭流
 ? ? ? ? ? ?fis.close();
 ? ? ?  }
 ?  }

同样调用了JDBCUtils类中的recover方法。

?

退出功能

退出功能即结束程序,通常使用

System.exit(0);

因此退出菜单项的事件监听器代码就是下面这些了:

 ? ?/**
 ? ? * “退出”菜单项的事件监听器
 ? ? *
 ? ? * @param actionEvent 事件
 ? ? */
 ? ?@FXML
 ? ?public void exitMenuItemEvent(ActionEvent actionEvent) {
 ? ? ? ?System.exit(0);
 ?  }

然后点击”退出“菜单项就会结束程序。

?

?

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

注意:在公众号后台回复【20200321】可获取本章的源码 。

本文《Java实战之管家婆记账系统(7)——备份、恢复和退出功能实现》版权归二木成林所有,引用管家婆财务软件做账原理需遵循CC 4.0 BY-SA版权协议。

本文地址:https://www.ufidawhy.com/gjp/120053.html
版权声明:本文来源于网络,如有侵权请E-mail联系 ufidawhy 站长 ufidawhy@vip.qq.com!

评论已关闭!