📄 secondcatalogpane.java
字号:
package file2;
import java.sql.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class SecondCatalogPane extends JPanel implements ActionListener{
//声明数据库连接组件
private DBConnection con=null;
//声明各种标签
private JLabel tip=null;
private JLabel name=null;
private JLabel belongsTo=null;
private JTextField nameTextField=null;
private JComboBox comboBox=null;
private JButton insert=null;
private JButton reset=null;
//声明装载上面声明的各种控件的面版
private JPanel tipPane=null;
private JPanel namePane=null;
private JScrollPane belongsToPane=null;//装载list
private JPanel belongstoPane=null;//装载belongsTo标签
private JPanel buttonsPane=null;
//private JPanel echoPane=null;//装载所有面版
public SecondCatalogPane(){
try{
tip=new JLabel("请在下面输入商品明细类信息");
name=new JLabel("名称:");
belongsTo=new JLabel("所属类别:");
nameTextField=new JTextField(10);
insert=new JButton("插入");
reset=new JButton("重置");
tipPane=new JPanel();
tipPane.setLayout(new FlowLayout(FlowLayout.LEFT));
tipPane.add(tip);
namePane=new JPanel();
namePane.setLayout(new FlowLayout(FlowLayout.LEFT));
namePane.add(name);
namePane.add(nameTextField);
belongstoPane=new JPanel();
belongstoPane.setLayout(new FlowLayout(FlowLayout.LEFT));
belongstoPane.add(belongsTo);
//声明所属类别面版
con=new DBConnection();
String[] dataOfList=new String[100];
dataOfList[0]="请选择商品大类:";
String querySql="select* from First_Catalog";
ResultSet set=con.executeSelect(querySql);
int count=1;
while(set.next()){
String name=set.getString(2);
dataOfList[count++]=name;
}
comboBox=new JComboBox(dataOfList);
comboBox.setSelectedIndex(0);
belongstoPane.add(comboBox);
//belongsToPane=new JScrollPane(comboBox);
buttonsPane=new JPanel();
buttonsPane.setLayout(new FlowLayout(FlowLayout.CENTER));
buttonsPane.add(insert);
buttonsPane.add(reset);
//echoPane=new JPanel();
this.setLayout(new GridLayout(4,1));
this.add(tipPane);
this.add(namePane);
this.add(belongstoPane);
//this.add(belongsToPane);
this.add(buttonsPane);
//this.setLayout(new FlowLayout());
//this.add(echoPane);
insert.addActionListener(this);
reset.addActionListener(this);
}catch(SQLException sql){
JOptionPane.showMessageDialog(null, "发生SQL错误!", "提示", JOptionPane.ERROR_MESSAGE);
return;
}
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==reset){
nameTextField.setText("");
comboBox.setSelectedIndex(0);
return;
}
if(e.getSource()==insert){
String name=nameTextField.getText().trim();
String belongsTo=(String)comboBox.getSelectedItem();
if(name.equals("")||belongsTo.equals("请选择商品大类:")){
JOptionPane.showMessageDialog(null, "请您选择商品大类,且商品名称不能为空!", "警告", JOptionPane.ERROR_MESSAGE);
return;
}
String querySql="select* from Second_Catalog where Second_name='"+name+"'";
ResultSet rs=con.executeSelect(querySql);
try{
if(rs.next()){
JOptionPane.showMessageDialog(null, "该名称已经存在!", "提示", JOptionPane.ERROR_MESSAGE);
return;
}
String queryID="select* from First_Catalog where First_name='"+belongsTo+"'";
ResultSet set=con.executeSelect(queryID);
if(set.next()){
int ID=set.getInt(1);
String insertSql="insert into Second_Catalog values("+ID+",'"+name+"')";
con.executeDML(insertSql);
JOptionPane.showMessageDialog(null, "插入成功!", "提示", JOptionPane.INFORMATION_MESSAGE);
return;
}
}catch(SQLException sql){
JOptionPane.showMessageDialog(null, "发生SQL错误!", "提示", JOptionPane.ERROR_MESSAGE);
sql.printStackTrace();
return;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -