📄 firstcatalogpane.java
字号:
package file2;
import java.sql.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class FirstCatalogPane extends JPanel implements ActionListener{
//声明数据库连接组件
private DBConnection con=null;
private JLabel tip=null;
private JLabel name=null;
private JButton insert=null;
private JButton reset=null;
private JTextField nameTextField=null;
private JPanel tipPane=null;
private JPanel namePane=null;
private JPanel buttonsPane=null;
public FirstCatalogPane(){
tip=new JLabel("请在下面输入商品大类名称");
name=new JLabel("名称:");
insert=new JButton("插入");
reset=new JButton("重置");
nameTextField=new JTextField(10);
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);
buttonsPane=new JPanel();
buttonsPane.setLayout(new FlowLayout(FlowLayout.CENTER,5,5));
buttonsPane.add(insert);
buttonsPane.add(reset);
this.setLayout(new GridLayout(3,1));
this.add(tipPane);
this.add(namePane);
this.add(buttonsPane);
insert.addActionListener(this);
reset.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==reset){
nameTextField.setText("");
return;
}
if(e.getSource()==insert){
String name=nameTextField.getText().trim();
if(name.equals("")){
JOptionPane.showMessageDialog(null, "商品名称不能为空!", "提示", JOptionPane.ERROR_MESSAGE);
return;
}
con=new DBConnection();
String querySql="select* from First_Catalog where First_name='"+name+"'";
String insertSql="insert into First_Catalog values('"+name+"')";
try{
ResultSet set=con.executeSelect(querySql);
if(set.next()){
JOptionPane.showMessageDialog(null, "该名称已经存在", "提示", JOptionPane.ERROR_MESSAGE);
return;
}
con.executeDML(insertSql);
JOptionPane.showMessageDialog(null, "插入成功!", "提示", JOptionPane.INFORMATION_MESSAGE);
return;
}catch(SQLException sql){
JOptionPane.showMessageDialog(null, "发生SQL错误!", "提示", JOptionPane.ERROR_MESSAGE);
return;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -