📄 readeradd.java
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
public class ReaderAdd
extends JFrame
implements ActionListener {
DataBaseManager db = new DataBaseManager();
ResultSet rs;
JPanel panel1, panel2;//声明两个轻量级容器
JLabel ReadernoLabel, ReaderNameLabel,//用于短文本字符串或图像或二者的显示区
ReaderTypeLabel, sexLabel,PartLabel, AddressLabel,
TelephoneLabel, EmailLabel,IntimeLabel,ComLabel;
JTextField ReadernoTextField, ReaderNameTextField,//JTextField允许编辑单行文本
ReaderTypeTextField, sexTextField,PartTextField,
AddressTextField, TelephoneTextField,EmailTextField,IntimeTextField,ComTextField;
Container c;
JButton clearBtn, addBtn, exitBtn;//配置按钮
public ReaderAdd() {
super("添加读者信息");
c = getContentPane();//获得当前的容器
c.setLayout(new BorderLayout());//使用边界页面设置来布局
ReadernoLabel = new JLabel("学号", JLabel.CENTER);//显示名称
ReaderNameLabel = new JLabel("姓名", JLabel.CENTER);
ReaderTypeLabel= new JLabel("类型", JLabel.CENTER);
sexLabel = new JLabel("性别", JLabel.CENTER);
PartLabel = new JLabel("单位", JLabel.CENTER);
AddressLabel = new JLabel("地址", JLabel.CENTER);
TelephoneLabel = new JLabel("电话", JLabel.CENTER);
EmailLabel= new JLabel("电子邮件",JLabel.CENTER);
IntimeLabel = new JLabel("登记日期",JLabel.CENTER);
ComLabel = new JLabel("备注", JLabel.CENTER);
ReadernoTextField = new JTextField(20);//文本框的宽度可以容纳15个字符
ReaderNameTextField = new JTextField(20);
ReaderTypeTextField = new JTextField(20);
sexTextField = new JTextField(20);
PartTextField = new JTextField(20);
AddressTextField = new JTextField(20);
TelephoneTextField = new JTextField(20);
EmailTextField = new JTextField(20);
IntimeTextField = new JTextField(20);
ComTextField = new JTextField(20);
panel1 = new JPanel();
panel1.setLayout(new GridLayout(10, 2));//使用方格式页面设置来布局,10行2列
panel1.add(ReadernoLabel);
panel1.add(ReadernoTextField);
panel1.add(ReaderNameLabel);
panel1.add(ReaderNameTextField);
panel1.add(ReaderTypeLabel);
panel1.add(ReaderTypeTextField);
panel1.add(sexLabel);
panel1.add(sexTextField);
panel1.add(PartLabel);
panel1.add(PartTextField);
panel1.add(AddressLabel);
panel1.add(AddressTextField);
panel1.add(TelephoneLabel);
panel1.add(TelephoneTextField);
panel1.add(EmailLabel);
panel1.add(EmailTextField);
panel1.add(IntimeLabel);
panel1.add(IntimeTextField);
panel1.add(ComLabel);
panel1.add(ComTextField);
panel2 = new JPanel();
panel2.setLayout(new GridLayout(1, 3));//1行3列
clearBtn = new JButton("清空");
clearBtn.addActionListener(this);
addBtn = new JButton("添加");
addBtn.addActionListener(this);
exitBtn = new JButton("退出");
exitBtn.addActionListener(this);
panel2.add(clearBtn);//添加按钮到面板
panel2.add(addBtn);
panel2.add(exitBtn);
c.add(panel1, BorderLayout.CENTER);//中间位置
c.add(panel2, BorderLayout.SOUTH);//南边位置
}
//事件发生的处理操作
public void actionPerformed(ActionEvent e) {
if (e.getSource() == exitBtn) {
db.closeConnection();
this.dispose();//关闭窗体并释放该窗体的占用的部分资源
}
else if (e.getSource() == clearBtn) {//Button为ActionEvent的事件源
ReadernoTextField.setText("");//设置文本框为空
ReaderNameTextField.setText("");
ReaderTypeTextField.setText("");
sexTextField.setText("");
PartTextField.setText("");
AddressTextField.setText("");
TelephoneTextField.setText("");
EmailTextField.setText("");
IntimeTextField.setText("");
ComTextField.setText("");
}
else if (e.getSource() == addBtn) {//获得按钮的信息
if (ReadernoTextField.getText().trim().equals("")) {
JOptionPane.showMessageDialog(null, "学号不能为空!");
}
else if (ReaderNameTextField.getText().trim().equals("")) {
JOptionPane.showMessageDialog(null, "姓名不能为空!");
}
else if (ReaderTypeTextField.getText().trim().equals("")) {
JOptionPane.showMessageDialog(null, "类型不能为空!");
}
else if (sexTextField.getText().trim().equals("")) {
JOptionPane.showMessageDialog(null, "性别不能为空!");
}
else if (TelephoneTextField.getText().trim().equals("")) {
JOptionPane.showMessageDialog(null, "电话不能为空!");
}
else if (IntimeTextField.getText().trim().equals("")) {
JOptionPane.showMessageDialog(null, "登记日期不能为空!");
}
else {
try {//strSQL为传入的查询的sql语
String strSQL = "insert into readers(Readerno,ReaderName,ReaderType,sex,Part,Address,Telephone,Email,Intime,Com) values('"+
ReadernoTextField.getText().trim() + "','" +
ReaderNameTextField.getText().trim() + "','" +
ReaderTypeTextField.getText().trim() + "','" +
sexTextField.getText().trim() + "','" +
PartTextField.getText().trim() + "','" +
AddressTextField.getText().trim() + "','" +
TelephoneTextField.getText().trim() + "','" +
EmailTextField.getText().trim()+ "','" +
IntimeTextField.getText().trim()+"','"+
ComTextField.getText().trim() + "')";
if (db.updateSql(strSQL)>0) {//执行了对数据库的操作
JOptionPane.showMessageDialog(null, "添加读者成功!");//显示一个消息对话框
this.dispose();
}
else {
JOptionPane.showMessageDialog(null, "添加读者失败!");
this.dispose();
}
db.closeConnection();//断开数据库的连接
}
catch (Exception ex) {
System.out.println(ex.toString());
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -