📄 deleteitemfromsecondcatalog.java
字号:
package file2;
import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.sql.*;
public class DeleteItemFromSecondCatalog extends JPanel implements ActionListener,ItemListener{
private DBConnection con=null;//声明数据库连接组件
private JComboBox comboBoxFirst=null;
private JComboBox comboBoxSecond=null;
private JLabel first=new JLabel("请选择商品大类:");
private JLabel second=new JLabel("请选择商品明细类:");
private JButton delete=null;
private JPanel comboBoxPane=null;
private JPanel buttonsPane=null;
private String[] dataSecond=new String [1000];
public DeleteItemFromSecondCatalog(){
try{
delete=new JButton("删除");
con=new DBConnection();
String queryStr="select* from First_Catalog ";
ResultSet rs=con.executeSelect(queryStr);
int size=0;
while(rs.next()){
size++;
}
if(size==0){
JOptionPane.showMessageDialog(null, "数据库中没有任何商品大类!", "提示", JOptionPane.ERROR_MESSAGE);
delete.setEnabled(false);
}
String[] dataFirst=new String[size+1];
dataFirst[0]="";
int count=1;
rs=null;
rs=con.executeSelect(queryStr);
while(rs.next()){
int ID=rs.getInt(1);
String name=rs.getString(2);
dataFirst[count++]=name;
}
comboBoxFirst=new JComboBox(dataFirst);
comboBoxPane=new JPanel();
comboBoxPane.setLayout(new FlowLayout());
comboBoxPane.add(first);
comboBoxPane.add(comboBoxFirst);
comboBoxPane.add(second);
comboBoxSecond=new JComboBox();
dataSecond[0]="";
comboBoxSecond.addItem(dataSecond[0]);
comboBoxPane.add(comboBoxSecond);
buttonsPane=new JPanel();
buttonsPane.setLayout(new FlowLayout(FlowLayout.CENTER));
buttonsPane.add(delete);
this.setLayout(new GridLayout(2,1));
this.add(comboBoxPane);
this.add(buttonsPane);
comboBoxFirst.addItemListener(this);
delete.addActionListener(this);
}catch(Exception e){
//e.printStackTrace();
JOptionPane.showMessageDialog(null, "发生错误!", "提示", JOptionPane.ERROR_MESSAGE);
}
//return;
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==delete){
try{
String nameFirst=(String)comboBoxFirst.getSelectedItem();
String nameSecond=(String)comboBoxSecond.getSelectedItem();
if(nameFirst==null){
JOptionPane.showMessageDialog(null, "请您选择商品大类", "提示",JOptionPane.ERROR_MESSAGE);
return;
}else{
if(nameFirst.equals("")){
JOptionPane.showMessageDialog(null, "请您选择商品大类", "提示",JOptionPane.ERROR_MESSAGE);
return;
}
}
if(nameSecond==null){
JOptionPane.showMessageDialog(null, "请您选择明细类", "提示",JOptionPane.ERROR_MESSAGE);
return;
}else{
if(nameSecond.equals("")){
JOptionPane.showMessageDialog(null, "请您选择明细类", "提示",JOptionPane.ERROR_MESSAGE);
return;
}
}
int confirm=JOptionPane.showConfirmDialog(null,"您确认要删除 "+nameSecond+" 吗?","确认",JOptionPane.YES_NO_OPTION);
if(confirm==JOptionPane.YES_OPTION){
String queryStr1="select Second_ID from Second_Catalog where Second_name='"+nameSecond+"'";
ResultSet set1=con.executeSelect(queryStr1);
int IDSecond=0;
if(set1.next()){
IDSecond=set1.getInt(1);
}
String deleteStr1="delete from Second_Catalog where Second_name='"+nameSecond+"'";
con.executeDML(deleteStr1);
//删除Second_Catalog 表中的相应记录
String queryStr3="select Third_ID from Third_Catalog where To_Second_ID="+
IDSecond;
ResultSet set3=con.executeSelect(queryStr3);
while(set3.next()){
int id=set3.getInt(1);
String queryStr2="select* from operationRecord where To_Third_ID="+id;
ResultSet set4=con.executeSelect(queryStr2);
if(set4.next()){
con.executeDML("delete from operationRecord where To_Third_ID="+id);
}
}
con.executeDML("delete from Third_Catalog where To_Second_ID="+IDSecond);
JOptionPane.showMessageDialog(null, "删除成功!", "提示", JOptionPane.INFORMATION_MESSAGE);
//return;
comboBoxSecond.removeAllItems();
comboBoxSecond.addItem(dataSecond[0]);
comboBoxFirst.setSelectedIndex(0);
comboBoxFirst.updateUI();
//addCount=0;
}
}catch(Exception excep){
//excep.printStackTrace();
JOptionPane.showMessageDialog(null, "发生错误!", "提示", JOptionPane.ERROR_MESSAGE);
return;
}
}
}
public void itemStateChanged(ItemEvent e){
if(e.getStateChange()==ItemEvent.SELECTED){
delete.setEnabled(true);
comboBoxSecond.removeAllItems();
String nameFirst=(String)comboBoxFirst.getSelectedItem();
String queryFirst="select First_ID from First_Catalog where First_name='"+nameFirst+"'";
ResultSet set=con.executeSelect(queryFirst);
int first_ID=0;
try{
if(set.next()){
first_ID=set.getInt(1);
}
String querySecond="select Second_name from Second_Catalog where To_First_ID="+first_ID;
set=null;
set=con.executeSelect(querySecond);
int count=1;
if(!set.next()){
JOptionPane.showMessageDialog(null,"该商品大类目前还没有明细类!" , "提示", JOptionPane.INFORMATION_MESSAGE);
delete.setEnabled(false);
}
set=null;
set=con.executeSelect(querySecond);
while(set.next()){
dataSecond[count++]=set.getString(1);
}
for(int addNum=1;addNum<count;addNum++){
comboBoxSecond.addItem(dataSecond[addNum]);
}
}catch(Exception excep){
//excep.printStackTrace();
//JOptionPane.showMessageDialog(null, "发生错误!", "提示", JOptionPane.INFORMATION_MESSAGE);
}
return;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -