增、删、查都完成了!

This commit is contained in:
2024-06-27 12:41:04 +08:00
parent 366097260b
commit 56e0796852
3 changed files with 188 additions and 4 deletions
+16 -4
View File
@@ -1,12 +1,9 @@
package com.msksbr.MainFrm;
import com.msksbr.MainFrm.MenuItemDiaog.*;
import com.msksbr.MainFrm.MenuItemDiaog.Adder.BookAdder;
import com.msksbr.MainFrm.MenuItemDiaog.Adder.RentAdder;
import com.msksbr.MainFrm.MenuItemDiaog.Adder.StudentAdder;
import com.msksbr.MainFrm.MenuItemDiaog.AuthorDIalog;
import com.msksbr.MainFrm.MenuItemDiaog.MITDialog;
import com.msksbr.MainFrm.MenuItemDiaog.SearchDIalog;
import com.msksbr.MainFrm.MenuItemDiaog.aboutDIalog;
import javax.swing.*;
import java.awt.event.ActionEvent;
@@ -76,8 +73,23 @@ public class MenuBar extends JMenuBar {
// ”删除“菜单
JMenu removeMenu = new JMenu("删除");
JMenuItem bookRemove = new JMenuItem("删除图书信息");
bookRemove.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new Remover("books");
}
});
JMenuItem studentRemove = new JMenuItem("删除学生信息");
studentRemove.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new Remover("students");
}
});
JMenuItem rentRemove = new JMenuItem("删除借阅信息");
rentRemove.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new Remover("rents");
}
});
removeMenu.add(bookRemove);
removeMenu.add(studentRemove);
removeMenu.add(rentRemove);
@@ -0,0 +1,66 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="com.msksbr.MainFrm.MenuItemDiaog.Remover">
<grid id="cbd77" binding="contentPane" layout-manager="GridLayoutManager" row-count="2" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="10" left="10" bottom="10" right="10"/>
<constraints>
<xy x="48" y="54" width="436" height="297"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<grid id="94766" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="1" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<grid id="9538f" layout-manager="FlowLayout" hgap="5" vgap="5" flow-align="1">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="e7465" class="javax.swing.JButton" binding="buttonOK">
<constraints/>
<properties>
<text value="删除"/>
</properties>
</component>
</children>
</grid>
</children>
</grid>
<grid id="e3588" layout-manager="BorderLayout" hgap="0" vgap="0">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="1b7c" class="javax.swing.JLabel" binding="idLable">
<constraints border-constraint="West"/>
<properties>
<text value=""/>
</properties>
</component>
<grid id="8147a" layout-manager="FlowLayout" hgap="5" vgap="5" flow-align="0">
<constraints border-constraint="Center"/>
<properties/>
<border type="none"/>
<children>
<component id="19c5" class="javax.swing.JTextField" binding="idField">
<constraints/>
<properties>
<columns value="15"/>
</properties>
</component>
</children>
</grid>
</children>
</grid>
</children>
</grid>
</form>
@@ -0,0 +1,106 @@
package com.msksbr.MainFrm.MenuItemDiaog;
import com.msksbr.LoginFrm.ScreenSize;
import com.msksbr.SQL.Connector;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.sql.SQLException;
public class Remover extends JDialog {
private JPanel contentPane;
private JButton buttonOK;
private JLabel idLable;
private JTextField idField;
private String table;
private String Searchsql;
private String searchSQL;
private String deleteSQL;
private String searchErrMsg;
private int id;
private Connector searchConnector;
private Connector deleteConnector;
public Remover(String table) {
try {
searchConnector = new Connector();
deleteConnector = new Connector();
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
} catch (SQLException e) {
throw new RuntimeException(e);
}
buttonOK.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
onOK();
}
});
this.table = table;
if (table.equals("books")) {
setTitle("删除图书信息");
idLable.setText("请输入书号:");
searchErrMsg = "未找到该图书信息";
} else if (table.equals("students")) {
setTitle("删除学生信息");
idLable.setText("请输入学号:");
searchErrMsg = "未找到该学生信息";
} else if (table.equals("rents")) {
setTitle("删除借阅信息");
idLable.setText("请输入书号:");
searchErrMsg = "未找到该借阅信息";
}
setContentPane(contentPane);
setModal(true);
getRootPane().setDefaultButton(buttonOK);
pack();
setResizable(false);
setLocation();
setVisible(true);
}
private void onOK() {
// 在此处添加您的代码
try {
this.id = Integer.valueOf(idField.getText());
searchSQLMaker();
deleteSQLMaker();
ResultSet rs = searchConnector.executeQuery(searchSQL);
if (rs.next()) {
deleteConnector.executeUpdate(deleteSQL);
} else {
JOptionPane.showMessageDialog(this, searchErrMsg, "错误", JOptionPane.WARNING_MESSAGE);
}
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(this, "请输入一个整数", "错误", JOptionPane.ERROR_MESSAGE);
} catch (SQLException e) {
throw new RuntimeException(e);
}
dispose();
}
private void searchSQLMaker() {
if (table.equals("books")) {
searchSQL = "select * from " + table + " where book_id=" + id + ";";
} else if (table.equals("students")) {
searchSQL = "select * from " + table + " where student_id=" + id + ";";
} else if (table.equals("rents")) {
searchSQL = "select * from " + table + " where book_id=" + id + ";";
}
}
private void deleteSQLMaker() {
if (table.equals("books")) {
deleteSQL = "delete from " + table + " where book_id=" + id + ";";
} else if (table.equals("students")) {
deleteSQL = "delete from " + table + " where student_id=" + id + ";";
} else if (table.equals("rents")) {
deleteSQL = "UPDATE books SET borrowed_by = null WHERE book_id=" + id + ";";
}
}
public void setLocation() {
setLocation(new ScreenSize().width / 2 - getWidth() / 2, new ScreenSize().height / 2 - getHeight() / 2);
}
}