📄 deleteitemfromfirstcatalog.java
字号:
package file2;
import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
public class DeleteItemFromFirstCatalog extends JPanel implements ActionListener{
private DBConnection con=null;//声明数据库连接组件
private JComboBox comboBox=null;
private JButton delete=null;
private JButton reset=null;
private JPanel comboBoxPane=null;
private JPanel buttonsPane=null;
public DeleteItemFromFirstCatalog(){
try{
delete=new JButton("删除");
reset=new JButton("重置");
con=new DBConnection();
String queryStr="select* from First_Catalog ";
ResultSet rs=con.executeSelect(queryStr);
int size=0;
while(rs.next()){
size++;
}
String[] data=new String[size+1];
data[0]="请选择商品大类:";
int count=1;
rs=null;
rs=con.executeSelect(queryStr);
while(rs.next()){
int ID=rs.getInt(1);
String name=rs.getString(2);
data[count++]=name;
}
comboBox=new JComboBox(data);
comboBox.setSelectedIndex(0);
comboBoxPane=new JPanel();
comboBoxPane.add(comboBox,BorderLayout.CENTER);
buttonsPane=new JPanel();
buttonsPane.setLayout(new FlowLayout(FlowLayout.CENTER));
buttonsPane.add(delete);
buttonsPane.add(reset);
this.add(comboBox,BorderLayout.NORTH);
this.add(buttonsPane,BorderLayout.CENTER);
delete.addActionListener(this);
reset.addActionListener(this);
if(size==0){
JOptionPane.showMessageDialog(null, "数据库中没有任何商品大类!", "提示", JOptionPane.ERROR_MESSAGE);
delete.setEnabled(false);
reset.setEnabled(false);
return;
}
}catch(Exception e){
//e.printStackTrace();
JOptionPane.showMessageDialog(null, "发生错误!", "提示", JOptionPane.ERROR_MESSAGE);
return;
}
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==reset){
comboBox.setSelectedIndex(0);
return;
}
if(e.getSource()==delete){
try{
String name=(String)comboBox.getSelectedItem();
if(name.equals("请选择商品大类:")){
JOptionPane.showMessageDialog(null, "您没有选择商品大类!", "提示", JOptionPane.ERROR_MESSAGE);
return;
}
int confirm=JOptionPane.showConfirmDialog(null,"您确认要删除 "+name+" 吗?","确认",JOptionPane.YES_NO_OPTION);
if(confirm==JOptionPane.YES_OPTION){
String queryStr1="select First_ID from First_Catalog where First_name='"+name+"'";
ResultSet set1=con.executeSelect(queryStr1);
int ID=0;
if(set1.next()){
ID=set1.getInt(1);
}
String deleteStr1="delete from First_Catalog where First_name='"+name+"'";
con.executeDML(deleteStr1);
//删除Second_Catalog 表中的相应记录
String queryStr3="select Second_ID from Second_Catalog where To_First_ID="+
ID;
ResultSet set3=con.executeSelect(queryStr3);
while(set3.next()){
String queryStr2="select Third_ID from Third_Catalog where To_Second_ID="+set3.getInt(1);
ResultSet set4=con.executeSelect(queryStr2);
while(set4.next()){
//JOptionPane.showMessageDialog(null, "Third_Catalog中存在记录!", "提示", JOptionPane.INFORMATION_MESSAGE);
String queryStr4="select* from operationRecord where To_Third_ID="+set4.getInt(1);
ResultSet set5=con.executeSelect(queryStr4);
while(set5.next()){
con.executeDML("delete from operationRecord where To_Third_ID="+set4.getInt(1));
//JOptionPane.showMessageDialog(null, "从operationRecord表中删除记录成功!", "提示", JOptionPane.INFORMATION_MESSAGE);
//continue;
}
con.executeDML("delete from Third_Catalog where To_Second_ID="+set3.getInt(1));
//JOptionPane.showMessageDialog(null, "operationRecord没有相应记录!", "提示", JOptionPane.INFORMATION_MESSAGE);
}
con.executeDML("delete from Second_ID from Second_Catalog where To_First_ID="+
ID);
}
comboBox.setSelectedIndex(0);
JOptionPane.showMessageDialog(null, "删除成功!", "提示", JOptionPane.INFORMATION_MESSAGE);
return;
}
comboBox.setSelectedIndex(0);
return;
}catch(Exception excep){
excep.printStackTrace();
JOptionPane.showMessageDialog(null, "发生错误!", "提示", JOptionPane.ERROR_MESSAGE);
return;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -