java员工管理系统mysql桌面应用
时间: 2023-11-20 14:57:35 浏览: 80
以下是一个Java员工管理系统的示例,使用MySQL作为数据库,Swing作为界面框架:
1. 登录模块
```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
public class Login extends JFrame implements ActionListener {
JLabel l1, l2;
JTextField tf1;
JPasswordField pf2;
JButton b1, b2;
Login() {
setTitle("登录");
setVisible(true);
setSize(800, 800);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(null);
l1 = new JLabel("用户名:");
l1.setBounds(200, 150, 100, 30);
add(l1);
l2 = new JLabel("密码:");
l2.setBounds(200, 200, 100, 30);
add(l2);
tf1 = new JTextField();
tf1.setBounds(300, 150, 150, 30);
add(tf1);
pf2 = new JPasswordField();
pf2.setBounds(300, 200, 150, 30);
add(pf2);
b1 = new JButton("登录");
b1.setBounds(200, 250, 100, 30);
b1.addActionListener(this);
add(b1);
b2 = new JButton("取消");
b2.setBounds(300, 250, 100, 30);
b2.addActionListener(this);
add(b2);
}
public void actionPerformed(ActionEvent ae) {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/kq", "root", "");
Statement stmt = con.createStatement();
String sql = "select * from admin where username='" + tf1.getText() + "' and password='" + pf2.getText() + "'";
ResultSet rs = stmt.executeQuery(sql);
if (rs.next()) {
JOptionPane.showMessageDialog(null, "登录成功");
new Menu();
dispose();
} else {
JOptionPane.showMessageDialog(null, "用户名或密码错误");
}
con.close();
} catch (Exception e) {
System.out.println(e);
}
}
public static void main(String[] args) {
new Login();
}
}
```
2. 职工管理模块
```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
public class Employee extends JFrame implements ActionListener {
JLabel l1, l2, l3, l4, l5, l6, l7, l8, l9, l10;
JTextField tf1, tf2, tf3, tf4, tf5, tf6, tf7, tf8, tf9, tf10;
JButton b1, b2, b3, b4, b5, b6, b7, b8, b9, b10;
Employee() {
setTitle("职工管理");
setVisible(true);
setSize(800, 800);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(null);
l1 = new JLabel("职工编号:");
l1.setBounds(50, 50, 100, 30);
add(l1);
tf1 = new JTextField();
tf1.setBounds(150, 50, 150, 30);
add(tf1);
l2 = new JLabel("姓名:");
l2.setBounds(50, 100, 100, 30);
add(l2);
tf2 = new JTextField();
tf2.setBounds(150, 100, 150, 30);
add(tf2);
l3 = new JLabel("性别:");
l3.setBounds(50, 150, 100, 30);
add(l3);
tf3 = new JTextField();
tf3.setBounds(150, 150, 150, 30);
add(tf3);
l4 = new JLabel("年龄:");
l4.setBounds(50, 200, 100, 30);
add(l4);
tf4 = new JTextField();
tf4.setBounds(150, 200, 150, 30);
add(tf4);
l5 = new JLabel("职位:");
l5.setBounds(50, 250, 100, 30);
add(l5);
tf5 = new JTextField();
tf5.setBounds(150, 250, 150, 30);
add(tf5);
l6 = new JLabel("部门:");
l6.setBounds(50, 300, 100, 30);
add(l6);
tf6 = new JTextField();
tf6.setBounds(150, 300, 150, 30);
add(tf6);
l7 = new JLabel("入职时间:");
l7.setBounds(50, 350, 100, 30);
add(l7);
tf7 = new JTextField();
tf7.setBounds(150, 350, 150, 30);
add(tf7);
l8 = new JLabel("联系电话:");
l8.setBounds(50, 400, 100, 30);
add(l8);
tf8 = new JTextField();
tf8.setBounds(150, 400, 150, 30);
add(tf8);
l9 = new JLabel("邮箱:");
l9.setBounds(50, 450, 100, 30);
add(l9);
tf9 = new JTextField();
tf9.setBounds(150, 450, 150, 30);
add(tf9);
l10 = new JLabel("地址:");
l10.setBounds(50, 500, 100, 30);
add(l10);
tf10 = new JTextField();
tf10.setBounds(150, 500, 150, 30);
add(tf10);
b1 = new JButton("添加");
b1.setBounds(350, 50, 100, 30);
b1.addActionListener(this);
add(b1);
b2 = new JButton("修改");
b2.setBounds(350, 100, 100, 30);
b2.addActionListener(this);
add(b2);
b3 = new JButton("删除");
b3.setBounds(350, 150, 100, 30);
b3.addActionListener(this);
add(b3);
b4 = new JButton("查询");
b4.setBounds(350, 200, 100, 30);
b4.addActionListener(this);
add(b4);
b5 = new JButton("显示");
b5.setBounds(350, 250, 100, 30);
b5.addActionListener(this);
add(b5);
b6 = new JButton("清空");
b6.setBounds(350, 300, 100, 30);
b6.addActionListener(this);
add(b6);
b7 = new JButton("退出");
b7.setBounds(350, 350, 100, 30);
b7.addActionListener(this);
add(b7);
}
public void actionPerformed(ActionEvent ae) {
if (ae.getSource() == b1) {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/kq", "root", "");
Statement stmt = con.createStatement();
String sql = "insert into employee values('" + tf1.getText() + "','" + tf2.getText() + "','" + tf3.getText() + "','" + tf4.getText() + "','" + tf5.getText() + "','" + tf6.getText() + "','" + tf7.getText() + "','" + tf8.getText() + "','" + tf9.getText() + "','" + tf10.getText() + "')";
stmt.executeUpdate(sql);
JOptionPane.showMessageDialog(null, "添加成功");
con.close();
} catch (Exception e) {
System.out.println(e);
}
} else if (ae.getSource() == b2) {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/kq", "root", "");
Statement stmt = con.createStatement();
String sql = "update employee set name='" + tf2.getText() + "',sex='" + tf3.getText() + "',age='" + tf4.getText() + "',position='" + tf5.getText() + "',department='" + tf6.getText() + "',entry_time='" + tf7.getText() + "',phone='" + tf8.getText() + "',email='" + tf9.getText() + "',address='" + tf10.getText() + "' where id='" + tf1.getText() + "'";
stmt.executeUpdate(sql);
JOptionPane.showMessageDialog(null, "修改成功");
con.close();
} catch (Exception e) {
System.out.println(e);
}
} else if (ae.getSource() == b3) {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/kq", "root", "");
Statement stmt = con.createStatement();
String sql = "delete from employee where id='" + tf1.getText() + "'";
stmt.executeUpdate(sql);
JOptionPane.showMessageDialog(null, "删除成功");
con.close();
} catch (Exception e) {
System.out.println(e);
}
} else if (ae.getSource() == b4) {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/kq", "root", "");
Statement stmt = con.createStatement();
String sql = "select * from employee where id='" + tf1.getText() + "'";
ResultSet rs = stmt.executeQuery(sql);
if (rs.next()) {
tf2.setText(rs.getString("name"));
tf3.setText(rs.getString("sex"));
tf4.setText(rs.getString("age"));
tf5.setText(rs.getString("position"));
tf6.setText(rs.getString("department"));
tf7.setText(rs.getString("entry_time"));
tf8.setText(rs.getString("phone"));
tf9.setText(rs.getString("email"));
tf10.setText(rs.getString("address"));
} else {
JOptionPane.showMessageDialog(null, "没有找到该职工");
}
con.close();
} catch (Exception e) {
System.out.println(e);
}
} else if (ae.getSource() == b5) {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/kq", "root", "");
Statement stmt = con.createStatement();
String sql = "select * from employee";
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
System.out.println(rs.getString("id") + " " + rs.getString("name") + " " + rs.getString("sex") + " " + rs.getString("age") + " " + rs.getString("position") + " " + rs.getString("department") + " " + rs.getString("entry_time") + " " + rs.getString("phone") + " " + rs.getString("email") + " " + rs.getString("address"));
}
con.close();
} catch (Exception e) {
System.out.println(e);
}
} else if (ae.getSource() == b6) {
tf1.setText("");
tf2.setText("");
tf3.setText("");
tf4.setText("");
tf5.setText("");
tf6.setText("");
tf7.setText("");
tf8.setText("");
tf9.setText("");
tf10.setText("");
} else if (ae.getSource() == b7) {
dispose();
}
}
public static void main(String[] args) {
new Employee();
}
}
```
3. 出勤信息模块
```
阅读全文