package login_123;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
public class AddGoods extends JFrame implements ActionListener{
JLabel JL = new JLabel("添加基本信息:");
JLabel number = new JLabel("商品编号");
JTextField Number = new JTextField();
JLabel JClass=new JLabel("类别");
String str[]={"女装","男装","童装"};
JComboBox jcb=new JComboBox(str);
JLabel name = new JLabel("商品名称");
JTextField Name = new JTextField();
JLabel price=new JLabel("商品价格");
JTextField Price = new JTextField();
JLabel storage= new JLabel("库存量");
JTextField Storage = new JTextField();
JTextField jt=new JTextField(10);
JButton Add = new JButton("添加");
JButton Reset = new JButton("重置");
JButton Exit = new JButton("退出");
public AddGoods() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("添加商品");
this.setBounds(5,5,450,550);
this.setLocationRelativeTo(null);
this.setLayout(null);
this.add(JL);
JL.setBounds(100, 100, 100, 20);
this.add(number);
number.setBounds(100, 130, 100, 20);
this.add(Number);
Number.setBounds(150, 130, 100, 20);
this.add(JClass);
JClass.setBounds(100, 160, 100, 20);
jcb.setBounds(150, 160, 100, 20);
this.add(jcb);
jcb.addActionListener(this);
this.add(name);
name.setBounds(100,190, 100, 20);
this.add(Name);
Name.setBounds(150, 190, 100, 20);
this.add(price);
price.setBounds(100, 220, 100, 20);
this.add(Price);
Price.setBounds(150,220,100,20);
this.add(storage);
price.setBounds(100, 250, 100, 20);
this.add(Storage);
Price.setBounds(150,250,100,20);
this.add(Add);
Add.setBounds(90, 300, 60, 20);
Add.addActionListener(this);
this.add(Reset);
Reset.setBounds(190,300,60,20);
Reset.addActionListener(this);
this.add(Exit);
Exit.setBounds(290,300,60,20);
Exit.addActionListener(this);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
String sql = null;
public void actionPerformed(ActionEvent e) {
if(e.getSource()==Add) {
String snumber=Number.getText();
snumber = null;
String sname=Name.getText();
sname = null;
String sprice=Price.getText();
sprice=null;
String sstorage=Storage.getText();
sstorage=null;
try {
Connection cot=ConnectionFactory.getConnection();
Statement stm=cot.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE ,ResultSet.CONCUR_UPDATABLE );
int s=jcb.getSelectedIndex();
String jc=null;
if(s==0)jc="女装";
else if(s==1)jc="男装";
else if(s==2)jc="童装";
sql="insert into goods values("+snumber+","+jc+","+sname+","+sprice+","+sstorage+")";
int n=stm.executeUpdate(sql);
if(n!=0)JOptionPane.showMessageDialog(null,"添加成功!");
else JOptionPane.showMessageDialog(null,"该商品已存在!");
}catch(Exception ee) {
ee.printStackTrace();
}
}
if(e.getSource()==Reset) {
Number.setText(null);
Name.setText(null);
Price.setText(null);
Storage.setText(null);
}
if(e.getSource()==Exit) {
this.setVisible(false);
}
}
}
错误提示:
at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
我运行没问题: