📄 deleteitemfromthirdcatalog.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 DeleteItemFromThirdCatalog extends JPanel implements ActionListener,ItemListener{
private DBConnection con=null;//声明数据库连接组件
private JComboBox comboBoxFirst=null;
private JComboBox comboBoxSecond=null;
private JComboBox comboBoxThird=null;
private JLabel first=new JLabel("请选择商品大类:");
private JLabel second=new JLabel("请选择商品明细类:");
private JLabel third=new JLabel("请选择具体商品:");
private JButton delete=new JButton("删除");
private JPanel comboBoxPane=null;
private JPanel buttonsPane=null;
private String[] dataSecond=new String [1000];
private String[] dataThird=new String[1000];
public DeleteItemFromThirdCatalog(){
try{
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 GridLayout(3,2));
comboBoxPane.add(first);
comboBoxPane.add(comboBoxFirst);
comboBoxPane.add(second);
comboBoxSecond=new JComboBox();
dataSecond[0]="";
comboBoxSecond.addItem(dataSecond[0]);
comboBoxPane.add(comboBoxSecond);
comboBoxThird=new JComboBox();
dataThird[0]="";
comboBoxThird.addItem(dataThird[0]);
comboBoxPane.add(third);
comboBoxPane.add(comboBoxThird);
buttonsPane=new JPanel();
buttonsPane.setLayout(new FlowLayout());
buttonsPane.add(delete);
this.setLayout(new GridLayout(2,1));
this.add(comboBoxPane);
this.add(buttonsPane);
comboBoxFirst.addItemListener(this);
comboBoxSecond.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();
String nameThird=(String)comboBoxThird.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;
}
}
if(nameThird==null){
JOptionPane.showMessageDialog(null, "请您选择具体商品!", "提示",JOptionPane.ERROR_MESSAGE);
return;
}else{
if(nameThird.equals("")){
JOptionPane.showMessageDialog(null, "请您选择具体商品!", "提示",JOptionPane.ERROR_MESSAGE);
return;
}
}
int confirm=JOptionPane.showConfirmDialog(null,"您确认要删除 "+nameThird+" 吗?","确认",JOptionPane.YES_NO_OPTION);
if(confirm==JOptionPane.YES_OPTION){
String queryStr1="select Third_ID from Third_Catalog where Third_name='"+nameThird+"'";
ResultSet set1=con.executeSelect(queryStr1);
int IDThird=0;
if(set1.next()){
IDThird=set1.getInt(1);
}
String deleteStr1="delete from Third_Catalog where Third_name='"+nameThird+"'";
con.executeDML(deleteStr1);
String queryStr3="select* from operationRecord where To_Third_ID="+
IDThird;
ResultSet set3=con.executeSelect(queryStr3);
if(set3.next()){
con.executeDML("delete from operationRecord where To_Third_ID="+IDThird);
}
JOptionPane.showMessageDialog(null, "删除成功!", "提示", JOptionPane.INFORMATION_MESSAGE);
//return;
//comboBoxSecond.removeAllItems();
//comboBoxSecond.addItem(dataSecond[0]);
comboBoxThird.removeAllItems();
comboBoxThird.addItem(dataSecond[0]);
comboBoxFirst.setSelectedIndex(0);
comboBoxFirst.updateUI();
}
}catch(Exception excep){
//excep.printStackTrace();
JOptionPane.showMessageDialog(null, "发生错误!", "提示", JOptionPane.ERROR_MESSAGE);
return;
}
}
}
public void itemStateChanged(ItemEvent e){
if(e.getSource()==comboBoxFirst){
if(e.getStateChange()==ItemEvent.SELECTED){
delete.setEnabled(true);
comboBoxSecond.removeAllItems();
String nameFirst=(String)comboBoxFirst.getSelectedItem();
if(nameFirst==null){
//JOptionPane.showMessageDialog(null, "请您选择商品大类", "提示",JOptionPane.ERROR_MESSAGE);
return;
}else{
if(nameFirst.equals("")){
//JOptionPane.showMessageDialog(null, "请您选择商品大类", "提示",JOptionPane.ERROR_MESSAGE);
return;
}
}
String queryFirst="select First_ID from First_Catalog where First_name='"+nameFirst+"'";
ResultSet set=con.executeSelect(queryFirst);
int first_ID=0;
int second_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=con.executeSelect(querySecond);
int count=1;
if(!set.next()){
JOptionPane.showMessageDialog(null,"该商品大类目前还没有明细类!" , "提示", JOptionPane.INFORMATION_MESSAGE);
delete.setEnabled(false);
return;
}
set=null;
set=con.executeSelect(querySecond);
while(set.next()){
dataSecond[count]=set.getString(1);
comboBoxSecond.addItem(dataSecond[count]);
count++;
}
comboBoxSecond.setSelectedIndex(0);
}catch(Exception excep){
//excep.printStackTrace();
//JOptionPane.showMessageDialog(null, "发生错误!", "提示", JOptionPane.INFORMATION_MESSAGE);
}
}
}
if(e.getSource()==comboBoxSecond){
if(e.getStateChange()==ItemEvent.SELECTED){
delete.setEnabled(true);
comboBoxThird.removeAllItems();
String nameSecond=(String)comboBoxSecond.getSelectedItem();
if(nameSecond==null){
//JOptionPane.showMessageDialog(null, "请您选择明细类", "提示",JOptionPane.ERROR_MESSAGE);
return;
}else{
if(nameSecond.equals("")){
//JOptionPane.showMessageDialog(null, "请您选择明细类", "提示",JOptionPane.ERROR_MESSAGE);
return;
}
}
String querySecond="select Second_ID from Second_Catalog where Second_name='"+nameSecond+"'";
ResultSet set=con.executeSelect(querySecond);
int second_ID=0;
try{
if(set.next()){
second_ID=set.getInt(1);
}
String queryThird="select Third_name from Third_Catalog where To_Second_ID="+second_ID;
set=null;
set=con.executeSelect(queryThird);
int count=1;
if(!set.next()){
JOptionPane.showMessageDialog(null,"该商品明细类目前还没有具体商品!" , "提示", JOptionPane.INFORMATION_MESSAGE);
delete.setEnabled(false);
return;
}
set=null;
set=con.executeSelect(queryThird);
while(set.next()){
dataThird[count]=set.getString(1);
comboBoxThird.addItem(dataThird[count]);
count++;
}
}catch(Exception excep){
//excep.printStackTrace();
//JOptionPane.showMessageDialog(null, "发生错误!", "提示", JOptionPane.INFORMATION_MESSAGE);
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -