武汉工程大学计算机科学与工程学院 课程设计报告
this.add(jLabel6); this.add(jTextField5); this.add(jButton1); }
// 点击按钮事件
public void actionPerformed(ActionEvent e) { // 获取用户输入的信息
String EmployeeID = jTextField1.getText(); String EmployeeName = jTextField2.getText();
String EmployeeSex = \if (jRadioButton1.isSelected()) EmployeeSex += \男\if (jRadioButton2.isSelected())
EmployeeSex += \女\
String EmployeeAge = jTextField3.getText(); String EmployeeAddress = jTextField4.getText(); String EmployeePhone = jTextField5.getText(); try {
// 设置日期格式
st.execute(\
// 利用st对象执行SQL语句,进行插入操作
st.executeUpdate(\into Employee values('\+ EmployeeID + \+ EmployeeName + \ + \+ EmployeeAge + \+ EmployeeAddress + \+ EmployeePhone + \
// 利用消息对话框提示职工添加成功
JOptionPane.showMessageDialog(this, \职工添加成功!\// 清空文本行的内容 jTextField1.setText(\
jTextField2.setText(\ jTextField3.setText(\ jTextField4.setText(\ jTextField5.setText(\} catch (Exception ex) {
// 利用消息对话框提示异常的信息
JOptionPane.showMessageDialog(this, \职工添加失败!\ ex.printStackTrace(); }
- 12 -
武汉工程大学计算机科学与工程学院 课程设计报告
} }
(4) 修改员工信息 ModifyEmployee.java,该类中主要使用事件的动作和侦听方法来实现对按钮的侦听,设置按钮和文本框方便信息修改,判断输入信息的准确性,提示用户,同时调用数据库,以完成员工信息的修改。
public class ModifyEmployee extends JPanel implements ActionListener {
//声明连接数据库对象 Connection con; //声明SQL语句对象 Statement st;
//创建组件对象:标签、文本行、单选
JLabel jLabel1=new JLabel(\请输入职工的编号:\JTextField jTextField1=new JTextField();
JButton jButton1=new JButton(\查找该职工\JLabel jLabel2=new JLabel(\职工编号:\JTextField jTextField2=new JTextField(); JLabel jLabel3=new JLabel(\职工姓名:\JTextField jTextField3=new JTextField(); JLabel jLabel4=new JLabel(\职工性别:\JTextField jTextField4=new JTextField(); JLabel jLabel5=new JLabel(\职工年龄:\JTextField jTextField5=new JTextField(); JLabel jLabel6=new JLabel(\家庭住址:\JTextField jTextField6=new JTextField(); JLabel jLabel7=new JLabel(\联系电话:\JTextField jTextField7=new JTextField(); JButton jButton2=new JButton(\修改职工信息\//构造方法
public ModifyEmployee() {
try{
//调用初始化方法 jbInit(); }
catch(Exception exception){
- 13 -
武汉工程大学计算机科学与工程学院 课程设计报告
exception.printStackTrace(); } }
//面板初始化方法
private void jbInit() throws Exception {
//连接数据库
con=DBConnect.getConn(); st=con.createStatement(); //框架的布局
this.setLayout(null); //设置各组件的大小
jLabel1.setFont(new java.awt.Font(\楷体\jLabel1.setBounds(new Rectangle(40,15,140,30));
jTextField1.setBounds(new Rectangle(185,15,120,30)); jButton1.setBounds(new Rectangle(110,55,150,30)); jLabel2.setFont(new java.awt.Font(\楷体\jLabel2.setBounds(new Rectangle(20,105,80,30));
jTextField2.setBounds(new Rectangle(100,105,80,30));
jLabel3.setFont(new java.awt.Font(\楷体\jLabel3.setBounds(new Rectangle(190,105,80,30)); jTextField3.setBounds(new Rectangle(270,105,80,30)); jLabel4.setFont(new java.awt.Font(\楷体\jLabel4.setBounds(new Rectangle(20,145,80,30));
jTextField4.setBounds(new Rectangle(100,145,80,30));
jLabel5.setFont(new java.awt.Font(\楷体\jLabel5.setBounds(new Rectangle(190,145,80,30)); jTextField5.setBounds(new Rectangle(270,145,80,30)); jLabel6.setFont(new java.awt.Font(\楷体\jLabel6.setBounds(new Rectangle(20,185,80,30));
jTextField6.setBounds(new Rectangle(100,185,80,30));
jLabel7.setFont(new java.awt.Font(\楷体\jLabel7.setBounds(new Rectangle(190,185,80,30)); jTextField7.setBounds(new Rectangle(270,185,80,30)); jButton2.setBounds(new Rectangle(120,240,150,30)); //添加按钮动作事件
jButton1.addActionListener(this); jButton2.addActionListener(this); //添加组件到面板
- 14 -
武汉工程大学计算机科学与工程学院 课程设计报告
}
this.add(jLabel1); this.add(jTextField1); this.add(jButton1); this.add(jLabel2); this.add(jTextField2);
jTextField2.setEditable(false); this.add(jLabel3); this.add(jTextField3); this.add(jLabel4); this.add(jTextField4); this.add(jLabel5); this.add(jTextField5); this.add(jLabel6); this.add(jTextField6); this.add(jLabel7); this.add(jTextField7); this.add(jButton2);
//点击按钮事件
public void actionPerformed(ActionEvent actionevent)
{ //点击“查找该职工”按钮 if(actionevent.getSource()==jButton1) { try{ //利用st对象执行SQL语句,返回结果集对象 ResultSet rs=st.executeQuery(\* from Employee where 职工编号='\
//处理结果集 if(rs.next())
{
jTextField2.setText(rs.getString(\职工编号\ jTextField3.setText(rs.getString(\职工姓名\ jTextField4.setText(rs.getString(\职工性别\ jTextField5.setText(rs.getString(\职工年龄\ jTextField6.setText(rs.getString(\家庭住址\ jTextField7.setText(rs.getString(\联系电话\ } else
- 15 -
武汉工程大学计算机科学与工程学院 课程设计报告
{ JOptionPane.showMessageDialog(this,\没有这个工号的职工!\ } } catch(Exception ex){ //利用消息对话框提示查询失败 JOptionPane.showMessageDialog(this,\职工信息修改失败!\ } }
//点击“修改工资”按钮
if(actionevent.getSource()==jButton2) {
//获取用户更改的工资 try{
姓性年
名别龄址话
//利用st对象执行SQL语句,进行插入操作 st.executeUpdate(\Employee set 职工='\职工编号='\ st.executeUpdate(\Employee set 职工='\职工编号='\ st.executeUpdate(\Employee set 职工
='\职工编号='\ st.executeUpdate(\Employee set 家庭住='\职工编号='\ st.executeUpdate(\Employee set 联系电='\职工编号='\ //利用消息对话框提示职工信息修改成功 JOptionPane.showMessageDialog(this,\职工信息修改成功!\ } catch(Exception ex){ //利用消息对话框提示异常的信息
} }
}
JOptionPane.showMessageDialog(this,\职工信息修改失败!\ex.printStackTrace(); }
(5) 删除员工DeleteEmployee.java,该类中主要使用事件的动作和侦听方法来实现对按
- 16 -