在此,先提醒一下想直接copy我的代码的”GUET“同学们:万一到时候,验收的时候,老师发现咱俩做的东西是一模一样的,会不会很尴尬呢(O(∩_∩)O),所以呢,记得改改界面啊喂。。。。
这次的pro版本其实就是上次的javaGUI作品,我把它从文件管理的方式改为了数据库管理(貌似看起来更专业了???)
直接上代码吧!!!!
1.首先是图形化界面的代码,(先提前声明啊:图形化界面的代码大部分由插件自动生成的(我比较懒)):
添加员工界面:
/*
* Created by JFormDesigner on Sat Apr 03 11:45:59 CST 2021
*/
package view;
import Controller.*;
import model.Employee;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.GroupLayout;
/**
* @author Brainrain
*/
public class AddEmployeeView extends JFrame {
public AddEmployeeView() {
initComponents();
}
private void CommitButtonActionPerformed(ActionEvent e) { //进行用户交互,添加用户信息
// 添加员工信息
try {
new Write(EmployName.getText(),EmployBirthday.getText(),
EmployEmail.getText(),EmploySalary.getText());
JOptionPane.showMessageDialog(this,"成功添加!!!");
} catch (EmailErrorException exception) {
JOptionPane.showMessageDialog(this,"您输入的邮箱格式有误,请重新输入,例如:1234567@qq.com");
} catch (BirthdayErrorException birthdayErrorException) {
JOptionPane.showMessageDialog(this,"您输入的出生日期格式有误,请重新输入,例如:2001-01-01");
} catch (NumFromatException numFromatException) {
JOptionPane.showMessageDialog(this,"您输入的数字有误,请重新输入,");
} catch (EmpNullException empNullException) {
JOptionPane.showMessageDialog(this,"请不要输入空信息");
} catch (NameException nameException) {
JOptionPane.showMessageDialog(this,"您输入的名字为空!!");
}
}
private void initComponents() {
// JFormDesigner - Component initialization - DO NOT MODIFY //GEN-BEGIN:initComponents
label1 = new JLabel();
label2 = new JLabel();
label3 = new JLabel();
label5 = new JLabel();
label6 = new JLabel();
EmployBirthday = new JTextField();
EmploySalary = new JTextField();
EmployEmail = new JTextField();
EmployName = new JTextField();
CommitButton = new JButton();
//======== this ========
setTitle("\u804c\u5de5\u4fe1\u606f\u7ba1\u7406\u7cfb\u7edf");
Container contentPane = getContentPane();
//---- label1 ----
label1.setText("\u57fa\u672c\u5de5\u8d44:");
//---- label2 ----
label2.setText("email:");
//---- label3 ----
label3.setText("\u51fa\u751f\u5e74\u6708:");
//---- label5 ----
label5.setText("\u59d3\u540d:");
//---- CommitButton ----
CommitButton.setText("\u63d0\u4ea4");
CommitButton.addActionListener(e -> CommitButtonActionPerformed(e));
GroupLayout contentPaneLayout = new GroupLayout(contentPane);
contentPane.setLayout(contentPaneLayout);
contentPaneLayout.setHorizontalGroup(
contentPaneLayout.createParallelGroup()
.addGroup(contentPaneLayout.createSequentialGroup()
.addGap(115, 115, 115)
.addGroup(contentPaneLayout.createParallelGroup()
.addComponent(label5)
.addComponent(label3)
.addComponent(label1)
.addComponent(label2))
.addGap(60, 60, 60)
.addGroup(contentPaneLayout.createParallelGroup()
.addGroup(contentPaneLayout.createSequentialGroup()
.addComponent(CommitButton)
.addContainerGap(244, Short.MAX_VALUE))
.addGroup(contentPaneLayout.createSequentialGroup()
.addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.TRAILING)
.addComponent(EmployBirthday, GroupLayout.DEFAULT_SIZE, 96, Short.MAX_VALUE)
.addComponent(EmployName, GroupLayout.DEFAULT_SIZE, 96, Short.MAX_VALUE)
.addComponent(EmploySalary, GroupLayout.Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 96, Short.MAX_VALUE)
.addComponent(EmployEmail, GroupLayout.DEFAULT_SIZE, 96, Short.MAX_VALUE))
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addComponent(label6, GroupLayout.PREFERRED_SIZE, 49, GroupLayout.PREFERRED_SIZE)
.addGap(171, 171, 171))))
);
contentPaneLayout.setVerticalGroup(
contentPaneLayout.createParallelGroup()
.addGroup(GroupLayout.Alignment.TRAILING, contentPaneLayout.createSequentialGroup()
.addContainerGap(57, Short.MAX_VALUE)
.addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(label5)
.addComponent(label6)
.addComponent(EmployName, GroupLayout.PREFERRED_SIZE, 18, GroupLayout.PREFERRED_SIZE))
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(label3, GroupLayout.PREFERRED_SIZE, 23, GroupLayout.PREFERRED_SIZE)
.addComponent(EmployBirthday, GroupLayout.PREFERRED_SIZE, 18, GroupLayout.PREFERRED_SIZE))
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(label1)
.addComponent(EmploySalary, GroupLayout.DEFAULT_SIZE, 0, Short.MAX_VALUE))
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(label2)
.addComponent(EmployEmail, GroupLayout.PREFERRED_SIZE, 20, GroupLayout.PREFERRED_SIZE))
.addGap(68, 68, 68)
.addComponent(CommitButton)
.addGap(132, 132, 132))
);
pack();
setLocationRelativeTo(getOwner());
// JFormDesigner - End of component initialization //GEN-END:initComponents
}
// JFormDesigner - Variables declaration - DO NOT MODIFY //GEN-BEGIN:variables
private JLabel label1;
private JLabel label2;
private JLabel label3;
private JLabel label5;
private JLabel label6;
private JTextField EmployBirthday;
private JTextField EmploySalary;
private JTextField EmployEmail;
private JTextField EmployName;
private JButton CommitButton;
// JFormDesigner - End of variables declaration //GEN-END:variables
}
删除员工界面:
/*
* Created by JFormDesigner on Sat Apr 03 14:01:29 CST 2021
*/
package view;
import java.awt.*;
import Controller.Delete;
import Controller.EmptyException;
import Controller.NotFoundNoException;
import Controller.NotFoundNoException;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.GroupLayout;
/**
* @author Brainrain
*/
public class DeleteView extends JFrame {
public DeleteView() {
initComponents();
}
private void DeleteButtonActionPerformed(ActionEvent e) { //进行用户交互,删除用户信息
try {
Delete.delete(EmployNo.getText().toString());
JOptionPane.showMessageDialog(this,"删除成功");
} catch (NotFoundNoException exception)
{
JOptionPane.showMessageDialog(this,"无法查询到对应的员工号");
}
}
private void initComponents() {
// JFormDesigner - Component initialization - DO NOT MODIFY //GEN-BEGIN:initComponents
label2 = new JLabel();
label6 = new JLabel();
EmployNo = new JTextField();
DeleteButton = new JButton();
//======== this ========
setTitle("\u804c\u5de5\u4fe1\u606f\u7ba1\u7406\u7cfb\u7edf");
setMinimumSize(new Dimension(420, 280));
Container contentPane = getContentPane();
//---- label2 ----
label2.setText("\u5de5\u53f7\uff1a");
//---- DeleteButton ----
DeleteButton.setText("\u5220\u9664");
DeleteButton.addActionListener(e -> DeleteButtonActionPerformed(e));
GroupLayout contentPaneLayout = new GroupLayout(contentPane);
contentPane.setLayout(contentPaneLayout);
contentPaneLayout.setHorizontalGroup(
contentPaneLayout.createParallelGroup()
.addGroup(contentPaneLayout.createSequentialGroup()
.addGap(115, 115, 115)
.addComponent(label2)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 47, Short.MAX_VALUE)
.addGroup(contentPaneLayout.createParallelGroup()
.addGroup(GroupLayout.Alignment.TRAILING, contentPaneLayout.createSequentialGroup()
.addComponent(label6, GroupLayout.PREFERRED_SIZE, 49, GroupLayout.PREFERRED_SIZE)
.addGap(171, 171, 171))
.addGroup(GroupLayout.Alignment.TRAILING, contentPaneLayout.createSequentialGroup()
.addComponent(EmployNo, GroupLayout.PREFERRED_SIZE, 114, GroupLayout.PREFERRED_SIZE)
.addGap(76, 76, 76))))
.addGroup(contentPaneLayout.createSequentialGroup()
.addGap(154, 154, 154)
.addComponent(DeleteButton)
.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
contentPaneLayout.setVerticalGroup(
contentPaneLayout.createParallelGroup()
.addGroup(GroupLayout.Alignment.TRAILING, contentPaneLayout.createSequentialGroup()
.addContainerGap(15, Short.MAX_VALUE)
.addComponent(label6)
.addGap(67, 67, 67)
.addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(label2)
.addComponent(EmployNo, GroupLayout.PREFERRED_SIZE, 20, GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addComponent(DeleteButton)
.addGap(98, 98, 98))
);
pack();
setLocationRelativeTo(getOwner());
// JFormDesigner - End of component initialization //GEN-END:initComponents
}
// JFormDesigner - Variables declaration - DO NOT MODIFY //GEN-BEGIN:variables
private JLabel label2;
private JLabel label6;
private JTextField EmployNo;
private JButton DeleteButton;
// JFormDesigner - End of variables declaration //GEN-END:variables
}
登录界面:
/*
* Created by JFormDesigner on Sat Apr 03 10:05:04 CST 2021
*/
package view;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
* @author Brainrain
*/
public class LoginView extends JFrame {
public static void main(String[] args) {
new LoginView().setVisible(true);
}
public LoginView() {
initComponents();
}
private void ResetButtonActionPerformed(ActionEvent e) { //重置按钮的交互
ResetValue(e);
}
private void ResetValue(ActionEvent e) { //重置用户输入的内容
UserName.setText(" ");
Password.setText(" ");
}
private void LoginButtonActionPerformed(ActionEvent e) {//登录按钮的交互
Login(e);
}
private void Login(ActionEvent e) {
String passwd=Password.getText().toString(); //获取用户输入的内容
String user = UserName.getText().toString();
if(passwd.equals("")||user.equals(""))
{
JOptionPane.showMessageDialog(this,"用户名或密码不能为空");
}
else if(passwd.equals("123")&&user.equals("root"))
{
JOptionPane.showMessageDialog(this,"登录成功!!!");
this.dispose();
new MainView().setVisible(true);
}
else JOptionPane.showMessageDialog(this,"用户名或密码错误");
}
private void initComponents() {
// JFormDesigner - Component initialization - DO NOT MODIFY //GEN-BEGIN:initComponents
label1 = new JLabel();
label2 = new JLabel();
label3 = new JLabel();
UserName = new JTextField();
Password = new JTextField();
LoginButton = new JButton();
ResetButton = new JButton();
//======== this ========
setTitle("\u804c\u5de5\u4fe1\u606f\u7ba1\u7406\u7cfb\u7edf");
setFont(new Font(Font.DIALOG, Font.BOLD, 12));
Container contentPane = getContentPane();
//---- label1 ----
label1.setText("\u804c\u5de5\u4fe1\u606f\u7ba1\u7406\u7cfb\u7edf\u767b\u5f55\u754c\u9762");
label1.setHorizontalAlignment(SwingConstants.CENTER);
label1.setFont(new Font("Microsoft YaHei UI", Font.BOLD, 12));
//---- label2 ----
label2.setText("UserName:");
//---- label3 ----
label3.setText("Password:");
//---- LoginButton ----
LoginButton.setText("Login");
LoginButton.addActionListener(e -> LoginButtonActionPerformed(e));
//---- ResetButton ----
ResetButton.setText("Reset");
ResetButton.addActionListener(e -> ResetButtonActionPerformed(e));
GroupLayout contentPaneLayout = new GroupLayout(contentPane);
contentPane.setLayout(contentPaneLayout);
contentPaneLayout.setHorizontalGroup(
contentPaneLayout.createParallelGroup()
.addGroup(contentPaneLayout.createSequentialGroup()
.addGap(141, 141, 141)
.addGroup(contentPaneLayout.createParallelGroup()
.addGroup(contentPaneLayout.createSequentialGroup()
.addComponent(label1, GroupLayout.PREFERRED_SIZE, 232, GroupLayout.PREFERRED_SIZE)
.addGap(0, 19, Short.MAX_VALUE))
.addGroup(contentPaneLayout.createSequentialGroup()
.addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.TRAILING, false)
.addComponent(label2, GroupLayout.Alignment.LEADING)
.addComponent(label3, GroupLayout.Alignment.LEADING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(LoginButton, GroupLayout.Alignment.LEADING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGap(18, 18, 18)
.addGroup(contentPaneLayout.createParallelGroup()
.addGroup(contentPaneLayout.createSequentialGroup()
.addComponent(UserName, GroupLayout.PREFERRED_SIZE, 108, GroupLayout.PREFERRED_SIZE)
.addGap(0, 0, Short.MAX_VALUE))
.addGroup(GroupLayout.Alignment.TRAILING, contentPaneLayout.createSequentialGroup()
.addGap(0, 77, Short.MAX_VALUE)
.addComponent(ResetButton))
.addComponent(Password))))
.addContainerGap(116, Short.MAX_VALUE))
);
contentPaneLayout.setVerticalGroup(
contentPaneLayout.createParallelGroup()
.addGroup(contentPaneLayout.createSequentialGroup()
.addGap(24, 24, 24)
.addComponent(label1, GroupLayout.PREFERRED_SIZE, 139, GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(label2)
.addComponent(UserName, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addGap(23, 23, 23)
.addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(label3)
.addComponent(Password, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 31, Short.MAX_VALUE)
.addGroup(contentPaneLayout.createParallelGroup()
.addComponent(ResetButton)
.addComponent(LoginButton, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGap(23, 23, 23))
);
pack();
setLocationRelativeTo(getOwner());
// JFormDesigner - End of component initialization //GEN-END:initComponents
}
// JFormDesigner - Variables declaration - DO NOT MODIFY //GEN-BEGIN:variables
private JLabel label1;
private JLabel label2;
private JLabel label3;
private JTextField UserName;
private JTextField Password;
private JButton LoginButton;
private JButton ResetButton;
// JFormDesigner - End of variables declaration //GEN-END:variables
}
主界面:
/*
* Created by JFormDesigner on Sat Apr 03 11:12:05 CST 2021
*/
package view;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.GroupLayout;
/**
* @author Brainrain
*/
public class MainView extends JFrame {
public MainView() {
initComponents();
}
private void AddEmployeeButtonActionPerformed(ActionEvent e) {
new AddEmployeeView().setVisible(true); //与添加员工按钮,进行交互
}
private void ShowEmployeeButtonActionPerformed(ActionEvent e) { //与展示员工信息按钮进行交互
new ShowEmployeeView().setVisible(true);
}
private void initComponents() {
// JFormDesigner - Component initialization - DO NOT MODIFY //GEN-BEGIN:initComponents
AddEmployeeButton = new JButton();
label1 = new JLabel();
ShowEmployeeButton = new JButton();
//======== this ========
setTitle("\u804c\u5de5\u7ba1\u7406\u7cfb\u7edf");
Container contentPane = getContentPane();
//---- AddEmployeeButton ----
AddEmployeeButton.setText("\u6dfb\u52a0\u804c\u5de5\u4fe1\u606f");
AddEmployeeButton.addActionListener(e -> AddEmployeeButtonActionPerformed(e));
//---- label1 ----
label1.setText("\u804c\u5de5\u7ba1\u7406\u7cfb\u7edf");
label1.setHorizontalAlignment(SwingConstants.CENTER);
label1.setFont(new Font("\u534e\u6587\u65b0\u9b4f", Font.PLAIN, 24));
//---- ShowEmployeeButton ----
ShowEmployeeButton.setText("\u6d4f\u89c8\u804c\u5de5\u4fe1\u606f");
ShowEmployeeButton.addActionListener(e -> ShowEmployeeButtonActionPerformed(e));
GroupLayout contentPaneLayout = new GroupLayout(contentPane);
contentPane.setLayout(contentPaneLayout);
contentPaneLayout.setHorizontalGroup(
contentPaneLayout.createParallelGroup()
.addGroup(contentPaneLayout.createSequentialGroup()
.addGap(55, 55, 55)
.addComponent(AddEmployeeButton, GroupLayout.PREFERRED_SIZE, 135, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 82, Short.MAX_VALUE)
.addComponent(ShowEmployeeButton, GroupLayout.PREFERRED_SIZE, 135, GroupLayout.PREFERRED_SIZE)
.addGap(76, 76, 76))
.addGroup(contentPaneLayout.createSequentialGroup()
.addGap(155, 155, 155)
.addComponent(label1, GroupLayout.PREFERRED_SIZE, 156, GroupLayout.PREFERRED_SIZE)
.addContainerGap(172, Short.MAX_VALUE))
);
contentPaneLayout.setVerticalGroup(
contentPaneLayout.createParallelGroup()
.addGroup(contentPaneLayout.createSequentialGroup()
.addGap(78, 78, 78)
.addComponent(label1, GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE)
.addGap(34, 34, 34)
.addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(AddEmployeeButton)
.addComponent(ShowEmployeeButton))
.addContainerGap(176, Short.MAX_VALUE))
);
pack();
setLocationRelativeTo(getOwner());
// JFormDesigner - End of component initialization //GEN-END:initComponents
}
// JFormDesigner - Variables declaration - DO NOT MODIFY //GEN-BEGIN:variables
private JButton AddEmployeeButton;
private JLabel label1;
private JButton ShowEmployeeButton;
// JFormDesigner - End of variables declaration //GEN-END:variables
}
不知道该叫什么名字的界面
/*
* Created by JFormDesigner on Sat Apr 03 12:03:23 CST 2021
*/
package view;
import Controller.Count;
import Controller.EmptyException;
import Controller.ShowInfo;
import model.Employee;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.GroupLayout;
/**
* @author Brainrain
*/
public class ShowEmployeeView extends JFrame {
public ShowEmployeeView() {
initComponents();
}
private void ConfigActionPerformed(ActionEvent e) { //与修改按钮进行交互
new UpdateView().setVisible(true);
}
private void DeleteActionPerformed(ActionEvent e) {
new DeleteView().setVisible(true);
}//与删除按钮进行交互
private void ShowActionPerformed(ActionEvent e) { //与显示全体员工按钮进行交互
new ShowView().setVisible(true);
}
private void CountActionPerformed(ActionEvent e) { //与统计人数按钮进行交互
try {
int num = Controller.Count.count();
JOptionPane.showMessageDialog(this,"目前人数为"+num+"!!");
} catch (EmptyException emptyException) {
JOptionPane.showMessageDialog(this,"目前人数为0!!");
}
}
private void AvgSalActionPerformed(ActionEvent e) { //与求平均值按钮进行交互
try {
String avg = Controller.AvgSal.avgsal();
JOptionPane.showMessageDialog(this,"基本工资的平均工资为:"+avg);
} catch (EmptyException emptyException) {
JOptionPane.showMessageDialog(this,"目前人数为0!!无法计算平均工资");
}
}
private void HighestSalEmpActionPerformed(ActionEvent e) { //与最高工资员工按钮进行交互
try {
String emp = Controller.HighestSal.high(); //得到最高工资员工的个人信息
String string = "最高工资的员工信息如下\n"+emp;
JOptionPane.showMessageDialog(this,string);
} catch (EmptyException emptyException) {
JOptionPane.showMessageDialog(this,"目前人数为0!!无法计算最高工资");
}
}
private void LowestSalEmpActionPerformed(ActionEvent e) { //与最低资员工按钮进行交互
try {
String emp = Controller.LowestSal.low();//得到最低工资员工的个人信息
String string = "最低工资的员工信息如下"+"\n"+emp;
JOptionPane.showMessageDialog(this,string);
} catch (EmptyException emptyException) {
JOptionPane.showMessageDialog(this,"目前人数为0!!无法计算最低工资");
}
}
private void initComponents() {
// JFormDesigner - Component initialization - DO NOT MODIFY //GEN-BEGIN:initComponents
menuBar1 = new JMenuBar();
menu3 = new JMenu();
Count = new JMenuItem();
AvgSal = new JMenuItem();
HighestSalEmp = new JMenuItem();
LowestSalEmp = new JMenuItem();
Config = new JButton();
Delete = new JButton();
Show = new JButton();
//======== this ========
setTitle("\u804c\u5de5\u7ba1\u7406\u7cfb\u7edf");
Container contentPane = getContentPane();
//======== menuBar1 ========
{
//======== menu3 ========
{
menu3.setText("\u7edf\u8ba1");
//---- Count ----
Count.setText("\u7edf\u8ba1\u804c\u5de5\u4eba\u6570");
Count.addActionListener(e -> CountActionPerformed(e));
menu3.add(Count);
//---- AvgSal ----
AvgSal.setText("\u57fa\u672c\u5de5\u8d44\u5e73\u5747\u503c");
AvgSal.addActionListener(e -> AvgSalActionPerformed(e));
menu3.add(AvgSal);
//---- HighestSalEmp ----
HighestSalEmp.setText("\u6700\u9ad8\u5de5\u8d44\u804c\u5de5");
HighestSalEmp.addActionListener(e -> HighestSalEmpActionPerformed(e));
menu3.add(HighestSalEmp);
//---- LowestSalEmp ----
LowestSalEmp.setText("\u6700\u4f4e\u5de5\u8d44\u804c\u5de5");
LowestSalEmp.addActionListener(e -> LowestSalEmpActionPerformed(e));
menu3.add(LowestSalEmp);
}
menuBar1.add(menu3);
//---- Config ----
Config.setText("\u4fee\u6539");
Config.addActionListener(e -> ConfigActionPerformed(e));
menuBar1.add(Config);
//---- Delete ----
Delete.setText("\u5220\u9664");
Delete.addActionListener(e -> DeleteActionPerformed(e));
menuBar1.add(Delete);
//---- Show ----
Show.setText("\u663e\u793a\u5168\u4f53\u5458\u5de5\u4fe1\u606f");
Show.addActionListener(e -> ShowActionPerformed(e));
menuBar1.add(Show);
}
setJMenuBar(menuBar1);
GroupLayout contentPaneLayout = new GroupLayout(contentPane);
contentPane.setLayout(contentPaneLayout);
contentPaneLayout.setHorizontalGroup(
contentPaneLayout.createParallelGroup()
.addGap(0, 508, Short.MAX_VALUE)
);
contentPaneLayout.setVerticalGroup(
contentPaneLayout.createParallelGroup()
.addGap(0, 332, Short.MAX_VALUE)
);
pack();
setLocationRelativeTo(getOwner());
// JFormDesigner - End of component initialization //GEN-END:initComponents
}
// JFormDesigner - Variables declaration - DO NOT MODIFY //GEN-BEGIN:variables
private JMenuBar menuBar1;
private JMenu menu3;
private JMenuItem Count;
private JMenuItem AvgSal;
private JMenuItem HighestSalEmp;
private JMenuItem LowestSalEmp;
private JButton Config;
private JButton Delete;
private JButton Show;
// JFormDesigner - End of variables declaration //GEN-END:variables
}
展示所有员工界面:
/*
* Created by JFormDesigner on Sun Apr 04 09:57:23 CST 2021
*/
package view;
import Controller.EmptyException;
import Controller.ShowInfo;
import model.Employee;
import java.awt.*;
import java.beans.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Vector;
import javax.swing.*;
import javax.swing.GroupLayout;
import javax.swing.table.*;
/**
* @author Brainrain
*/
public class ShowView extends JFrame {
public ShowView() {
initComponents();
SetTable();
}
private void initComponents() {
// JFormDesigner - Component initialization - DO NOT MODIFY //GEN-BEGIN:initComponents
Message = new JScrollPane();
table1 = new JTable();
//======== this ========
setTitle("\u804c\u5de5\u7ba1\u7406\u7cfb\u7edf");
Container contentPane = getContentPane();
//======== Message ========
{
//---- table1 ----
table1.setModel(new DefaultTableModel(
new Object[][] {
{null, null, "", null, null},
{null, null, null, null, null},
{null, null, null, null, null},
{null, null, null, null, null},
{null, null, null, null, null},
{null, null, null, null, null},
{null, null, null, null, null},
{null, null, null, null, null},
{null, null, null, null, null},
{null, null, null, null, null},
{null, null, null, null, null},
{null, null, null, null, null},
{null, null, null, null, null},
{null, null, null, null, null},
{null, null, null, null, null},
{null, null, null, null, null},
{null, null, null, null, null},
{null, null, null, null, null},
{null, null, null, null, null},
{null, null, null, null, null},
{null, null, null, null, null},
{null, null, null, null, null},
{null, null, null, null, null},
{null, null, null, null, null},
{null, null, null, null, null},
{null, null, null, null, null},
{null, null, null, null, null},
{null, null, null, null, null},
{null, null, null, null, null},
{null, null, null, null, null},
{null, null, null, null, null},
{null, null, null, null, null},
{null, null, null, null, null},
{null, null, null, null, null},
{null, null, null, null, null},
{null, null, null, null, null},
{null, null, null, null, null},
{null, null, null, null, null},
{null, null, null, null, null},
{null, null, null, null, null},
{null, null, null, null, null},
{null, null, null, null, null},
{null, null, null, null, null},
{null, null, null, null, null},
{null, null, null, null, null},
{null, null, null, null, null},
{null, null, null, null, null},
{null, null, null, null, null},
{null, null, null, null, null},
{null, null, null, null, null},
{null, null, null, null, null},
{null, null, null, null, null},
{null, null, null, null, null},
},
new String[] {
"\u5de5\u53f7", "\u59d3\u540d", "\u51fa\u751f\u65e5\u671f", "Email", "\u5de5\u8d44"
}
));
Message.setViewportView(table1);
}
GroupLayout contentPaneLayout = new GroupLayout(contentPane);
contentPane.setLayout(contentPaneLayout);
contentPaneLayout.setHorizontalGroup(
contentPaneLayout.createParallelGroup()
.addGroup(contentPaneLayout.createSequentialGroup()
.addContainerGap()
.addComponent(Message, GroupLayout.DEFAULT_SIZE, 501, Short.MAX_VALUE)
.addContainerGap())
);
contentPaneLayout.setVerticalGroup(
contentPaneLayout.createParallelGroup()
.addGroup(contentPaneLayout.createSequentialGroup()
.addContainerGap()
.addComponent(Message, GroupLayout.DEFAULT_SIZE, 381, Short.MAX_VALUE)
.addContainerGap())
);
pack();
setLocationRelativeTo(getOwner());
// JFormDesigner - End of component initialization //GEN-END:initComponents
SetTable();
}
// JFormDesigner - Variables declaration - DO NOT MODIFY //GEN-BEGIN:variables
private JScrollPane Message;
private JTable table1;
// JFormDesigner - End of variables declaration //GEN-END:variables
private void SetTable() //这里在显示职工信息的时候,用了Jtable,因此在这个地方设置jtable的内容
{
DefaultTableModel dft = (DefaultTableModel) table1.getModel();
dft.setRowCount(0);
try {
ArrayList<String[]>arrayList = ShowInfo.showinfo();
for(String[] emp : arrayList)
{
Vector<Object> a = new Vector<>();
Collections.addAll(a, emp);
dft.addRow(a);
}
} catch (EmptyException emptyException) {
// JOptionPane.showMessageDialog(this,"该员工表为空");
}
}
}
修改员工信息界面:
/*
* Created by JFormDesigner on Sat Apr 03 13:51:35 CST 2021
*/
package view;
import Controller.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.GroupLayout;
/**
* @author Brainrain
*/
public class UpdateView extends JFrame {
public UpdateView() {
initComponents();
}
private void UpdateButtonActionPerformed(ActionEvent e) { //与更新员工按钮进行交互
String no = EmpNO.getText();
Object obj = comboBox2.getSelectedItem();
String change_item = obj.toString();
String change_text = textField1.getText();
try {
Update.update(change_item,change_text,no);
JOptionPane.showMessageDialog(this,"修改成功");
} catch (NotFoundNoException notFoundNoException) {
JOptionPane.showMessageDialog(this,"找不到该员工号");
} catch (EmptyException emptyException) {
JOptionPane.showMessageDialog(this,"找不到该员工号");
} catch (EmailErrorException emailErrorException) {
JOptionPane.showMessageDialog(this,"邮箱格式错误");
} catch (BirthdayErrorException birthdayErrorException) {
JOptionPane.showMessageDialog(this,"生日格式错误");
} catch (NumFromatException numFromatException) {
JOptionPane.showMessageDialog(this,"请输入合法数字");
}
}
private void initComponents() {
// JFormDesigner - Component initialization - DO NOT MODIFY //GEN-BEGIN:initComponents
label6 = new JLabel();
UpdateButton = new JButton();
label4 = new JLabel();
EmpNO = new JTextField();
comboBox2 = new JComboBox<>();
textField1 = new JTextField();
//======== this ========
setTitle("\u804c\u5de5\u7ba1\u7406\u7cfb\u7edf");
Container contentPane = getContentPane();
//---- UpdateButton ----
UpdateButton.setText("\u66f4\u65b0");
UpdateButton.addActionListener(e -> UpdateButtonActionPerformed(e));
//---- label4 ----
label4.setText("\u5de5\u53f7");
//---- comboBox2 ----
comboBox2.setModel(new DefaultComboBoxModel<>(new String[] {
"\u8bf7\u9009\u62e9\u8981\u66f4\u6539\u7684\u5c5e\u6027",
"\u51fa\u751f\u65e5\u671f",
"\u7535\u5b50\u90ae\u7bb1",
"\u5de5\u8d44"
}));
GroupLayout contentPaneLayout = new GroupLayout(contentPane);
contentPane.setLayout(contentPaneLayout);
contentPaneLayout.setHorizontalGroup(
contentPaneLayout.createParallelGroup()
.addGroup(contentPaneLayout.createSequentialGroup()
.addGroup(contentPaneLayout.createParallelGroup()
.addGroup(contentPaneLayout.createSequentialGroup()
.addGap(40, 40, 40)
.addComponent(EmpNO, GroupLayout.PREFERRED_SIZE, 55, GroupLayout.PREFERRED_SIZE))
.addGroup(contentPaneLayout.createSequentialGroup()
.addContainerGap()
.addComponent(label4, GroupLayout.PREFERRED_SIZE, 51, GroupLayout.PREFERRED_SIZE)))
.addGroup(contentPaneLayout.createParallelGroup()
.addGroup(contentPaneLayout.createSequentialGroup()
.addGap(60, 60, 60)
.addGroup(contentPaneLayout.createParallelGroup()
.addGroup(contentPaneLayout.createSequentialGroup()
.addComponent(UpdateButton)
.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(GroupLayout.Alignment.TRAILING, contentPaneLayout.createSequentialGroup()
.addGap(0, 43, Short.MAX_VALUE)
.addComponent(label6, GroupLayout.PREFERRED_SIZE, 49, GroupLayout.PREFERRED_SIZE)
.addGap(171, 171, 171))))
.addGroup(contentPaneLayout.createSequentialGroup()
.addGap(30, 30, 30)
.addComponent(comboBox2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(textField1, GroupLayout.PREFERRED_SIZE, 108, GroupLayout.PREFERRED_SIZE)
.addContainerGap(18, Short.MAX_VALUE))))
);
contentPaneLayout.setVerticalGroup(
contentPaneLayout.createParallelGroup()
.addGroup(GroupLayout.Alignment.TRAILING, contentPaneLayout.createSequentialGroup()
.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(label6)
.addGap(92, 92, 92)
.addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(label4)
.addComponent(EmpNO, GroupLayout.PREFERRED_SIZE, 17, GroupLayout.PREFERRED_SIZE)
.addComponent(comboBox2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(textField1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addGap(50, 50, 50)
.addComponent(UpdateButton)
.addGap(47, 47, 47))
);
pack();
setLocationRelativeTo(getOwner());
// JFormDesigner - End of component initialization //GEN-END:initComponents
}
// JFormDesigner - Variables declaration - DO NOT MODIFY //GEN-BEGIN:variables
private JLabel label6;
private JButton UpdateButton;
private JLabel label4;
private JTextField EmpNO;
private JComboBox<String> comboBox2;
private JTextField textField1;
// JFormDesigner - End of variables declaration //GEN-END:variables
}
职工类(附带输入异常检测):
package model;
import Controller.BirthdayErrorException;
import Controller.EmailErrorException;
import Controller.NameException;
import Controller.NumFromatException;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
public class Employee implements Serializable {
private String No; //员工工号
private String Name; //员工姓名
private String BirthDay;//员工出生日期
private String Sal; //员工工资
private String Email; //员工邮箱
static int i=0; //员工入职的序号
public Employee() {
}
// public Employee(String name, String birthDay,String sal, String email) throws EmailErrorException, BirthdayErrorException, NumFromatException, NameException {
//
// if(name.isEmpty()) //检查名字是否为空
// {
// throw new NameException();
// }
// if(!check_email(email)) //检查邮箱格式
// {
// throw new EmailErrorException();
// }
// if(!check_birthday(birthDay))//检查出生日期格式
// {
// throw new BirthdayErrorException();
// }
// if(!check_sal(sal))//检查工资格式
// {
// throw new NumFromatException();
// }
// Name = name;
// BirthDay = birthDay;
// Sal = sal;
// Email=email;
//
// }
public static boolean check_sal(String sal) { //检查工资格式
String format = "^[0-9]*$";
return sal.matches(format);
}
public static boolean check_birthday(String birthDay) {//检查出生日期格式
String format="^\\d{4}-\\d{1,2}-\\d{1,2}";
return birthDay.matches(format);
}
public static boolean check_email(String email) {//检查邮箱格式
String format = "[a-zA-Z0-9_]{1,}[@][a-z0-9]{2,3}[.][a-z]{2,3}";
return email.matches(format);
}
public String getNo() {
return No;
}
public void setNo(String no) {
No = no;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public String getBirthDay() {
return BirthDay;
}
public void setBirthDay(String birthDay) {
BirthDay = birthDay;
}
public String getSal() {
return Sal;
}
public void setSal(String sal) {
Sal = sal;
}
public String getEmail() {
return Email;
}
public void setEmail(String email) {
Email = email;
}
@Override
public String toString() {
return "Employee{" +
"No='" + No + '\'' +
", Name='" + Name + '\'' +
", BirthDay='" + BirthDay + '\'' +
", Email='" + Email + '\'' +
'}';
}
}
// if(arrayList!=null && arrayList.size()!=0)
// {
// for(Employee e : arrayList)
// {
// if(o.equals(e.getNo().substring(6,7)))
// {
// i++;
// if(i<10)
// {
// o="0"+ Employee.i;
// }
// else o=Employee.i+"";
// break;
// }
// }
// }
注释好像忘删了。。。。。。懒得改了,就这样吧,不影响
数据库管理类(关于数据库的所有操作都在这里面啦)
package model;
import Controller.EmptyException;
import Controller.NotFoundNoException;
import java.sql.*;
import java.text.DecimalFormat;
import java.text.Format;
import java.text.NumberFormat;
import java.util.ArrayList;
public class DataBase {
Connection conn = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
public String Avg() throws EmptyException {
double avg=0;
try {
preparedStatement = conn.prepareStatement("select avg(sal) from emp");
resultSet = preparedStatement.executeQuery();
if(resultSet.next())
{
avg = resultSet.getDouble(1);
DecimalFormat df = new DecimalFormat("#.00");
return df.format(avg);
}
else{
throw new EmptyException();
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
if(preparedStatement!=null)
{
try {
preparedStatement.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if(resultSet!=null)
{
try {
resultSet.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
return null;
}
public String Min() throws EmptyException {
String rest="";
try {
preparedStatement = conn.prepareStatement("select * from emp where sal=(select min(sal) from emp);");
resultSet = preparedStatement.executeQuery();
ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
int column = resultSetMetaData.getColumnCount();
if(resultSet.next())
{
for(int i=1;i<=column;i++)
{
String s = resultSetMetaData.getColumnName(i);
if(s.equals("No"))s="工号";
if(s.equals("Name"))s="姓名";
if(s.equals("Birthday"))s="出生日期";
if(s.equals("Email"))s="Email";
if(s.equals("Sal"))s="工资";
rest+=s+":"+resultSet.getString(i)+"\n";
}
}
if(rest.equals(""))
{
throw new EmptyException();
}
else return rest;
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
if(preparedStatement!=null)
{
try {
preparedStatement.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if(resultSet!=null)
{
try {
resultSet.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
return rest;
}
public String Highest() throws EmptyException {
String rest="";
try {
preparedStatement = conn.prepareStatement("select * from emp where sal=(select max(sal) from emp);");
resultSet = preparedStatement.executeQuery();
ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
int column = resultSetMetaData.getColumnCount();
if(resultSet.next())
{
for(int i=1;i<=column;i++)
{
String s = resultSetMetaData.getColumnName(i);
if(s.equals("No"))s="工号";
if(s.equals("Name"))s="姓名";
if(s.equals("Birthday"))s="出生日期";
if(s.equals("Email"))s="Email";
if(s.equals("Sal"))s="工资";
rest+=s+":"+resultSet.getString(i)+"\n";
}
}
if(rest.equals(""))
{
throw new EmptyException();
}
else return rest;
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
if(preparedStatement!=null)
{
try {
preparedStatement.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if(resultSet!=null)
{
try {
resultSet.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
return rest;
}
public int count()
{
int res=0;
try {
preparedStatement = conn.prepareStatement("select count(*) from emp");
resultSet = preparedStatement.executeQuery();
if(resultSet.next())
{
res = resultSet.getInt(1);
return res;
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
if(preparedStatement!=null)
{
try {
preparedStatement.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if(resultSet!=null)
{
try {
resultSet.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
return res;
}
public void delete(int No) throws NotFoundNoException {
boolean success= false;
try {
preparedStatement = conn.prepareStatement("delete from emp where No=?");
preparedStatement.setInt(1,No);
success = preparedStatement.execute();
} catch (SQLException throwables) {
throw new NotFoundNoException("");
}
}
public void update(String item,String text,int No) throws NotFoundNoException {
try {
String s=null;
if(item.equals("工资"))
{
preparedStatement = conn.prepareStatement("update emp set sal"+"=? where no=?");
preparedStatement.setInt(2,No);
preparedStatement.setInt(1,Integer.parseInt(text));
preparedStatement.execute();
}else if(item.equals("电子邮箱"))
{
preparedStatement = conn.prepareStatement("update emp set email"+"=? where no=?");
preparedStatement.setInt(2,No);
preparedStatement.setString(1,text);
preparedStatement.execute();
}
else{
preparedStatement = conn.prepareStatement("update emp set birthday"+"=? where no=?");
preparedStatement.setInt(2,No);
preparedStatement.setString(1,text);
preparedStatement.execute();
}
} catch (SQLException throwables) {
throwables.printStackTrace();
throw new NotFoundNoException("");
}
}
public void insert(String Name,String Birthday,String Email,int Sal)
{
try {
preparedStatement = conn.prepareStatement("select max(no) from emp");
resultSet = preparedStatement.executeQuery();
ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
int column = resultSetMetaData.getColumnCount();
int id=0;
if(resultSet.next())
{
for(int i=1;i<=column;i++)
{
id= resultSet.getInt(1);
}
}
//更改autoincrement初始值
preparedStatement = conn.prepareStatement("ALTER TABLE emp AUTO_INCREMENT =?");
preparedStatement.setInt(1,id+1);
preparedStatement.execute();
preparedStatement = conn.prepareStatement("insert into emp (name,birthday,email,sal) values(?,?,?,?)");
preparedStatement.setString(1,Name);
preparedStatement.setString(2,Birthday);
preparedStatement.setString(3,Email);
preparedStatement.setInt(4,Sal);
preparedStatement.execute();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
public ArrayList<String[]> query_ALL()
{
ArrayList<String[]> arrayList = new ArrayList<>();
try {
preparedStatement = conn.prepareStatement("select * from emp");
resultSet = preparedStatement.executeQuery();
ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
int column = resultSetMetaData.getColumnCount();
int k = 0;
while (resultSet.next())
{
String[]strs = new String[5];
k=0;
for(int i=1;i<=column;i++)
{
strs[k]=resultSet.getString(i);
k++;
}
arrayList.add(strs);
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
if(preparedStatement!=null)
{
try {
preparedStatement.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if(resultSet!=null)
{
try {
resultSet.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
return arrayList;
}
public String[] query_people(int No) throws NotFoundNoException {
String []strs = new String[7];
int flag=0;
try {
preparedStatement = conn.prepareStatement("select * from emp where No=?");
preparedStatement.setInt(1,No);
resultSet = preparedStatement.executeQuery();
ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
int column = resultSetMetaData.getColumnCount();
if(resultSet.next())
{
for(int i=1;i<=column;i++)
{
strs[i]=resultSet.getString(i);
}
flag=1;
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
if(preparedStatement!=null)
{
try {
preparedStatement.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if(resultSet!=null)
{
try {
resultSet.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
if(flag==0)
{
throw new NotFoundNoException("");
}
else{
return strs;
}
}
public DataBase()
{
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/employee","root","333");
} catch (Exception e) {
e.printStackTrace();
}
}
}
这段代码没有写注释,主要是都是些基本操作,应该一看就懂吧(ε=ε=ε=┏(゜ロ゜;)┛)
其余的代码,都是调用上面的数据库类中的功能,就不说了吧!!!
package Controller;
public class BirthdayErrorException extends Exception{
public BirthdayErrorException (){}
public BirthdayErrorException (String s){super(s);}
}
package Controller;
public class BirthdayErrorException extends Exception{
public BirthdayErrorException (){}
public BirthdayErrorException (String s){super(s);}
}
package Controller;
import model.DataBase;
public class Count {
public static int count() throws EmptyException {
return new DataBase().count();
}
}
package Controller;
import model.DataBase;
public class Delete {
public static void delete(String No) throws NotFoundNoException {
int nos = Integer.parseInt(No);
new DataBase().query_people(nos);
new DataBase().delete(nos);
}
}
package Controller;
public class EmailErrorException extends Exception{
public EmailErrorException(){}
public EmailErrorException(String s)
{super(s);}
}
package Controller;
public class EmpNullException extends Exception{
public EmpNullException (){}
public EmpNullException(String s){super(s);}
}
package Controller;
public class EmptyException extends Exception {
public EmptyException(){}
public EmptyException(String s)
{super(s);}
}
package Controller;
import model.DataBase;
public class HighestSal {
public static String high() throws EmptyException {
return new DataBase().Highest();
}
}
package Controller;
import model.DataBase;
public class LowestSal {
public static String low() throws EmptyException {
return new DataBase().Min();
}
}
package Controller;
public class NameException extends Exception{
public NameException (){}
public NameException(String s){super(s);}
}
package Controller;
public class NotFoundNoException extends Exception {
public NotFoundNoException(){}
public NotFoundNoException(String s)
{super(s);}
}
package Controller;
public class NumFromatException extends Exception{
public NumFromatException(){}
public NumFromatException(String s){super(s);}
}
package Controller;
import model.DataBase;
import java.util.ArrayList;
public class ShowInfo {
public static ArrayList<String[]> showinfo() throws EmptyException {
return new DataBase().query_ALL();
}
}
package Controller;
import model.DataBase;
import static model.Employee.*;
public class Update {
public Update() {
}
//更新员工信息
public static void update(String item, String text,String no) throws NotFoundNoException, EmptyException, EmailErrorException, BirthdayErrorException, NumFromatException {
if(item.equals("电子邮箱")&&!text.isEmpty()&&!check_email(text)) //如果用户输入的邮箱不合法,则抛出异常
{
throw new EmailErrorException();
}
if(item.equals("出生日期")&&!text.isEmpty()&&!check_birthday(text)) //用户输入生日日期格式错误抛出异常
{
throw new BirthdayErrorException();
}
if(item.equals("工资")&&!text.isEmpty()&&!check_sal(text)) //如果用户输入的工资不是数字的话抛出异常
{
throw new NumFromatException();
}
new DataBase().update(item,text,Integer.parseInt(no));
}
}
package Controller;
import model.DataBase;
import model.Employee;
import static model.Employee.*;
public class Write {
Employee employee;
public Write() {
}
public Write(String Name,String Birthday,String Email,String Sal) throws EmpNullException, EmailErrorException, BirthdayErrorException, NumFromatException, NameException {
if(!Email.isEmpty()&&!check_email(Email)) //如果用户输入的邮箱不合法,则抛出异常
{
throw new EmailErrorException();
}
if(Name.isEmpty())
{
throw new NameException();
}
if(!Birthday.isEmpty()&&!check_birthday(Birthday)) //用户输入生日日期格式错误抛出异常
{
throw new BirthdayErrorException();
}
if(!Sal.isEmpty()&&!check_sal(Sal)) //如果用户输入的工资不是数字的话抛出异常
{
throw new NumFromatException();
}
new DataBase().insert(Name,Birthday,Email,Integer.parseInt(Sal));
}
}
大概总结一下吧:这次做的东西也没有用什么高端的算法,就是一些基本操作,增删改查什么的,总而言之并不难,把思路捋顺了就行了。另外,想要参考的同学(应该没有想看我代码的人吧,毕竟代码写得真的很烂)要注意了啊,在连接数据库的时候要把那一段代码,改成你自己的用户名和密码,还要改端口地址啊,千万别忘了!!!!
希望老师能多给我点分吧55555