当前位置:   article > 正文

JavaFX+MySQL实现图书管理系统(包含完整代码)_javafx+mysql实现图书管理系统

javafx+mysql实现图书管理系统

目录

一、功能介绍

1.ER图 

2.学生管理员登陆页面

3.图书馆首页

4.学生图书馆搜索界面

5.学生注册

6.管理员后台

增加书籍

查看借阅记录

​编辑

修改图书

二、功能分解

1.界面展示

2.点击按钮切换页面

普通按钮点击

复杂按钮点击

3.登录注册

创建登陆注册的页面(图为登录)

设置两个输入框的id

带有sql语句的方法

写登录页面登录按钮的点击方法

在FXML文件里设置控件点击事件

注册功能的sql语句方法

4.表格控件的绑定

实体类

表格控件的Controller类

  FXML文件

给整个表格设置id

给各列设置id

5.增、删、改

从表格选择图书并删除

修改

6.模糊查询

7.左外连接查询

三、总代码

页面类

控制器类

HelloController类

图书馆控制类

搜索页面控制类

管理员后台控制类

增加图书页面控制类

修改图书控制类

管理员查看图书借阅记录控制类

实体类

图书馆

管理员后台类

type对应type-name类

JDBC封装以及一个带有sql语句的方法的类

SQL语句方法类

JDBC封装类

FXML文件

登录

登陆失败

注册

注册失败

管理员登陆

图书馆

搜索

借书成功

借书失败

还书成功

还书失败

管理员后台

查询借阅记录

修改图书

增加图书

增加成功


一、功能介绍

1.ER图 

2.学生管理员登陆页面

3.图书馆首页

其他几个按扭点击均会出现不同类型的书

4.学生图书馆搜索界面

5.学生注册

6.管理员后台

增加书籍

查看借阅记录
修改图书

二、功能分解

1.界面展示

在HelloApplication类里,首先定义一个公共舞台 

    public static Stage primaryStage;

然后创建不同的场景,定义一个开始方法展示程序运行时展示的第一个页面

  1. public void start(Stage primaryStage) throws IOException {
  2. this.primaryStage = primaryStage;
  3. starScene();
  4. }

然后创建其他场景(这里举个例子):

  1. //学生登录
  2. public static void starScene() {
  3. try {
  4. FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("hello-view.fxml"));
  5. Scene scene = new Scene(fxmlLoader.load(), 693,460);
  6. primaryStage.setResizable(false);//设置页面大小固定
  7. primaryStage.setTitle("欢迎来到网络图书馆");
  8. primaryStage.setScene(scene);
  9. primaryStage.show();
  10. } catch (IOException e) {
  11. e.printStackTrace();
  12. }
  13. }

2.点击按钮切换页面

这个功能实现运用到了控制类 ,JavaFX项目在刚开始创建时有两个类,一个是HelloApplication另一个是HelloController中HelloApplication用于写各种方法并绑定不同的FXML文件,HelloController控制类处理用于控制FXML文件中的UI元素和处理逻辑。

在HelloController类中创建按钮点击时调用的HelloApplication的不同场景来实现页面转换,展示代码如下:

普通按钮点击
  1. @FXML
  2. public void back5(MouseEvent mouseEvent){HelloApplication.BackScene();}

这是普通按钮点击,比如关闭按钮,完成按钮;复杂一点的按钮点击事件需要获取页面数值并传入实现对应功能的包含sql语句的方法中比如:

复杂按钮点击
  1. //管理员的登录按钮
  2. @FXML
  3. public void Glogin(MouseEvent mouseEvent) throws SQLException{
  4. String y = usernames.getText();//获取id为usernames的textfield的数值
  5. String m = passwords.getText();
  6. Boolean ok = Student.glogin(y,m);//使用boolean类型的变量去判断sql语句方法执行的结果
  7. if(ok){
  8. HelloApplication.BackScene();//调用成功之后出现的场景
  9. }else {
  10. if (!errorDisplayed) {
  11. HelloApplication.exit();//失败后调用的页面
  12. errorDisplayed = true;
  13. }
  14. }
  15. }

失败之后应出现小弹窗类型的窗口去提醒客户输入信息有误,所以这时候不能去用HelloApplication的公共舞台,需要自己搭建一个舞台。但是此时会出现一个问题就是当信息有误的情况下,一直点击按钮这个方法不断被调用会出现很多个弹窗窗口导致程序变卡也不美观,解决办法就是在HelloController类里创建一个布尔类型的成员变量为false,如果用户输入消息是错误的情况下曾加一个if判断,用于判断成员变量的值是否不为false,如果为true则调用窗口,同时设置它的值为true(注意if语句里面是!成员变量),然后再次点击时就不会多次调用弹窗方法。

3.登录注册

登陆注册功能实现需要连接数据库

创建登陆注册的页面(图为登录)

设置两个输入框的id

带有sql语句的方法
  1. // login 学生登陆
  2. public static boolean login(String username ,String password) throws SQLException {
  3. String sql = "select id from library.user where name = ? and password = ? and type=0";
  4. Connection conn = JDBC.getConn();
  5. PreparedStatement ps = conn.prepareStatement(sql);
  6. ps.setString(1, username);
  7. ps.setString(2, password);
  8. ResultSet res = ps.executeQuery();//保存查询结果的集合
  9. while (res.next()) {//res.next()是判断下一个结果是否为空,为空代表查询完毕
  10. return true;
  11. }
  12. return false;
  13. }
写登录页面登录按钮的点击方法
  1. //学生页面的登录按钮
  2. @FXML
  3. public void Booklist(MouseEvent mouseEvent) throws SQLException {
  4. u = username.getText();
  5. String p = password.getText();
  6. //SelectController selectController = new SelectController();
  7. //selectController.setValues(u);//将变量u通过这种方法传到SelectController类里
  8. //TypeController typeController = new TypeController();
  9. //typeController.setValues(u);这一部分跟登录方法没有关系
  10. Boolean ok = Student.login(u,p);
  11. if(ok){
  12. HelloApplication.BookScene();
  13. }else {
  14. if (!errorDisplayed) {
  15. HelloApplication.fail();
  16. errorDisplayed = true;
  17. }
  18. }
  19. }
在FXML文件里设置控件点击事件

此时一个登录方法就完成了。注册方法跟登陆方法差不多但是要注意在注册时需要对数据库现有数据进行判断以防止重复数据出现。

注册功能的sql语句方法
  1. //学生注册
  2. public static boolean slogin(String name, String npassword) throws SQLException {
  3. String sql1 = "select id from library.user where name = ? and password = ? and type=0";//学生和管理员在数据库的一张表通过type的值来区别。这句sql语句是查询库里是否存在有着一模一样的用户名和密码的数据
  4. String sql = "insert into library.user (name,password,type) values(?,?,0)";
  5. Connection conn = JDBC.getConn();
  6. PreparedStatement pg =conn.prepareStatement(sql1);
  7. PreparedStatement gp = conn.prepareStatement(sql);
  8. pg.setString(1,name);
  9. pg.setString(2,npassword);
  10. gp.setString(1, name);
  11. gp.setString(2, npassword);
  12. ResultSet re = pg.executeQuery();
  13. int up = gp.executeUpdate();//插入结果后更新的条数,如果插入成功即up不为0
  14. while (up == 0 || re.next()){//若插入失败或者查询的结果有重复的都会返回false
  15. return false;
  16. }
  17. return true;
  18. }

4.表格控件的绑定

实体类

实体类里主要包括各种实例变量的定义以及get、set方法和构造函数对实例变量进行初始化,注意实例变量的类型必须是SimpleStringProperty类

  1. private final SimpleStringProperty name;
  2. private final SimpleStringProperty writer;
  3. private final SimpleIntegerProperty bank;
  4. public SBook(String name, String writer, int bank) {
  5. this.name = new SimpleStringProperty(name);
  6. this.writer= new SimpleStringProperty(writer);
  7. this.bank= new SimpleIntegerProperty(bank);
  8. }
  9. public String getName() {
  10. return name.get();
  11. }
  12. public SimpleStringProperty nameProperty() {
  13. return name;
  14. }
  15. public void setName(String name) {
  16. this.name.set(name);
  17. }
表格控件的Controller类

先定义表格控以及各列的id,查询数据库的数据后返回一个结果集(结果集类型自己定义)传输到各列上面,还有一个默认方法initialize即每次进入当前页面就会刷新表格

  1. public class TypeController {
  2. public static String n;
  3. public TableView tableView;
  4. public TableColumn name;
  5. public TableColumn writer;
  6. public TableColumn bank;
  7. public static int a;
  8. public static String k;
  9. private boolean errorDisplayed = false;
  10. public void initialize() throws SQLException {
  11. allBook();
  12. }
  13. public void allBook()throws SQLException{
  14. String sql = "select name,writer,bank from library.book_list";
  15. Connection conn = JDBC.getConn();
  16. PreparedStatement ps = conn.prepareStatement(sql);
  17. ResultSet resultSet = ps.executeQuery();
  18. ObservableList<SBook> sBookList = FXCollections.observableArrayList();
  19. while (resultSet.next()) {
  20. String sBookName=resultSet.getString("name");
  21. String sBookWriter=resultSet.getString("writer");
  22. int sBookBank = resultSet.getInt("bank");
  23. SBook sBook = new SBook(sBookName,sBookWriter,sBookBank);
  24. sBookList.add(sBook);
  25. // 设置其他属性
  26. }
  27. name.setCellValueFactory(new PropertyValueFactory<>("name"));
  28. writer.setCellValueFactory(new PropertyValueFactory<>("writer"));
  29. bank.setCellValueFactory(new PropertyValueFactory<>("bank"));
  30. tableView.setItems(sBookList);
  31. }
  FXML文件

给整个表格设置id

给各列设置id

5.增、删、改

这几个功能分别用在注册功能、借书还书功能、管理员修改功能、管理员删除功能。

从表格选择图书并删除

借书跟删除功能除了sql语句不一样大体一样,这里删除为例:

  1. // 点击删除按钮
  2. public void delete(MouseEvent mouseEvent) throws SQLException {
  3. // 获取表格视图的选择模型
  4. TableView.TableViewSelectionModel<GBook> selectionModel = GtableView.getSelectionModel();
  5. // 获取被选中的项的列表
  6. ObservableList<GBook> selectedItems = selectionModel.getSelectedItems();
  7. // 检查是否有选中的项
  8. if (!selectedItems.isEmpty()) {
  9. // 假设只选择了一行,获取选中的第一项
  10. GBook selectedItem = selectedItems.get(0);
  11. // 检查是否成功获取选中项
  12. if (selectedItem != null) {
  13. // 获取选中项的作者名字
  14. String writerName = selectedItem.getWriter();
  15. // 调用删除方法,传入作者名字
  16. delete(writerName);
  17. }
  18. // 从表格视图中移除选中的行
  19. ObservableList selectedRows = selectionModel.getSelectedItems();
  20. GtableView.getItems().removeAll(selectedRows);
  21. } else {
  22. // 没有选中项的情况下输出提示
  23. System.out.println("请选择一本书");
  24. }
  25. }
修改

修改方法主要使用在管理员从图书馆后台选择书籍去对书籍的名字、作者、种类、库存进行修改。这里在选择某一行时要获取这一行图书的信息并存为成员变量,再点击修改时修改页面就会出现原有的信息。

  1. //管理员看到图书页面的修改按钮
  2. @FXML
  3. public void alter(MouseEvent mouseEvent){
  4. TableView.TableViewSelectionModel<GBook> selectionModel = GtableView.getSelectionModel();
  5. ObservableList<GBook> selectedItems = selectionModel.getSelectedItems();
  6. if(!selectedItems.isEmpty()){
  7. GBook selectedItem = selectedItems.get(0);
  8. if (selectedItem != null) {
  9. int i = selectedItem.getId();
  10. n = selectedItem.getName();
  11. w = selectedItem.getWriter();
  12. t = selectedItem.getType();
  13. b = selectedItem.getBank();
  14. AlterController alterController=new AlterController();
  15. alterController.setValue(n,w,t,b,i);//传入修改的页面Controller类,使TextFiled获取数据
  16. HelloApplication.AlterScene();
  17. }
  18. }else{
  19. System.out.println("请选择一本书");
  20. }
  21. }

6.模糊查询

首先使用两个成员变量储存用户输入的值,这两个变量将作为sql语句条件去查询。这里是让用户可以输入书名或者作者名或者两者综合去查询。若两个都输入这时就要筛选同时符合这两个条件的书籍,这里用了MySQL的if函数

  1. public void sou(MouseEvent mouseEvent) throws SQLException {
  2. b = bookname.getText();
  3. w = bookwriter.getText();
  4. loadData();
  5. }
  6. public void loadData() throws SQLException {
  7. String sql = "select name,writer,bank from library.book_list where if(? = '',true, name like ?) and if( ? = '',true,writer like ?)";
  8. Connection conn = JDBC.getConn();
  9. PreparedStatement ps = conn.prepareStatement(sql);
  10. ps.setString(1,b);
  11. ps.setString(2,"%"+b+"%");
  12. ps.setString(3,w);
  13. ps.setString(4,"%"+w+"%");
  14. ResultSet resultSet = ps.executeQuery();
  15. ObservableList<SBook> sBookList = FXCollections.observableArrayList();
  16. while (resultSet.next()) {
  17. String sBookName=resultSet.getString("name");
  18. String sBookWriter=resultSet.getString("writer");
  19. int sBookBank = resultSet.getInt("bank");
  20. SBook sBook = new SBook(sBookName,sBookWriter,sBookBank);
  21. sBookList.add(sBook); // 设置其他属性
  22. }
  23. name.setCellValueFactory(new PropertyValueFactory<>("name"));
  24. writer.setCellValueFactory(new PropertyValueFactory<>("writer"));
  25. bank.setCellValueFactory(new PropertyValueFactory<>("bank"));
  26. tableView.setItems(sBookList);
  27. }

7.左外连接查询

这是两表一起查询,在数据库里图书种类我用了数字代替,而在管理员后台看到的是每种数字代表的图书种类。我又额外增添一个type跟type-name对应的表。

  1. public void initialize() throws SQLException {
  2. loadData();
  3. }
  4. public void loadData() throws SQLException {
  5. String sql = "select * from book_list as b left join book_type as bt on b.type=bt.book_type";//查询图书数字代表的名称采用左外连接的方法
  6. Connection conn = JDBC.getConn();
  7. PreparedStatement ps = conn.prepareStatement(sql);
  8. ResultSet resultSet = ps.executeQuery();
  9. ObservableList<GBook> gBookList = FXCollections.observableArrayList();
  10. while (resultSet.next()) {
  11. String gBookName=resultSet.getString("name");
  12. String gBookWriter=resultSet.getString("writer");
  13. String gBookBank = resultSet.getString("bank");
  14. int gBookId= resultSet.getInt("id");
  15. String gBookUserName=resultSet.getString("username");
  16. String gBookType= resultSet.getString("type_name");
  17. GBook gBook = new GBook(gBookName,gBookWriter,gBookBank,gBookUserName,gBookType,gBookId);
  18. gBookList.add(gBook);
  19. // 设置其他属性
  20. }
  21. Gname.setCellValueFactory(new PropertyValueFactory<>("name"));
  22. Gwriter.setCellValueFactory(new PropertyValueFactory<>("writer"));
  23. Gbank.setCellValueFactory(new PropertyValueFactory<>("bank"));
  24. UserName.setCellValueFactory(new PropertyValueFactory<>("username"));
  25. Gtype.setCellValueFactory(new PropertyValueFactory<>("type"));
  26. GtableView.setItems(gBookList);
  27. }

三、总代码

虽然采用MVC对包进行分类但是代码并没有遵守。每个表格控件对应一个Controller类,其他功能基本在HelloController类里,带有sql的方法有的写在控制类里,有的单独写了一个类。

页面类

  1. package com.example.controller;
  2. import javafx.application.Application;
  3. import javafx.fxml.FXMLLoader;
  4. import javafx.scene.Scene;
  5. import javafx.stage.Stage;
  6. import java.io.IOException;
  7. public class HelloApplication extends Application {
  8. public static Stage primaryStage;
  9. public void start(Stage primaryStage) throws IOException {
  10. this.primaryStage = primaryStage;
  11. starScene();
  12. }
  13. //登陆不成功显示的画面
  14. public static void fail(){
  15. try {
  16. FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("win.fxml"));
  17. Scene scene = new Scene(fxmlLoader.load(), 500,150 );
  18. Stage stage = new Stage();
  19. stage.setResizable(false);
  20. stage.setTitle("登陆失败");
  21. stage.setScene(scene);
  22. stage.show();
  23. } catch (IOException e) {
  24. e.printStackTrace();
  25. }
  26. }
  27. //注册显示用户已存在
  28. public static void exit(){
  29. try {
  30. FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("exit.fxml"));
  31. Scene scene = new Scene(fxmlLoader.load(), 500,150 );
  32. Stage stage = new Stage();
  33. stage.setResizable(false);
  34. stage.setTitle("注册失败");
  35. stage.setScene(scene);
  36. stage.show();
  37. } catch (IOException e) {
  38. e.printStackTrace();
  39. }
  40. }
  41. //还书失败
  42. public static void backFalse(){
  43. try {
  44. FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("backfalse.fxml"));
  45. Scene scene = new Scene(fxmlLoader.load(), 500,150 );
  46. Stage stage = new Stage();
  47. stage.setResizable(false);
  48. stage.setTitle("还书失败");
  49. stage.setScene(scene);
  50. stage.show();
  51. } catch (IOException e) {
  52. e.printStackTrace();
  53. }
  54. }
  55. //还书成功
  56. public static void backWin(){
  57. try {
  58. FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("backwin.fxml"));
  59. Scene scene = new Scene(fxmlLoader.load(), 500,150 );
  60. Stage stage = new Stage();
  61. stage.setResizable(false);
  62. stage.setTitle("还书成功");
  63. stage.setScene(scene);
  64. stage.show();
  65. } catch (IOException e) {
  66. e.printStackTrace();
  67. }
  68. }
  69. //借书成功
  70. public static void last(){
  71. try {
  72. FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("Last.fxml"));
  73. Scene scene = new Scene(fxmlLoader.load(), 500,150 );
  74. Stage stage = new Stage();
  75. stage.setResizable(false);
  76. stage.setTitle("借书成功");
  77. stage.setScene(scene);
  78. stage.show();
  79. } catch (IOException e) {
  80. e.printStackTrace();
  81. }
  82. }
  83. //提醒用户还书
  84. public static void back(){
  85. try {
  86. FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("back.fxml"));
  87. Scene scene = new Scene(fxmlLoader.load(), 500,150 );
  88. Stage stage = new Stage();
  89. stage.setResizable(false);
  90. stage.setTitle("借书失败");
  91. stage.setScene(scene);
  92. stage.show();
  93. } catch (IOException e) {
  94. e.printStackTrace();
  95. }
  96. }
  97. //学生登录
  98. public static void starScene() {
  99. try {
  100. FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("hello-view.fxml"));
  101. Scene scene = new Scene(fxmlLoader.load(), 693,460);
  102. primaryStage.setResizable(false);
  103. primaryStage.setTitle("欢迎来到网络图书馆");
  104. primaryStage.setScene(scene);
  105. primaryStage.show();
  106. } catch (IOException e) {
  107. e.printStackTrace();
  108. }
  109. }
  110. //管理员登陆
  111. public static void loginScene(){
  112. try {
  113. FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("login.fxml"));
  114. Scene scene = new Scene(fxmlLoader.load(), 693, 460);
  115. primaryStage.setResizable(false);
  116. primaryStage.setTitle("图书馆管理");
  117. primaryStage.setScene(scene);
  118. primaryStage.show();
  119. }catch (IOException e){
  120. e.printStackTrace();
  121. }
  122. }
  123. //管理员看到的后台数据
  124. public static void BackScene(){
  125. try {
  126. FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("backstage.fxml"));
  127. Scene scene = new Scene(fxmlLoader.load(), 693, 580);
  128. primaryStage.setResizable(false);
  129. primaryStage.setTitle("后台管理");
  130. primaryStage.setScene(scene);
  131. primaryStage.show();
  132. }catch (IOException e){
  133. e.printStackTrace();
  134. }
  135. }
  136. //增加书籍
  137. public static void addBook(){
  138. try {
  139. FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("addBook.fxml"));
  140. Scene scene = new Scene(fxmlLoader.load(), 693, 500);
  141. primaryStage.setResizable(false);
  142. primaryStage.setTitle("后台管理");
  143. primaryStage.setScene(scene);
  144. primaryStage.show();
  145. } catch (IOException e) {
  146. throw new RuntimeException(e);
  147. }
  148. }
  149. //添加成功的方法
  150. public static void Addwin(){
  151. try {
  152. FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("addwin.fxml"));
  153. Scene scene = new Scene(fxmlLoader.load(), 693, 500);
  154. primaryStage.setResizable(false);
  155. primaryStage.setTitle("成功了");
  156. primaryStage.setScene(scene);
  157. primaryStage.show();
  158. } catch (IOException e) {
  159. throw new RuntimeException(e);
  160. }
  161. }
  162. //查看图书被借阅次数的页面
  163. public static void inBook(){
  164. try {
  165. FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("inBook.fxml"));
  166. Scene scene = new Scene(fxmlLoader.load(), 693, 400);
  167. primaryStage.setResizable(false);
  168. primaryStage.setTitle("后台管理");
  169. primaryStage.setScene(scene);
  170. primaryStage.show();
  171. }catch (IOException e){
  172. e.printStackTrace();
  173. }
  174. }
  175. //注册的方法
  176. public static void registerScene(){
  177. try {
  178. FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("zhuce.fxml"));
  179. Scene scene = new Scene(fxmlLoader.load(), 693, 460);
  180. primaryStage.setResizable(false);
  181. primaryStage.setTitle("注册");
  182. primaryStage.setScene(scene);
  183. primaryStage.show();
  184. } catch (IOException e) {
  185. throw new RuntimeException(e);
  186. }
  187. }
  188. //学生登陆进去后显示的图书种类
  189. public static void BookScene(){
  190. try {
  191. FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("Booktype.fxml"));
  192. Scene scene = new Scene(fxmlLoader.load(), 693, 460);
  193. primaryStage.setResizable(false);
  194. primaryStage.setTitle("图书种类");
  195. primaryStage.setScene(scene);
  196. primaryStage.show();
  197. } catch (IOException e) {
  198. throw new RuntimeException(e);
  199. }
  200. }
  201. //管理员修改图书的页面
  202. public static void AlterScene(){
  203. try {
  204. FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("alter.fxml"));
  205. Scene scene = new Scene(fxmlLoader.load(), 693, 400);
  206. primaryStage.setResizable(false);
  207. primaryStage.setTitle("修改");
  208. primaryStage.setScene(scene);
  209. primaryStage.show();
  210. } catch (IOException e) {
  211. throw new RuntimeException(e);
  212. }
  213. }
  214. //搜索页面
  215. public static void select(){
  216. try {
  217. FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("select.fxml"));
  218. Scene scene = new Scene(fxmlLoader.load(), 693, 515);
  219. primaryStage.setResizable(false);
  220. primaryStage.setTitle("搜索");
  221. primaryStage.setScene(scene);
  222. primaryStage.show();
  223. } catch (IOException e) {
  224. throw new RuntimeException(e);
  225. }
  226. }
  227. public static void main(String[] args) {
  228. launch(args);
  229. }
  230. }

控制器类

HelloController类
  1. package com.example.controller;
  2. import com.example.dao.GBook;
  3. import com.example.dao.JDBC;
  4. import com.example.dao.Student;
  5. import javafx.fxml.FXML;
  6. import javafx.scene.control.*;
  7. import javafx.scene.input.MouseEvent;
  8. import java.sql.Connection;
  9. import java.sql.PreparedStatement;
  10. import java.sql.SQLException;
  11. import java.util.Objects;
  12. public class HelloController {
  13. public int b;
  14. public static String u;
  15. @FXML
  16. private TextField username;
  17. @FXML
  18. private PasswordField password;
  19. @FXML
  20. private TextField usernames;
  21. @FXML
  22. private PasswordField passwords;
  23. @FXML
  24. private TextField Name;
  25. @FXML
  26. private PasswordField npassword;
  27. @FXML
  28. private PasswordField apassword;
  29. private boolean errorDisplayed = false;
  30. public HelloController() {
  31. }
  32. //学生登陆页面的按钮点击跳转管理员登陆界面
  33. @FXML
  34. public void ButtonClick(MouseEvent mouseEvent){HelloApplication.loginScene();}
  35. //管理员的登录按钮
  36. @FXML
  37. public void Glogin(MouseEvent mouseEvent) throws SQLException{
  38. String y = usernames.getText();
  39. String m = passwords.getText();
  40. Boolean ok = Student.glogin(y,m);
  41. if(ok){
  42. HelloApplication.BackScene();
  43. }else {
  44. if (!errorDisplayed) {
  45. HelloApplication.exit();
  46. errorDisplayed = true;
  47. }
  48. }
  49. }
  50. //从管理员登录界面退回到学生登陆界面
  51. @FXML
  52. public void back1(MouseEvent mouseEvent){
  53. HelloApplication.starScene();
  54. }
  55. //添加成功后继续添加书籍的按钮
  56. @FXML
  57. public void back4(MouseEvent mouseEvent){
  58. HelloApplication.addBook();
  59. }
  60. //添加图书成功之后返回后台的按钮
  61. @FXML
  62. public void back5(MouseEvent mouseEvent){HelloApplication.BackScene();}
  63. //学生页面的注册按钮
  64. @FXML
  65. public void register(MouseEvent mouseEvent){HelloApplication.registerScene();}
  66. //学生页面的登录按钮
  67. @FXML
  68. public void Booklist(MouseEvent mouseEvent) throws SQLException {
  69. u = username.getText();
  70. String p = password.getText();
  71. SelectController selectController = new SelectController();
  72. selectController.setValues(u);
  73. TypeController typeController = new TypeController();
  74. typeController.setValues(u);
  75. Boolean ok = Student.login(u,p);
  76. if(ok){
  77. HelloApplication.BookScene();
  78. }else {
  79. // Only display the error page if it hasn't been displayed before.
  80. if (!errorDisplayed) {
  81. HelloApplication.fail();
  82. // Set the flag to true to indicate that the error page has been displayed.
  83. errorDisplayed = true;
  84. }
  85. }
  86. }
  87. //学生页面的注册按钮
  88. @FXML
  89. public void regis(MouseEvent mouseEvent) throws SQLException {
  90. String u = Name.getText();
  91. String p = npassword.getText();
  92. String a = apassword.getText();
  93. if (Objects.equals(p, a)) {
  94. Boolean ok = Student.slogin(u, p);
  95. if (ok)
  96. HelloApplication.starScene();
  97. } else {
  98. // Only display the error page if it hasn't been displayed before.
  99. if (!errorDisplayed) {
  100. HelloApplication.fail();
  101. // Set the flag to true to indicate that the error page has been displayed.
  102. errorDisplayed = true;
  103. }
  104. }
  105. }
  106. //书籍界面通过给b赋值确定图书种类
  107. @FXML
  108. public void type1(MouseEvent mouseEvent){
  109. b=0;
  110. }
  111. @FXML
  112. public void type2(MouseEvent mouseEvent){
  113. b=1;
  114. }
  115. @FXML
  116. public void type3(MouseEvent mouseEvent){
  117. b=2;
  118. }
  119. @FXML
  120. public void type4(MouseEvent mouseEvent){
  121. b=3;
  122. }
  123. //种类界面的搜索按钮
  124. public void select(MouseEvent mouseEvent) {
  125. HelloApplication.select();
  126. }
  127. public void back7(MouseEvent mouseEvent) throws SQLException {
  128. boolean ok = backBook();
  129. if(ok){
  130. HelloApplication.backWin();
  131. }
  132. else{
  133. HelloApplication.backFalse();
  134. }
  135. }
  136. public boolean backBook() throws SQLException {
  137. String sql = "update library.user set BookName = null where name = ?";
  138. String sql2 = "update library.book_list set bank = bank+1 where UserName = ? ";
  139. String sql3 = "update library.book_list set UserName = null where UserName = ?";
  140. Connection con = JDBC.getConn();
  141. PreparedStatement ps = con.prepareStatement(sql);
  142. PreparedStatement pd = con.prepareStatement(sql2);
  143. PreparedStatement pg = con.prepareStatement(sql3);
  144. ps.setString(1,u);
  145. pd.setString(1,u);
  146. pg.setString(1,u);
  147. int rs=ps.executeUpdate();
  148. int rd = pd.executeUpdate();
  149. int rg=pg.executeUpdate();
  150. if(rs!=0 & rd!=0){
  151. if(rg!=0){
  152. return true;
  153. }else{
  154. return false;
  155. }
  156. }
  157. else {
  158. return false;
  159. }
  160. }
  161. public void back9(MouseEvent mouseEvent) {
  162. HelloApplication.starScene();
  163. }
  164. public void back10(MouseEvent mouseEvent) {
  165. HelloApplication.starScene();
  166. }
  167. }
图书馆控制类
  1. package com.example.controller;
  2. import com.example.dao.JDBC;
  3. import com.example.dao.SBook;
  4. import javafx.collections.FXCollections;
  5. import javafx.collections.ObservableList;
  6. import javafx.fxml.FXML;
  7. import javafx.scene.control.TableColumn;
  8. import javafx.scene.control.TableView;
  9. import javafx.scene.control.cell.PropertyValueFactory;
  10. import javafx.scene.input.MouseEvent;
  11. import java.sql.Connection;
  12. import java.sql.PreparedStatement;
  13. import java.sql.ResultSet;
  14. import java.sql.SQLException;
  15. public class TypeController {
  16. public static String n;
  17. public TableView tableView;
  18. public TableColumn name;
  19. public TableColumn writer;
  20. public TableColumn bank;
  21. public static int a;
  22. public static String k;
  23. private boolean errorDisplayed = false;
  24. public void initialize() throws SQLException {
  25. allBook();
  26. }
  27. public void allBook()throws SQLException{
  28. String sql = "select name,writer,bank from library.book_list";
  29. Connection conn = JDBC.getConn();
  30. PreparedStatement ps = conn.prepareStatement(sql);
  31. ResultSet resultSet = ps.executeQuery();
  32. ObservableList<SBook> sBookList = FXCollections.observableArrayList();
  33. while (resultSet.next()) {
  34. String sBookName=resultSet.getString("name");
  35. String sBookWriter=resultSet.getString("writer");
  36. int sBookBank = resultSet.getInt("bank");
  37. SBook sBook = new SBook(sBookName,sBookWriter,sBookBank);
  38. sBookList.add(sBook);
  39. // 设置其他属性
  40. }
  41. name.setCellValueFactory(new PropertyValueFactory<>("name"));
  42. writer.setCellValueFactory(new PropertyValueFactory<>("writer"));
  43. bank.setCellValueFactory(new PropertyValueFactory<>("bank"));
  44. tableView.setItems(sBookList);
  45. }
  46. public void loadData() throws SQLException {
  47. String sql = "select name,writer,bank from library.book_list where type = ?";
  48. Connection conn = JDBC.getConn();
  49. PreparedStatement ps = conn.prepareStatement(sql);
  50. ps.setInt(1,a);
  51. ResultSet resultSet = ps.executeQuery();
  52. ObservableList<SBook> sBookList = FXCollections.observableArrayList();
  53. while (resultSet.next()) {
  54. String sBookName=resultSet.getString("name");
  55. String sBookWriter=resultSet.getString("writer");
  56. int sBookBank = resultSet.getInt("bank");
  57. SBook sBook = new SBook(sBookName,sBookWriter,sBookBank);
  58. sBookList.add(sBook);
  59. // 设置其他属性
  60. }
  61. name.setCellValueFactory(new PropertyValueFactory<>("name"));
  62. writer.setCellValueFactory(new PropertyValueFactory<>("writer"));
  63. bank.setCellValueFactory(new PropertyValueFactory<>("bank"));
  64. tableView.setItems(sBookList);
  65. }
  66. @FXML
  67. public void type1(MouseEvent mouseEvent) throws SQLException {
  68. a=0;
  69. loadData();
  70. }
  71. @FXML
  72. public void type2(MouseEvent mouseEvent) throws SQLException {
  73. a=1;
  74. loadData();
  75. }
  76. @FXML
  77. public void type3(MouseEvent mouseEvent) throws SQLException {
  78. a=2;
  79. loadData();
  80. }
  81. @FXML
  82. public void type4(MouseEvent mouseEvent) throws SQLException {
  83. a=3;
  84. loadData();
  85. }
  86. //显示全部书籍
  87. public void comein(MouseEvent mouseEvent) throws Exception {
  88. initialize();
  89. }
  90. //搜索
  91. public void select(MouseEvent mouseEvent) {
  92. HelloApplication.select();
  93. }
  94. //还书
  95. public void back7(MouseEvent mouseEvent) throws SQLException {
  96. boolean ok = backBook();
  97. if(ok){
  98. HelloApplication.backWin();
  99. allBook();
  100. }
  101. else {
  102. // Only display the error page if it hasn't been displayed before.
  103. if (!errorDisplayed) {
  104. HelloApplication.backFalse();
  105. // Set the flag to true to indicate that the error page has been displayed.
  106. errorDisplayed = true;
  107. }
  108. }
  109. }
  110. public boolean backBook() throws SQLException {
  111. String sql = "update library.user set BookName = null where name = ?";
  112. String sql2 = "update library.book_list set bank = bank+1 where UserName = ? ";
  113. String sql3 = "update library.book_list set UserName = null where UserName = ?";
  114. Connection con = JDBC.getConn();
  115. PreparedStatement ps = con.prepareStatement(sql);
  116. PreparedStatement pd = con.prepareStatement(sql2);
  117. PreparedStatement pg = con.prepareStatement(sql3);
  118. ps.setString(1,n);
  119. pd.setString(1,n);
  120. pg.setString(1,n);
  121. int rs=ps.executeUpdate();
  122. int rd = pd.executeUpdate();
  123. int rg=pg.executeUpdate();
  124. if(rs!=0 & rd!=0){
  125. if(rg!=0){
  126. return true;
  127. }else{
  128. return false;
  129. }
  130. }
  131. else {
  132. return false;
  133. }
  134. }
  135. //选好书之后
  136. public void OK(MouseEvent mouseEvent) throws SQLException {
  137. TableView.TableViewSelectionModel<SBook> selectionModel = tableView.getSelectionModel();
  138. ObservableList<SBook> selectedItems = selectionModel.getSelectedItems();
  139. if(!selectedItems.isEmpty()){
  140. SBook selectedItem = selectedItems.get(0);
  141. if(selectedItem != null){
  142. k = selectedItem.getName();
  143. }
  144. }
  145. else{
  146. System.out.println("请选择一本书");
  147. }
  148. String sql3 = "select BookName from library.user where name=?";
  149. Connection conn = JDBC.getConn();
  150. PreparedStatement pb = conn.prepareStatement(sql3);
  151. pb.setString(1, n);
  152. ResultSet rs = pb.executeQuery();
  153. String bk =null;
  154. while(rs.next()){
  155. bk=rs.getString("BookName");
  156. }
  157. if (bk ==null) {
  158. boolean ok = addName();
  159. boolean kc = bank();
  160. if(ok & kc){
  161. HelloApplication.last();
  162. allBook();
  163. }else{
  164. HelloApplication.back();
  165. }
  166. }
  167. }
  168. public boolean addName() throws SQLException {
  169. String sql = "update library.book_list set UserName = ? where name = ?";
  170. String sql2 = "update library.user set BookName = ? where name = ?";
  171. String sql3="insert into library.book_name (BookName,name) values(?,?)";
  172. Connection conn = JDBC.getConn();
  173. PreparedStatement ps = conn.prepareStatement(sql);
  174. PreparedStatement pg = conn.prepareStatement(sql2);
  175. PreparedStatement gp = conn.prepareStatement(sql3);
  176. ps.setString(1,n);
  177. ps.setString(2,k);
  178. pg.setString(1,k);
  179. pg.setString(2,n);
  180. gp.setString(1,k);
  181. gp.setString(2,n);
  182. int rs = ps.executeUpdate();
  183. int rg = pg.executeUpdate();
  184. int rp = gp.executeUpdate();
  185. if(rs != 0 & rg !=0 & rp != 0){
  186. return true;
  187. }
  188. return false;
  189. }
  190. public boolean bank() throws SQLException{
  191. String sql ="update library.book_list set bank = bank-1 where name = ?";
  192. Connection conn = JDBC.getConn();
  193. PreparedStatement ps = conn.prepareStatement(sql);
  194. ps.setString(1,k);
  195. int t = ps.executeUpdate();
  196. if (t != 0){
  197. return true;
  198. }
  199. return false;
  200. }
  201. //返回登陆页面
  202. public void back9(MouseEvent mouseEvent) {
  203. HelloApplication.starScene();
  204. }
  205. public void setValues(String u) {
  206. n=u;
  207. }
  208. }
搜索页面控制类
  1. package com.example.controller;
  2. import com.example.dao.JDBC;
  3. import com.example.dao.SBook;
  4. import javafx.collections.FXCollections;
  5. import javafx.collections.ObservableList;
  6. import javafx.fxml.FXML;
  7. import javafx.scene.control.TableColumn;
  8. import javafx.scene.control.TableView;
  9. import javafx.scene.control.TextField;
  10. import javafx.scene.control.cell.PropertyValueFactory;
  11. import javafx.scene.input.MouseEvent;
  12. import java.sql.Connection;
  13. import java.sql.PreparedStatement;
  14. import java.sql.ResultSet;
  15. import java.sql.SQLException;
  16. public class SelectController {
  17. @FXML
  18. private TableView tableView;
  19. @FXML
  20. private TableColumn name;
  21. @FXML
  22. private TableColumn writer;
  23. @FXML
  24. private TableColumn bank;
  25. @FXML
  26. private TextField bookname;
  27. @FXML
  28. private TextField bookwriter;
  29. public String b;
  30. public String w;
  31. public static String n;
  32. public static String k;
  33. public void back7(MouseEvent mouseEvent) {
  34. HelloApplication.BookScene();
  35. }
  36. public void sou(MouseEvent mouseEvent) throws SQLException {
  37. b = bookname.getText();
  38. w = bookwriter.getText();
  39. loadData();
  40. }
  41. public void loadData() throws SQLException {
  42. String sql = "select name,writer,bank from library.book_list where if(? = '',true, name like ?) and if( ? = '',true,writer like ?)";
  43. Connection conn = JDBC.getConn();
  44. PreparedStatement ps = conn.prepareStatement(sql);
  45. ps.setString(1,b);
  46. ps.setString(2,"%"+b+"%");
  47. ps.setString(3,w);
  48. ps.setString(4,"%"+w+"%");
  49. ResultSet resultSet = ps.executeQuery();
  50. ObservableList<SBook> sBookList = FXCollections.observableArrayList();
  51. while (resultSet.next()) {
  52. String sBookName=resultSet.getString("name");
  53. String sBookWriter=resultSet.getString("writer");
  54. int sBookBank = resultSet.getInt("bank");
  55. SBook sBook = new SBook(sBookName,sBookWriter,sBookBank);
  56. sBookList.add(sBook); // 设置其他属性
  57. }
  58. name.setCellValueFactory(new PropertyValueFactory<>("name"));
  59. writer.setCellValueFactory(new PropertyValueFactory<>("writer"));
  60. bank.setCellValueFactory(new PropertyValueFactory<>("bank"));
  61. tableView.setItems(sBookList);
  62. }
  63. public void last(MouseEvent mouseEvent)throws SQLException {
  64. TableView.TableViewSelectionModel<SBook> selectionModel = tableView.getSelectionModel();
  65. ObservableList<SBook> selectedItems = selectionModel.getSelectedItems();
  66. if(!selectedItems.isEmpty()){
  67. SBook selectedItem = selectedItems.get(0);
  68. if(selectedItem != null){
  69. k = selectedItem.getName();
  70. }
  71. }else {
  72. System.out.println("请选择一本书");
  73. }
  74. String sql = "select BookName from library.user where name = ?";
  75. String sql3 = "select bank from library.book_list where name=?";
  76. String sql2 ="insert into library.book_name (BookName,name) values(?,?)";
  77. Connection conn = JDBC.getConn();
  78. PreparedStatement ps = conn.prepareStatement(sql);
  79. PreparedStatement pb = conn.prepareStatement(sql3);
  80. PreparedStatement pg = conn.prepareStatement(sql2);
  81. ps.setString(1, n);
  82. pb.setString(1, k);
  83. pg.setString(1,k);
  84. pg.setString(2,n);
  85. ResultSet re = ps.executeQuery();
  86. ResultSet rs = pb.executeQuery();
  87. int rg=pg.executeUpdate();
  88. String bn = null;
  89. int bk = 0;
  90. while (rs.next()&re.next()&rg!=0) {
  91. bk = rs.getInt("bank");
  92. bn=re.getString("BookName");
  93. }
  94. if (bk != 0 &bn ==null) {
  95. boolean ok = addName();
  96. boolean kc = bank();
  97. if(ok & kc){
  98. HelloApplication.last();
  99. }
  100. } else {
  101. HelloApplication.back();
  102. }
  103. }
  104. public boolean addName() throws SQLException {
  105. String sql = "update library.book_list set UserName = ? where name = ?";
  106. String sql2 = "update library.user set BookName = ? where name = ?";
  107. Connection conn = JDBC.getConn();
  108. PreparedStatement ps = conn.prepareStatement(sql);
  109. PreparedStatement pg = conn.prepareStatement(sql2);
  110. ps.setString(1,n);
  111. ps.setString(2,k);
  112. pg.setString(1,k);
  113. pg.setString(2,n);
  114. int rs = ps.executeUpdate();
  115. int rg = pg.executeUpdate();
  116. if(rs != 0 & rg !=0){
  117. return true;
  118. }
  119. return false;
  120. }
  121. public boolean bank() throws SQLException{
  122. String sql ="update library.book_list set bank = bank-1 where name = ?";
  123. Connection conn = JDBC.getConn();
  124. PreparedStatement ps = conn.prepareStatement(sql);
  125. ps.setString(1,k);
  126. int t = ps.executeUpdate();
  127. if (t != 0){
  128. return true;
  129. }
  130. return false;
  131. }
  132. public void setValues(String u) {
  133. n=u;
  134. }
  135. }
管理员后台控制类
  1. package com.example.controller;
  2. import com.example.dao.GBook;
  3. import com.example.dao.JDBC;
  4. import javafx.collections.FXCollections;
  5. import javafx.collections.ObservableList;
  6. import javafx.fxml.FXML;
  7. import javafx.scene.control.TableColumn;
  8. import javafx.scene.control.TableView;
  9. import javafx.scene.control.cell.PropertyValueFactory;
  10. import javafx.scene.input.MouseEvent;
  11. import java.sql.Connection;
  12. import java.sql.PreparedStatement;
  13. import java.sql.ResultSet;
  14. import java.sql.SQLException;
  15. public class GbookController {
  16. public TableView GtableView;
  17. public TableColumn Gname;
  18. public TableColumn Gwriter;
  19. public TableColumn Gbank;
  20. public TableColumn Gtype;
  21. public TableColumn UserName;
  22. private static String n;
  23. public static String w;
  24. public static String t;
  25. public static String b;
  26. public void initialize() throws SQLException {
  27. loadData();
  28. }
  29. public void loadData() throws SQLException {
  30. String sql = "select * from book_list as b left join book_type as bt on b.type=bt.book_type";
  31. Connection conn = JDBC.getConn();
  32. PreparedStatement ps = conn.prepareStatement(sql);
  33. ResultSet resultSet = ps.executeQuery();
  34. ObservableList<GBook> gBookList = FXCollections.observableArrayList();
  35. while (resultSet.next()) {
  36. String gBookName=resultSet.getString("name");
  37. String gBookWriter=resultSet.getString("writer");
  38. String gBookBank = resultSet.getString("bank");
  39. int gBookId= resultSet.getInt("id");
  40. String gBookUserName=resultSet.getString("username");
  41. String gBookType= resultSet.getString("type_name");
  42. GBook gBook = new GBook(gBookName,gBookWriter,gBookBank,gBookUserName,gBookType,gBookId);
  43. gBookList.add(gBook);
  44. // 设置其他属性
  45. }
  46. Gname.setCellValueFactory(new PropertyValueFactory<>("name"));
  47. Gwriter.setCellValueFactory(new PropertyValueFactory<>("writer"));
  48. Gbank.setCellValueFactory(new PropertyValueFactory<>("bank"));
  49. UserName.setCellValueFactory(new PropertyValueFactory<>("username"));
  50. Gtype.setCellValueFactory(new PropertyValueFactory<>("type"));
  51. GtableView.setItems(gBookList);
  52. }
  53. //删除的方法
  54. public Boolean delete(String writer)throws SQLException {
  55. String sql="delete from library.book_list where writer = ? ";
  56. Connection con = JDBC.getConn();
  57. PreparedStatement ps = con.prepareStatement(sql);
  58. ps.setString(1,writer);
  59. int r = ps.executeUpdate();
  60. if(r != 0){
  61. return true;
  62. }
  63. return false;
  64. }
  65. @FXML
  66. public void back2(MouseEvent mouseEvent){
  67. HelloApplication.loginScene();
  68. }
  69. //点击删除按钮
  70. public void delete(MouseEvent mouseEvent) throws SQLException {
  71. TableView.TableViewSelectionModel<GBook> selectionModel = GtableView.getSelectionModel();
  72. ObservableList<GBook> selectedItems = selectionModel.getSelectedItems();
  73. if(!selectedItems.isEmpty()){
  74. GBook selectedItem = selectedItems.get(0); // 假设只选择了一行
  75. if (selectedItem != null) {
  76. String s = selectedItem.getWriter();
  77. delete(s);
  78. }
  79. ObservableList selectedRows = selectionModel.getSelectedItems();
  80. GtableView.getItems().removeAll(selectedRows);
  81. }else{
  82. System.out.println("请选择一本书");
  83. }
  84. }
  85. //添加图书
  86. @FXML
  87. public void AddBook(MouseEvent mouseEvent) throws SQLException {
  88. HelloApplication.addBook();
  89. }
  90. //管理员看到图书页面的修改按钮
  91. @FXML
  92. public void alter(MouseEvent mouseEvent){
  93. TableView.TableViewSelectionModel<GBook> selectionModel = GtableView.getSelectionModel();
  94. ObservableList<GBook> selectedItems = selectionModel.getSelectedItems();
  95. if(!selectedItems.isEmpty()){
  96. GBook selectedItem = selectedItems.get(0);
  97. if (selectedItem != null) {
  98. int i = selectedItem.getId();
  99. n = selectedItem.getName();
  100. w = selectedItem.getWriter();
  101. t = selectedItem.getType();
  102. b = selectedItem.getBank();
  103. AlterController alterController=new AlterController();
  104. alterController.setValue(n,w,t,b,i);
  105. HelloApplication.AlterScene();
  106. }
  107. }else{
  108. System.out.println("请选择一本书");
  109. }
  110. }
  111. //点击查看书籍详情
  112. public void look(MouseEvent mouseEvent) {
  113. TableView.TableViewSelectionModel<GBook> selectionModel = GtableView.getSelectionModel();
  114. ObservableList<GBook> selectedItems = selectionModel.getSelectedItems();
  115. if (!selectedItems.isEmpty()) {
  116. GBook selectedItem = selectedItems.get(0);
  117. if (selectedItem != null) {
  118. String s = selectedItem.getName();
  119. InbookController inbookController = new InbookController();
  120. inbookController.setValue(s);
  121. HelloApplication.inBook();
  122. }
  123. } else {
  124. System.out.println("No item is selected.");
  125. }
  126. }
  127. }
增加图书页面控制类
  1. package com.example.controller;
  2. import com.example.dao.GBook;
  3. import javafx.fxml.FXML;
  4. import javafx.scene.control.ComboBox;
  5. import javafx.scene.control.TextField;
  6. import javafx.scene.input.MouseEvent;
  7. import java.sql.SQLException;
  8. public class AddbookController {
  9. @FXML
  10. private TextField Aname;
  11. @FXML
  12. private TextField Awriter;
  13. @FXML
  14. private TextField Atype;
  15. @FXML
  16. public void addwin(MouseEvent mouseEvent) throws SQLException {
  17. String n = Aname.getText();
  18. String w = Awriter.getText();
  19. String t = Atype.getText();
  20. Boolean ok = GBook.addBook(n,w,t);
  21. if(ok){
  22. HelloApplication.Addwin();
  23. }
  24. }
  25. @FXML
  26. public void back3(MouseEvent mouseEvent){
  27. HelloApplication.BackScene();
  28. }
  29. }
修改图书控制类
  1. package com.example.controller;
  2. import com.example.dao.JDBC;
  3. import javafx.scene.control.TextField;
  4. import javafx.scene.input.MouseEvent;
  5. import java.sql.Connection;
  6. import java.sql.PreparedStatement;
  7. import java.sql.ResultSet;
  8. import java.sql.SQLException;
  9. public class AlterController {
  10. public TextField Xname;
  11. public TextField Xwriter;
  12. public TextField Xtype;
  13. public TextField Xbank;
  14. public static String u;
  15. public static String r;
  16. public static String y;
  17. public static int i;
  18. public static String s;
  19. public static String m;
  20. public void initialize() throws SQLException {
  21. setValue(u,r,y,s,i);
  22. Xname.setText(u);
  23. Xwriter.setText(r);
  24. Xtype.setText(y);
  25. Xbank.setText(s);
  26. }
  27. public void setValue(String a,String b,String c,String e,int d){
  28. u=a;
  29. r=b;
  30. y=c;
  31. s=e;
  32. i=d;
  33. }
  34. public boolean alter(String XName,String XWriter,String XType,String Xbank) throws SQLException {
  35. String sql1="select book_type from library.book_type where type_name = ?";
  36. String sql = "update library.book_list set name=?,writer=? ,type=?,bank=? where id= ? ";
  37. Connection con= JDBC.getConn();
  38. PreparedStatement pg=con.prepareStatement(sql1);
  39. PreparedStatement ps = con.prepareStatement(sql);
  40. pg.setString(1,XType);
  41. ResultSet re = pg.executeQuery();
  42. while (re.next()){
  43. m= re.getString("book_type");
  44. }
  45. ps.setString(1,XName);
  46. ps.setString(2,XWriter);
  47. ps.setString(3,m);
  48. ps.setString(4,Xbank);
  49. ps.setInt(5,i);
  50. int rs = ps.executeUpdate();
  51. if(rs ==0){
  52. return false;
  53. }
  54. return true;
  55. }
  56. public void finish(MouseEvent mouseEvent) throws SQLException {
  57. String m = Xname.getText();
  58. String r = Xwriter.getText();
  59. String p = Xtype.getText();
  60. String q = Xbank.getText();
  61. boolean ok = alter(m,r,p,q);
  62. if(ok) {
  63. HelloApplication.BackScene();
  64. }
  65. else{
  66. HelloApplication.last();
  67. }
  68. }
  69. public void back8(MouseEvent mouseEvent) {
  70. HelloApplication.BackScene();
  71. }
  72. }
管理员查看图书借阅记录控制类
  1. package com.example.controller;
  2. import com.example.dao.BookName;
  3. import com.example.dao.JDBC;
  4. import javafx.collections.FXCollections;
  5. import javafx.collections.ObservableList;
  6. import javafx.fxml.FXML;
  7. import javafx.scene.control.TableColumn;
  8. import javafx.scene.control.TableView;
  9. import javafx.scene.control.cell.PropertyValueFactory;
  10. import javafx.scene.input.MouseEvent;
  11. import java.sql.Connection;
  12. import java.sql.PreparedStatement;
  13. import java.sql.ResultSet;
  14. import java.sql.SQLException;
  15. public class InbookController {
  16. public static String r;
  17. @FXML
  18. private TableView BtableView;
  19. @FXML
  20. private TableColumn Bookname;
  21. @FXML
  22. private TableColumn Bname;
  23. public void setValue(String s){
  24. r=s;
  25. }
  26. public void initialize() throws SQLException {
  27. loadData();
  28. }
  29. public void loadData() throws SQLException {
  30. String sql = "select BookName,name from library.book_name where BookName = ?";
  31. Connection conn = JDBC.getConn();
  32. PreparedStatement ps = conn.prepareStatement(sql);
  33. ps.setString(1,r);
  34. ResultSet resultSet = ps.executeQuery();
  35. ObservableList<BookName> BookList = FXCollections.observableArrayList();
  36. while (resultSet.next()) {
  37. String gBookName=resultSet.getString("BookName");
  38. String gBookUserName=resultSet.getString("name");
  39. BookName Book = new BookName(gBookName,gBookUserName);
  40. BookList.add(Book);
  41. // 设置其他属性
  42. }
  43. Bookname.setCellValueFactory(new PropertyValueFactory<>("name"));
  44. Bname.setCellValueFactory(new PropertyValueFactory<>("username"));
  45. BtableView.setItems(BookList);
  46. }
  47. public void back11(MouseEvent mouseEvent) {
  48. HelloApplication.BackScene();
  49. }
  50. }

实体类

图书馆
  1. package com.example.dao;
  2. import javafx.beans.property.SimpleIntegerProperty;
  3. import javafx.beans.property.SimpleStringProperty;
  4. public class SBook {
  5. private final SimpleStringProperty name;
  6. private final SimpleStringProperty writer;
  7. private final SimpleIntegerProperty bank;
  8. public SBook(String name, String writer, int bank) {
  9. this.name = new SimpleStringProperty(name);
  10. this.writer= new SimpleStringProperty(writer);
  11. this.bank= new SimpleIntegerProperty(bank);
  12. }
  13. public String getName() {
  14. return name.get();
  15. }
  16. public SimpleStringProperty nameProperty() {
  17. return name;
  18. }
  19. public void setName(String name) {
  20. this.name.set(name);
  21. }
  22. public String getWriter(){
  23. return writer.get();
  24. }
  25. public SimpleStringProperty writerProperty() {
  26. return writer;
  27. }
  28. public void setWriter(String writer) {
  29. this.writer.set(writer);
  30. }
  31. public SimpleIntegerProperty bankProperty() {
  32. return bank;
  33. }
  34. public void setBank(int bank) {
  35. this.bank.set(bank);
  36. }
  37. public int getBank(){
  38. return bank.get();
  39. }
  40. }
管理员后台类
  1. package com.example.dao;
  2. import javafx.beans.property.SimpleIntegerProperty;
  3. import javafx.beans.property.SimpleStringProperty;
  4. import java.sql.Connection;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. public class GBook {
  9. private final SimpleStringProperty name;
  10. private final SimpleStringProperty writer;
  11. private final SimpleStringProperty username;
  12. private final SimpleStringProperty bank;
  13. private final SimpleStringProperty type;
  14. private final SimpleIntegerProperty id;
  15. public GBook(String name, String writer, String bank,String username ,String type,int id) {
  16. this.name = new SimpleStringProperty(name);
  17. this.writer= new SimpleStringProperty(writer);
  18. this.bank= new SimpleStringProperty(bank);
  19. this.id= new SimpleIntegerProperty(id);
  20. this.username= new SimpleStringProperty(username);
  21. this.type= new SimpleStringProperty(type);
  22. }
  23. public String getName() {
  24. return name.get();
  25. }
  26. public SimpleStringProperty nameProperty() {
  27. return name;
  28. }
  29. public void setName(String name) {
  30. this.name.set(name);
  31. }
  32. public String getWriter(){
  33. return writer.get();
  34. }
  35. public SimpleStringProperty writerProperty() {
  36. return writer;
  37. }
  38. public void setWriter(String writer) {
  39. this.writer.set(writer);
  40. }
  41. public SimpleStringProperty bankProperty() {
  42. return bank;
  43. }
  44. public void setBank(String bank) {
  45. this.bank.set(bank);
  46. }
  47. public String getBank(){
  48. return bank.get();
  49. }
  50. public String getUsername(){
  51. return username.get();
  52. }
  53. public SimpleStringProperty usernameProperty() {
  54. return username;
  55. }
  56. public void setUsername(String username) {
  57. this.username.set(username);
  58. }
  59. public SimpleStringProperty typeProperty() {
  60. return type;
  61. }
  62. public SimpleIntegerProperty idProperty() {
  63. return id;
  64. }
  65. public void setId(int id) {
  66. this.id.set(id);
  67. }
  68. public int getId(){
  69. return id.get();
  70. }
  71. public void setType(String type) {
  72. this.type.set(type);
  73. }
  74. public String getType(){
  75. return type.get();
  76. }
  77. public static boolean addBook(String Aname,String Awriter,String Atype) throws SQLException {
  78. String sql1="select name,writer from library.book_list where name =? and writer = ?";
  79. String sql = "insert into library.book_list (name,writer,bank,type) values(?,?,1,?)";
  80. Connection conn = JDBC.getConn();
  81. PreparedStatement pg = conn.prepareStatement(sql1);
  82. PreparedStatement gp = conn.prepareStatement(sql);
  83. pg.setString(1,Aname);
  84. pg.setString(2,Awriter);
  85. gp.setString(1,Aname);
  86. gp.setString(2,Awriter);
  87. gp.setString(3,Atype);
  88. ResultSet re=pg.executeQuery();
  89. int up=gp.executeUpdate();
  90. if(up ==0 && re.next()){
  91. System.out.println("增加失败");
  92. return false;
  93. }
  94. return true;
  95. }
  96. }
type对应type-name类
  1. package com.example.dao;
  2. import javafx.beans.property.SimpleStringProperty;
  3. public class BookName{
  4. private final SimpleStringProperty name;
  5. private final SimpleStringProperty username;
  6. public BookName(String name, String username) {
  7. this.name = new SimpleStringProperty(name);
  8. this.username = new SimpleStringProperty(username);
  9. }
  10. public String getName() {
  11. return name.get();
  12. }
  13. public SimpleStringProperty nameProperty() {
  14. return name;
  15. }
  16. public void setName(String name) {
  17. this.name.set(name);
  18. }
  19. public String getUsername(){
  20. return username.get();
  21. }
  22. public SimpleStringProperty usernameProperty() {
  23. return username;
  24. }
  25. public void setUsername(String username) {
  26. this.username.set(username);
  27. }
  28. }
JDBC封装以及一个带有sql语句的方法的类
SQL语句方法类
  1. package com.example.dao;
  2. import java.sql.Connection;
  3. import java.sql.PreparedStatement;
  4. import java.sql.ResultSet;
  5. import java.sql.SQLException;
  6. import com.example.dao.JDBC;
  7. import static com.example.dao.JDBC.getConn;
  8. public class Student {
  9. // login 学生登陆
  10. public static boolean login(String username ,String password) throws SQLException {
  11. String sql = "select id from library.user where name = ? and password = ? and type=0";
  12. Connection conn = getConn();
  13. PreparedStatement ps = conn.prepareStatement(sql);
  14. ps.setString(1, username);
  15. ps.setString(2, password);
  16. ResultSet res = ps.executeQuery();
  17. while (res.next()) {
  18. return true;
  19. }
  20. return false;
  21. }
  22. public static boolean glogin(String usernames, String passwords) throws SQLException {
  23. String sql = "select id from library.user where name = ? and password = ? and type=1";
  24. Connection conn = getConn();
  25. PreparedStatement gp = conn.prepareStatement(sql);
  26. gp.setString(1, usernames);
  27. gp.setString(2, passwords);
  28. ResultSet res = gp.executeQuery();
  29. while (res.next()) {
  30. return true;
  31. }
  32. return false;
  33. }
  34. public static boolean slogin(String name, String npassword) throws SQLException {
  35. String sql1 = "select id from library.user where name = ? and password = ? and type=0";
  36. String sql = "insert into library.user (name,password,type) values(?,?,0)";
  37. Connection conn = getConn();
  38. PreparedStatement pg =conn.prepareStatement(sql1);
  39. PreparedStatement gp = conn.prepareStatement(sql);
  40. pg.setString(1,name);
  41. pg.setString(2,npassword);
  42. gp.setString(1, name);
  43. gp.setString(2, npassword);
  44. ResultSet re = pg.executeQuery();
  45. int up = gp.executeUpdate();
  46. while (up ==0 || re.next()){
  47. return false;
  48. }
  49. return true;
  50. }
  51. }
JDBC封装类
  1. package com.example.dao;
  2. import java.sql.*;
  3. import java.util.Objects;
  4. import java.sql.Connection;
  5. import java.sql.DriverManager;
  6. import java.sql.SQLException;
  7. public class JDBC {
  8. /**
  9. * URL地址
  10. */
  11. private static final String URL = "jdbc:mysql://127.0.0.1:3306/library?useSSL=false";
  12. /**
  13. * 登录数据库服务器的账号
  14. */
  15. private static final String USER = "root";
  16. /**
  17. * 登录数据库服务器的密码
  18. */
  19. private static final String PASSWORD = "*******";//换为自己的数据库密码
  20. /**
  21. * 返回数据库连接对象
  22. *
  23. * @return
  24. */
  25. public static Connection getConn() {
  26. try {
  27. return DriverManager.getConnection(URL, USER, PASSWORD);
  28. } catch (SQLException e) {
  29. e.printStackTrace();
  30. }
  31. return null;
  32. }
  33. /**
  34. * 关闭资源
  35. *
  36. * @param rs
  37. * @param stat
  38. * @param conn
  39. */
  40. public static void close(ResultSet rs, Statement stat, Connection conn) {
  41. try {
  42. if (rs != null) {
  43. rs.close();
  44. }
  45. if (stat != null) {
  46. stat.close();
  47. }
  48. if (conn != null) {
  49. conn.close();
  50. }
  51. } catch (SQLException e) {
  52. e.printStackTrace();
  53. }
  54. }
  55. /**
  56. * 封装通用的更新操作(即通过该方法实现对弈任意数据表的insert,update,delete操作)
  57. *
  58. * @param sql 需要执行的SQL语句
  59. * @param params 执行SQL语句时需要传递进去参数
  60. * @return 执行结果
  61. */
  62. public static boolean exeUpdate(String sql, Object... params) {
  63. //获取连接对象
  64. Connection conn = getConn();
  65. PreparedStatement ps = null;
  66. try {
  67. //获取预编译对象
  68. ps = conn.prepareStatement(sql);
  69. //执行参数赋值操作
  70. if (Objects.nonNull(params)) {
  71. //循环将所有的参数赋值
  72. for (int i = 0; i < params.length; i++) {
  73. ps.setObject(i + 1, params[i]);
  74. }
  75. }
  76. //执行更新
  77. return ps.executeUpdate() > 0;
  78. } catch (SQLException e) {
  79. e.printStackTrace();
  80. } finally {
  81. //关闭资源
  82. close(null, ps, conn);
  83. }
  84. return false;
  85. }
  86. }
FXML文件
登录
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <?import javafx.scene.control.*?>
  3. <?import javafx.scene.image.*?>
  4. <?import javafx.scene.layout.*?>
  5. <?import javafx.scene.text.*?>
  6. <AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="468.0" prefWidth="705.0" xmlns="http://javafx.com/javafx/11.0.14-internal" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.example.controller.HelloController">
  7. <children>
  8. <ImageView fitHeight="468.0" fitWidth="723.0" layoutY="2.0" pickOnBounds="true" preserveRatio="true">
  9. <image>
  10. <Image url="file:/C:/Users/Lenovo/Desktop/sea.jpg" />
  11. </image>
  12. </ImageView>
  13. <Button fx:id="login" layoutX="156.0" layoutY="361.0" mnemonicParsing="false" onMouseClicked="#Booklist" prefHeight="27.0" prefWidth="90.0" text="login" textFill="#691818" />
  14. <Label contentDisplay="CENTER" layoutX="246.0" layoutY="74.0" prefHeight="52.0" prefWidth="115.0" text="Welcome">
  15. <font>
  16. <Font size="23.0" />
  17. </font>
  18. </Label>
  19. <Label layoutX="130.0" layoutY="189.0" prefHeight="27.0" prefWidth="115.0" text="Student ID:">
  20. <font>
  21. <Font size="20.0" />
  22. </font></Label>
  23. <Label layoutX="100.0" layoutY="242.0" />
  24. <Label layoutX="134.0" layoutY="280.0" prefHeight="27.0" prefWidth="108.0" text="Password:">
  25. <font>
  26. <Font size="20.0" />
  27. </font></Label>
  28. <PasswordField fx:id="password" layoutX="345.0" layoutY="280.0" prefHeight="27.0" prefWidth="161.0" promptText="请输入密码" />
  29. <Button layoutX="573.0" layoutY="74.0" mnemonicParsing="false" onMouseClicked="#ButtonClick" prefHeight="35.0" prefWidth="34.0" style="-fx-background-color: powderblue;" text="管" textFill="#691818" textOverrun="CLIP" />
  30. <TextField fx:id="username" layoutX="345.0" layoutY="189.0" prefHeight="27.0" prefWidth="161.0" promptText="请输入账号" />
  31. <Button layoutX="426.0" layoutY="361.0" mnemonicParsing="false" onMouseClicked="#register" prefHeight="27.0" prefWidth="90.0" text="sign in" />
  32. </children>
  33. </AnchorPane>
登陆失败
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <?import javafx.scene.control.*?>
  3. <?import javafx.scene.image.*?>
  4. <?import javafx.scene.layout.*?>
  5. <?import javafx.scene.text.*?>
  6. <AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="115.0" prefWidth="463.0" xmlns="http://javafx.com/javafx/11.0.14-internal" xmlns:fx="http://javafx.com/fxml/1">
  7. <children>
  8. <ImageView fitHeight="540.0" fitWidth="744.0" layoutX="-230.0" layoutY="-141.0" pickOnBounds="true" preserveRatio="true">
  9. <image>
  10. <Image url="file:/C:/Users/Lenovo/Desktop/sea.jpg" />
  11. </image>
  12. </ImageView>
  13. <Label layoutX="99.0" layoutY="42.0" prefHeight="44.0" prefWidth="467.0" text="请输入正确信息">
  14. <font>
  15. <Font size="34.0" />
  16. </font></Label>
  17. </children>
  18. </AnchorPane>
注册
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <?import javafx.scene.control.*?>
  3. <?import javafx.scene.image.*?>
  4. <?import javafx.scene.layout.*?>
  5. <?import javafx.scene.text.*?>
  6. <AnchorPane prefHeight="468.0" prefWidth="705.0" xmlns="http://javafx.com/javafx/11.0.14-internal" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.example.controller.HelloController">
  7. <children>
  8. <ImageView fitHeight="475.0" fitWidth="737.0" layoutY="-4.0" pickOnBounds="true" preserveRatio="true">
  9. <image>
  10. <Image url="file:/C:/Users/Lenovo/Desktop/sea.jpg" />
  11. </image></ImageView>
  12. <Button layoutX="321.0" layoutY="382.0" mnemonicParsing="false" onMouseClicked="#regis" prefHeight="27.0" prefWidth="70.0" text="注册" />
  13. <Label layoutX="179.0" layoutY="156.0" text="姓名:">
  14. <font>
  15. <Font size="20.0" />
  16. </font></Label>
  17. <Label layoutX="178.0" layoutY="232.0" text="密码:">
  18. <font>
  19. <Font size="20.0" />
  20. </font></Label>
  21. <Label layoutX="152.0" layoutY="293.0" text="确认密码:">
  22. <font>
  23. <Font size="20.0" />
  24. </font>
  25. </Label>
  26. <PasswordField fx:id="npassword" layoutX="350.0" layoutY="234.0" />
  27. <PasswordField fx:id="apassword" layoutX="350.0" layoutY="295.0" />
  28. <TextField fx:id="Name" layoutX="350.0" layoutY="170.0" />
  29. <Button layoutX="523.0" layoutY="106.0" mnemonicParsing="false" onMouseClicked="#back10" prefHeight="23.0" prefWidth="30.0" text="X" />
  30. </children>
  31. </AnchorPane>
注册失败
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <?import javafx.scene.control.*?>
  3. <?import javafx.scene.image.*?>
  4. <?import javafx.scene.layout.*?>
  5. <?import javafx.scene.text.*?>
  6. <AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="115.0" prefWidth="463.0" xmlns="http://javafx.com/javafx/11.0.14-internal" xmlns:fx="http://javafx.com/fxml/1">
  7. <children>
  8. <ImageView fitHeight="540.0" fitWidth="744.0" layoutX="-230.0" layoutY="-141.0" pickOnBounds="true" preserveRatio="true">
  9. <image>
  10. <Image url="file:/C:/Users/Lenovo/Desktop/sea.jpg" />
  11. </image>
  12. </ImageView>
  13. <Label layoutX="99.0" layoutY="42.0" prefHeight="44.0" prefWidth="467.0" text="用户已存在">
  14. <font>
  15. <Font size="34.0" />
  16. </font></Label>
  17. </children>
  18. </AnchorPane>
管理员登陆
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <?import javafx.scene.control.*?>
  3. <?import javafx.scene.image.*?>
  4. <?import javafx.scene.layout.*?>
  5. <?import javafx.scene.text.*?>
  6. <Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="468.0" prefWidth="705.0" xmlns="http://javafx.com/javafx/11.0.14-internal" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.example.controller.HelloController">
  7. <children>
  8. <ImageView fitHeight="476.0" fitWidth="721.0" pickOnBounds="true" preserveRatio="true">
  9. <image>
  10. <Image url="file:/C:/Users/Lenovo/Desktop/sea.jpg" />
  11. </image></ImageView>
  12. <Label contentDisplay="CENTER" layoutX="196.0" layoutY="167.0" prefHeight="30.0" prefWidth="69.0" text="账号:">
  13. <font>
  14. <Font size="23.0" />
  15. </font>
  16. </Label>
  17. <Label layoutX="196.0" layoutY="246.0" text="密码:">
  18. <font>
  19. <Font size="23.0" />
  20. </font>
  21. </Label>
  22. <TextField fx:id="usernames" layoutX="353.0" layoutY="166.0" prefHeight="32.0" prefWidth="183.0" />
  23. <PasswordField fx:id="passwords" layoutX="353.0" layoutY="245.0" prefHeight="32.0" prefWidth="183.0" />
  24. <Label layoutX="277.0" layoutY="105.0" prefHeight="32.0" prefWidth="123.0" text="管理员你好">
  25. <font>
  26. <Font size="24.0" />
  27. </font>
  28. </Label>
  29. <Button fx:id="glogin" layoutX="302.0" layoutY="314.0" mnemonicParsing="false" onMouseClicked="#Glogin" prefHeight="32.0" prefWidth="73.0" text="登陆" />
  30. <Button layoutX="583.0" layoutY="30.0" mnemonicParsing="false" onMouseClicked="#back1" prefHeight="32.0" prefWidth="31.0" style="-fx-background-color: powderblue;" text="X" textFill="#691818" />
  31. </children>
  32. </Pane>
图书馆
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <?import javafx.scene.control.*?>
  3. <?import javafx.scene.image.*?>
  4. <?import javafx.scene.layout.*?>
  5. <AnchorPane prefHeight="428.0" prefWidth="693.0" xmlns="http://javafx.com/javafx/11.0.14-internal" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.example.controller.TypeController">
  6. <children>
  7. <ImageView fitHeight="499.0" fitWidth="749.0" layoutX="-13.0" layoutY="-35.0" pickOnBounds="true" preserveRatio="true">
  8. <image>
  9. <Image url="file:/C:/Users/Lenovo/Desktop/sea.jpg" />
  10. </image></ImageView>
  11. <Button layoutX="26.0" layoutY="57.0" mnemonicParsing="false" onMouseClicked="#type1" prefHeight="37.0" prefWidth="66.0" text="历史小说" />
  12. <Button layoutX="26.0" layoutY="195.0" mnemonicParsing="false" onMouseClicked="#type3" prefHeight="37.0" prefWidth="66.0" text="霸道总裁" />
  13. <Button layoutX="26.0" layoutY="267.0" mnemonicParsing="false" onMouseClicked="#type4" prefHeight="37.0" prefWidth="66.0" text="悬疑探案" />
  14. <Button layoutX="26.0" layoutY="124.0" mnemonicParsing="false" onMouseClicked="#type2" prefHeight="37.0" prefWidth="66.0" text="仙侠玄幻" />
  15. <Button layoutX="24.0" layoutY="339.0" mnemonicParsing="false" onMouseClicked="#comein" prefHeight="37.0" prefWidth="66.0" text="全部" />
  16. <Button layoutX="507.0" layoutY="29.0" mnemonicParsing="false" onMouseClicked="#select" prefHeight="37.0" prefWidth="66.0" text="搜索" />
  17. <Button layoutX="201.0" layoutY="29.0" mnemonicParsing="false" onMouseClicked="#back7" prefHeight="37.0" prefWidth="66.0" text="还书" />
  18. <Button layoutX="587.0" layoutY="2.0" mnemonicParsing="false" onMouseClicked="#back9" prefHeight="23.0" prefWidth="35.0" text="X" />
  19. <TableView fx:id="tableView" layoutX="165.0" layoutY="85.0" prefHeight="311.0" prefWidth="470.0">
  20. <columns>
  21. <TableColumn fx:id="name" prefWidth="203.99996948242188" text="书籍名" />
  22. <TableColumn fx:id="writer" prefWidth="187.3333740234375" text="作者" />
  23. <TableColumn fx:id="bank" prefWidth="78.0" text="库存" />
  24. </columns>
  25. </TableView>
  26. <Button layoutX="367.0" layoutY="412.0" mnemonicParsing="false" onMouseClicked="#OK" prefHeight="37.0" prefWidth="66.0" text="选好了" />
  27. </children>
  28. </AnchorPane>
搜索
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <?import javafx.scene.control.*?>
  3. <?import javafx.scene.image.*?>
  4. <?import javafx.scene.layout.*?>
  5. <?import javafx.scene.text.*?>
  6. <Pane prefHeight="656.0" prefWidth="628.0" xmlns="http://javafx.com/javafx/11.0.14-internal" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.example.controller.SelectController">
  7. <children>
  8. <ImageView fitHeight="669.0" fitWidth="993.0" layoutX="-206.0" layoutY="-7.0" pickOnBounds="true" preserveRatio="true">
  9. <image>
  10. <Image url="file:/C:/Users/Lenovo/Desktop/sea.jpg" />
  11. </image></ImageView>
  12. <TableView fx:id="tableView" layoutX="137.0" layoutY="105.0" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="371.0" prefWidth="409.0">
  13. <columns>
  14. <TableColumn fx:id="name" prefWidth="145.3333282470703" text="书籍名" />
  15. <TableColumn fx:id="writer" prefWidth="126.0" text="作者" />
  16. <TableColumn fx:id="bank" prefWidth="136.0" text="库存" />
  17. </columns>
  18. </TableView>
  19. <Label layoutX="266.0" layoutY="14.0" prefHeight="33.0" prefWidth="184.0" text="欢迎来到网上图书馆">
  20. <font>
  21. <Font size="20.0" />
  22. </font>
  23. </Label>
  24. <Button layoutX="504.0" layoutY="19.0" mnemonicParsing="false" onMouseClicked="#back7" text="返回" />
  25. <Button layoutX="316.0" layoutY="491.0" mnemonicParsing="false" onMouseClicked="#last" text="选好了" />
  26. <TextField fx:id="bookname" layoutX="191.0" layoutY="61.0" prefHeight="23.0" prefWidth="124.0" />
  27. <TextField fx:id="bookwriter" layoutX="388.0" layoutY="62.0" prefHeight="23.0" prefWidth="124.0" />
  28. <Label layoutX="130.0" layoutY="61.0" prefHeight="24.0" prefWidth="61.0" text="书名:">
  29. <font>
  30. <Font size="18.0" />
  31. </font></Label>
  32. <Label layoutX="331.0" layoutY="61.0" text="作者:">
  33. <font>
  34. <Font size="18.0" />
  35. </font></Label>
  36. <Button layoutX="524.0" layoutY="62.0" mnemonicParsing="false" onMouseClicked="#sou" prefHeight="23.0" prefWidth="28.0" text="搜" />
  37. </children>
  38. </Pane>
借书成功
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <?import javafx.scene.control.*?>
  3. <?import javafx.scene.image.*?>
  4. <?import javafx.scene.layout.*?>
  5. <?import javafx.scene.text.*?>
  6. <AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="115.0" prefWidth="463.0" xmlns="http://javafx.com/javafx/11.0.14-internal" xmlns:fx="http://javafx.com/fxml/1">
  7. <children>
  8. <ImageView fitHeight="540.0" fitWidth="744.0" layoutX="-230.0" layoutY="-141.0" pickOnBounds="true" preserveRatio="true">
  9. <image>
  10. <Image url="file:/C:/Users/Lenovo/Desktop/sea.jpg" />
  11. </image>
  12. </ImageView>
  13. <Label layoutX="99.0" layoutY="42.0" prefHeight="44.0" prefWidth="467.0" text="借书成功">
  14. <font>
  15. <Font size="34.0" />
  16. </font></Label>
  17. </children>
  18. </AnchorPane>
借书失败
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <?import javafx.scene.control.*?>
  3. <?import javafx.scene.image.*?>
  4. <?import javafx.scene.layout.*?>
  5. <?import javafx.scene.text.*?>
  6. <AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="115.0" prefWidth="463.0" xmlns="http://javafx.com/javafx/11.0.14-internal" xmlns:fx="http://javafx.com/fxml/1">
  7. <children>
  8. <ImageView fitHeight="433.0" fitWidth="744.0" layoutX="-170.0" layoutY="-101.0" pickOnBounds="true" preserveRatio="true">
  9. <image>
  10. <Image url="file:/C:/Users/Lenovo/Desktop/sea.jpg" />
  11. </image>
  12. </ImageView>
  13. <Label layoutX="24.0" layoutY="14.0" prefHeight="44.0" prefWidth="467.0" text="1.请先还书">
  14. <font>
  15. <Font size="34.0" />
  16. </font></Label>
  17. <Label layoutX="24.0" layoutY="71.0" prefHeight="44.0" prefWidth="393.0" text="2.没有库存请耐心等待">
  18. <font>
  19. <Font size="34.0" />
  20. </font>
  21. </Label>
  22. </children>
  23. </AnchorPane>
还书成功
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <?import javafx.scene.control.*?>
  3. <?import javafx.scene.image.*?>
  4. <?import javafx.scene.layout.*?>
  5. <?import javafx.scene.text.*?>
  6. <AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="115.0" prefWidth="463.0" xmlns="http://javafx.com/javafx/11.0.14-internal" xmlns:fx="http://javafx.com/fxml/1">
  7. <children>
  8. <ImageView fitHeight="540.0" fitWidth="744.0" layoutX="-230.0" layoutY="-141.0" pickOnBounds="true" preserveRatio="true">
  9. <image>
  10. <Image url="file:/C:/Users/Lenovo/Desktop/sea.jpg" />
  11. </image>
  12. </ImageView>
  13. <Label layoutX="99.0" layoutY="42.0" prefHeight="44.0" prefWidth="467.0" text="还书成功">
  14. <font>
  15. <Font size="34.0" />
  16. </font></Label>
  17. </children>
  18. </AnchorPane>
还书失败
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <?import javafx.scene.control.*?>
  3. <?import javafx.scene.image.*?>
  4. <?import javafx.scene.layout.*?>
  5. <?import javafx.scene.text.*?>
  6. <AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="115.0" prefWidth="463.0" xmlns="http://javafx.com/javafx/11.0.14-internal" xmlns:fx="http://javafx.com/fxml/1">
  7. <children>
  8. <ImageView fitHeight="540.0" fitWidth="744.0" layoutX="-230.0" layoutY="-141.0" pickOnBounds="true" preserveRatio="true">
  9. <image>
  10. <Image url="file:/C:/Users/Lenovo/Desktop/sea.jpg" />
  11. </image>
  12. </ImageView>
  13. <Label layoutX="99.0" layoutY="42.0" prefHeight="44.0" prefWidth="467.0" text="还书失败,请先借书">
  14. <font>
  15. <Font size="34.0" />
  16. </font></Label>
  17. </children>
  18. </AnchorPane>
管理员后台
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <?import javafx.scene.control.*?>
  3. <?import javafx.scene.image.*?>
  4. <?import javafx.scene.layout.*?>
  5. <?import javafx.scene.text.*?>
  6. <AnchorPane prefHeight="552.0" prefWidth="697.0" xmlns="http://javafx.com/javafx/11.0.14-internal" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.example.controller.GbookController">
  7. <children>
  8. <ImageView fitHeight="690.0" fitWidth="967.0" pickOnBounds="true" preserveRatio="true">
  9. <image>
  10. <Image url="file:/C:/Users/Lenovo/Desktop/sea.jpg" />
  11. </image>
  12. </ImageView>
  13. <TableView fx:id="GtableView" layoutX="146.0" layoutY="118.0" prefHeight="410.0" prefWidth="398.0">
  14. <columns>
  15. <TableColumn fx:id="Gname" prefWidth="111.0" text="书籍名" />
  16. <TableColumn fx:id="Gwriter" prefWidth="93.66665649414062" text="作者" />
  17. <TableColumn fx:id="UserName" prefWidth="91.00003051757812" text="借阅人" />
  18. <TableColumn fx:id="Gbank" prefWidth="56.0" text="库存" />
  19. <TableColumn fx:id="Gtype" prefWidth="43.6666259765625" text="种类" />
  20. </columns>
  21. </TableView>
  22. <Button layoutX="484.0" layoutY="70.0" mnemonicParsing="false" onMouseClicked="#AddBook" text="增加书籍" />
  23. <Label layoutX="287.0" layoutY="40.0" text="欢迎管理员">
  24. <font>
  25. <Font size="23.0" />
  26. </font>
  27. </Label>
  28. <Button layoutX="510.0" layoutY="28.0" mnemonicParsing="false" onMouseClicked="#back2" prefHeight="23.0" prefWidth="26.0" style="-fx-background-color: powderblue;" text="X" textFill="#691818" />
  29. <Button layoutX="157.0" layoutY="549.0" mnemonicParsing="false" onMouseClicked="#alter" text="修改" />
  30. <Button layoutX="484.0" layoutY="549.0" mnemonicParsing="false" onMouseClicked="#delete" text="删除" />
  31. <Button layoutX="325.0" layoutY="549.0" mnemonicParsing="false" onMouseClicked="#look" prefHeight="16.0" prefWidth="40.0" text="查看" />
  32. </children>
  33. </AnchorPane>
查询借阅记录
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <?import javafx.scene.control.*?>
  3. <?import javafx.scene.image.*?>
  4. <?import javafx.scene.layout.*?>
  5. <AnchorPane prefHeight="444.0" prefWidth="668.0" xmlns="http://javafx.com/javafx/11.0.14-internal" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.example.controller.InbookController">
  6. <children>
  7. <ImageView fitHeight="444.0" fitWidth="679.0" pickOnBounds="true" preserveRatio="true">
  8. <image>
  9. <Image url="file:/C:/Users/Lenovo/Desktop/sea.jpg" />
  10. </image></ImageView>
  11. <TableView fx:id="BtableView" layoutX="140.0" layoutY="103.0" prefHeight="297.0" prefWidth="423.0">
  12. <columns>
  13. <TableColumn fx:id="Bookname" prefWidth="200.6666488647461" text="书名" />
  14. <TableColumn fx:id="Bname" prefWidth="222.00003051757812" text="借阅人" />
  15. </columns>
  16. </TableView>
  17. <Button layoutX="546.0" layoutY="48.0" mnemonicParsing="false" onMouseClicked="#back11" prefHeight="23.0" prefWidth="36.0" text="X" />
  18. </children>
  19. </AnchorPane>
修改图书
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <?import javafx.scene.control.*?>
  3. <?import javafx.scene.image.*?>
  4. <?import javafx.scene.layout.*?>
  5. <?import javafx.scene.text.*?>
  6. <AnchorPane prefHeight="470.0" prefWidth="699.0" xmlns="http://javafx.com/javafx/11.0.14-internal" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.example.controller.AlterController">
  7. <children>
  8. <ImageView fitHeight="477.0" fitWidth="704.0" pickOnBounds="true" preserveRatio="true">
  9. <image>
  10. <Image url="file:/C:/Users/Lenovo/Desktop/sea.jpg" />
  11. </image>
  12. </ImageView>
  13. <Button layoutX="303.0" layoutY="351.0" mnemonicParsing="false" onMouseClicked="#finish" prefHeight="34.0" prefWidth="51.0" text="确定" />
  14. <Label layoutX="240.0" layoutY="113.0" prefHeight="28.0" prefWidth="63.0" text="书名:">
  15. <font>
  16. <Font size="21.0" />
  17. </font></Label>
  18. <Label layoutX="219.0" layoutY="175.0" text="作者名:">
  19. <font>
  20. <Font size="21.0" />
  21. </font></Label>
  22. <TextField fx:id="Xname" layoutX="378.0" layoutY="113.0" prefHeight="28.0" prefWidth="161.0" promptText="请输入正确信息" />
  23. <TextField fx:id="Xwriter" layoutX="378.0" layoutY="175.0" prefHeight="28.0" prefWidth="161.0" promptText="请输入正确信息" />
  24. <Label layoutX="238.0" layoutY="227.0" text="种类:">
  25. <font>
  26. <Font size="21.0" />
  27. </font>
  28. </Label>
  29. <TextField fx:id="Xtype" layoutX="378.0" layoutY="227.0" prefHeight="28.0" prefWidth="161.0" promptText="请输入文字" />
  30. <Button layoutX="568.0" layoutY="51.0" mnemonicParsing="false" onMouseClicked="#back8" prefHeight="23.0" prefWidth="34.0" text="X" />
  31. <Label layoutX="240.0" layoutY="290.0" text="库存:">
  32. <font>
  33. <Font size="21.0" />
  34. </font></Label>
  35. <TextField fx:id="Xbank" layoutX="378.0" layoutY="290.0" prefHeight="28.0" prefWidth="161.0" promptText="请输入数字" />
  36. <Label layoutX="270.0" layoutY="44.0" text="修改图书">
  37. <font>
  38. <Font size="29.0" />
  39. </font>
  40. </Label>
  41. </children>
  42. </AnchorPane>
增加图书
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <?import javafx.scene.control.*?>
  3. <?import javafx.scene.image.*?>
  4. <?import javafx.scene.layout.*?>
  5. <?import javafx.scene.text.*?>
  6. <AnchorPane prefHeight="473.0" prefWidth="730.0" xmlns="http://javafx.com/javafx/11.0.14-internal" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.example.controller.AddbookController">
  7. <children>
  8. <ImageView fitHeight="501.0" fitWidth="739.0" pickOnBounds="true" preserveRatio="true">
  9. <image>
  10. <Image url="file:/C:/Users/Lenovo/Desktop/sea.jpg" />
  11. </image>
  12. </ImageView>
  13. <Label layoutX="296.0" layoutY="93.0" text="添加书籍">
  14. <font>
  15. <Font size="23.0" />
  16. </font>
  17. </Label>
  18. <Label layoutX="178.0" layoutY="170.0" text="书名:">
  19. <font>
  20. <Font size="22.0" />
  21. </font>
  22. </Label>
  23. <Label layoutX="188.0" layoutY="246.0" text="作者:">
  24. <font>
  25. <Font size="22.0" />
  26. </font>
  27. </Label>
  28. <TextField fx:id="Aname" layoutX="362.0" layoutY="177.0" prefHeight="30.0" prefWidth="161.0" />
  29. <TextField fx:id="Awriter" layoutX="362.0" layoutY="246.0" prefHeight="30.0" prefWidth="161.0" />
  30. <Button layoutX="484.0" layoutY="386.0" mnemonicParsing="false" onMouseClicked="#addwin" prefHeight="29.0" prefWidth="51.0" text="OK" />
  31. <Button layoutX="535.0" layoutY="81.0" mnemonicParsing="false" onMouseClicked="#back3" text="返回" />
  32. <Label layoutX="188.0" layoutY="317.0" text="种类:">
  33. <font>
  34. <Font size="22.0" />
  35. </font>
  36. </Label>
  37. <TextField fx:id="Atype" layoutX="362.0" layoutY="313.0" prefHeight="30.0" prefWidth="161.0" />
  38. </children>
  39. </AnchorPane>
增加成功
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <?import javafx.scene.control.*?>
  3. <?import javafx.scene.image.*?>
  4. <?import javafx.scene.layout.*?>
  5. <?import javafx.scene.text.*?>
  6. <AnchorPane prefHeight="412.0" prefWidth="619.0" xmlns="http://javafx.com/javafx/11.0.14-internal" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.example.controller.HelloController">
  7. <children>
  8. <ImageView fitHeight="417.0" fitWidth="714.0" layoutY="-5.0" pickOnBounds="true" preserveRatio="true">
  9. <image>
  10. <Image url="file:/C:/Users/Lenovo/Desktop/sea.jpg" />
  11. </image></ImageView>
  12. <Label layoutX="237.0" layoutY="104.0" text="添加成功">
  13. <font>
  14. <Font size="38.0" />
  15. </font>
  16. </Label>
  17. <Button layoutX="169.0" layoutY="252.0" mnemonicParsing="false" onMouseClicked="#back4" prefHeight="35.0" prefWidth="67.0" text="继续添加" />
  18. <Button layoutX="427.0" layoutY="252.0" mnemonicParsing="false" onMouseClicked="#back5" prefHeight="35.0" prefWidth="67.0" text="返回" />
  19. </children>
  20. </AnchorPane>

OK写完了,里面还有很多小瑕疵,修改图书时不能把图书修改的跟数据库现存的一样,以及修改图书还是要填写数字。欢迎大家来评论区探讨!!

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/神奇cpp/article/detail/773554
推荐阅读
相关标签
  

闽ICP备14008679号