您的位置:首页 > 产品设计 > UI/UE

GUI开发和JDBC编程实现员工管理

2020-07-14 05:59 561 查看

GUI开发和JDBC编程实现员工管理

##实验目的
1、综合运用JAVA理论知识和编程技巧,设计和开发图形用户界面的员工注册和查询界面,实现良好的交互界面和JDBC数据库编程。

##实验内容和步骤
设计并开发员工管理系统的两个界面:
1、“员工登记”界面,用于录入一个新入职员工信息,包括员工编号、姓名、性别、出生日期、学历、所属部门、职务、工资、员工状态等。单击“保存”按钮后,将在数据库中新增一条员工信息(如员工编号重复,提示“该员工编号已存在”!)


2、“查询功能”界面:可按编号、姓名、部门等查询数据库中所有员工信息并以列表方式显示出来。


3.员工信息的数据库表可采用Microsoft ACCESS和Mysql、SqlServer等。
说明:程序要具备良好的交互性和容错性,例如要给出提示信息表明必填信息;员工编号姓名重复,也要提示信息重复。

##代码部分
hjcMain.java

package hjcPackage;

public class hjcMain {
public static void main(String[] args){

//员工登记界面设置
hjcRegister rView = new hjcRegister();
rView.setBounds(300,300,600,400);
rView.setTitle("员工登记");

//查询界面设置
hjcSearch sView = new hjcSearch();
sView.setBounds(100, 100, 900, 400);
sView.setTitle("查询功能");

}
}

hjcRegister.java

//员工登记类
package hjcPackage;

import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

public class hjcRegister extends JFrame{
/**
*
*/
private static final long serialVersionUID = 1L;

//数据库配置
Connection con;
Statement sql;
ResultSet rs;
String hjcUri = "jdbc:mysql://121.36.14.14:3306/gpnu_java_test?" +
"username=gpnu_user&password=123456&useSSL=true";
String hjcUser = "gpnu_user";
String hjcpassword = "123456";

//确定按钮
JButton sureButton = new JButton("确定");

//取消按钮
JButton exitButton = new JButton("取消");

//输入文本框
JTextField staff_id = new JTextField(20);
JTextField staff_name = new JTextField(20);
JTextField staff_birth = new JTextField(20);
JTextField staff_post = new JTextField(20);
JTextField staff_salary = new JTextField(20);

//内容提示文本
JLabel no = new JLabel("员工编号:",JLabel.CENTER);
JLabel name = new JLabel("员工姓名:",JLabel.CENTER);
JLabel sex = new JLabel("员工性别:",JLabel.CENTER);
JLabel birthday = new JLabel("出生日期:",JLabel.CENTER);
JLabel de_no =new JLabel("部门编号:",JLabel.CENTER);
JLabel post = new JLabel("员工职务:",JLabel.CENTER);
JLabel salary = new JLabel("员工工资:",JLabel.CENTER);
JLabel education = new JLabel("学历编号:",JLabel.CENTER);
JLabel state = new JLabel("员工状态:",JLabel.CENTER);

//列表框
JComboBox<String> staff_sex = new JComboBox<String>();
JComboBox<String> department_id = new JComboBox<String>();
JComboBox<String> education_id = new JComboBox<String>();
JComboBox<String> staff_state = new JComboBox<String>();

hjcRegister(){

//文本框字体大小设置
staff_id.setFont(new Font("宋体",Font.BOLD,20));
staff_name.setFont(new Font("宋体",Font.BOLD,20));
staff_birth.setFont(new Font("宋体",Font.BOLD,20));
staff_birth.setText("如:2018/10/23");
staff_post.setFont(new Font("宋体",Font.BOLD,20));
staff_salary.setFont(new Font("宋体",Font.BOLD,20));

//内容提示文本字体设置
no.setFont(new Font("宋体",Font.BOLD,20));
name.setFont(new Font("宋体",Font.BOLD,20));
sex.setFont(new Font("宋体",Font.BOLD,20));
birthday.setFont(new Font("宋体",Font.BOLD,20));
de_no.setFont(new Font("宋体",Font.BOLD,20));
post.setFont(new Font("宋体",Font.BOLD,20));
salary.setFont(new Font("宋体",Font.BOLD,20));
education.setFont(new Font("宋体",Font.BOLD,20));
state.setFont(new Font("宋体",Font.BOLD,20));

//设置框架10行2列
GridLayout grid = new GridLayout(10,2);
setLayout(grid);

this.add(no);					this.add(staff_id);
this.add(name);				this.add(staff_name);
this.add(sex);					this.add(staff_sex);
this.add(birthday);			this.add(staff_birth);
this.add(de_no);				this.add(department_id);
this.add(post);				this.add(staff_post);
this.add(salary);				this.add(staff_salary);
this.add(education);			this.add(education_id);
this.add(state); 				this.add(staff_state);
this.add(sureButton);			this.add(exitButton);

staff_sex.addItem("男");
staff_sex.addItem("女");

String [] departmen_array = {"销售部门","设计部门","组织部门","人力资源部门","策划部门","秘书部门","财务部门"};
for (int i = 0; i < departmen_array.length; i++) {
department_id.addItem(departmen_array[i]);
}

String [] educatin_array = {"小学","初中","高中","大专","本科","硕士","博士"};
for (int i = 0; i < educatin_array.length; i++) {
education_id.addItem(educatin_array[i]);
}

staff_state.addItem("已在职");
staff_state.addItem("已离职");

sureButton.addActionListener(new ActionSureButton());

exitButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "单击确定退出");
System.exit(0);
}
});

this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
this.setVisible(true);
}

public class ActionSureButton  implements ActionListener{

String sID;
String sNAME;
String sSEX;
String sBIRTH;
String dID;
String sPOST;
String sSALARY;
String eID;
String sSTATE;

@Override
public void actionPerformed(ActionEvent e){
try {
con = DriverManager.getConnection(hjcUri,hjcUser,hjcpassword);

}catch(SQLException ee) {
System.out.println("异常原因:"+ee);
JOptionPane.showMessageDialog(null,"连接失败",null,JOptionPane.INFORMATION_MESSAGE);
return;
}

try {
sql = con.createStatement();

}catch(SQLException ee) {
System.out.println("异常原因:"+ee);
JOptionPane.showMessageDialog(null,"获取数据失败",null,JOptionPane.INFORMATION_MESSAGE);
return;
}

sID = staff_id.getText().trim();
sNAME = staff_name.getText().trim();
sSEX = staff_sex.getSelectedItem().toString();
sBIRTH = staff_birth.getText().trim();
dID = department_id.getSelectedItem().toString();
sPOST = staff_post.getText().trim();
sSALARY = staff_salary.getText().trim();
eID =  education_id.getSelectedItem().toString();
sSTATE = staff_state.getSelectedItem().toString();
String hjcSql = "insert into hjcTable(sID,sNAME,sSEX,sBIRTH," +
"dID,sPOST,sSALARY,eID,sSTATE) values " +
"('"+sID+"','"+sNAME+"','"+sSEX+"','"+sBIRTH+"','"+dID+"" +
"','"+sPOST+"','"+sSALARY+"','"+eID+"','"+sSTATE+"')";

if(sID.equals("")||sNAME.equals("")||sSEX.equals("")||
sBIRTH.equals("")||dID.equals("")||sPOST.equals("")||
sSALARY.equals("")||eID.equals("")||sSTATE.equals("")) {
JOptionPane.showMessageDialog(null,"添加数据失败,请确认信息是否都有填写!",
null,JOptionPane.INFORMATION_MESSAGE);
return;
}
System.out.println(hjcSql);
try {
rs = sql.executeQuery("SELECT * FROM hjcTable");
while(rs.next()) {
if(rs.getString(1).equals(sID)) {
JOptionPane.showMessageDialog(null,"该员工编号已存在,请重新编号!",
null,JOptionPane.INFORMATION_MESSAGE);
return;
}
}
@SuppressWarnings("unused")
int u = sql.executeUpdate(hjcSql);
JOptionPane.showMessageDialog(null,"添加成功!",null,JOptionPane.INFORMATION_MESSAGE);

} catch (SQLException ee) {
System.out.println("异常原因:"+ee);
JOptionPane.showMessageDialog(null,"添加数据失败!",null,JOptionPane.INFORMATION_MESSAGE);
ee.printStackTrace();
}

}
}
}

hjcSearch.java

//查询功能类
package hjcPackage;
import java.awt.Button;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;

import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

public class hjcSearch extends JFrame{

/**
*
*/
private static final long serialVersionUID = 1L;
//数据库基础配置
Connection con;
Statement sql;
ResultSet rs;
String hjcUri = "jdbc:mysql://121.36.14.14:3306/gpnu_java_test?" +
"username=gpnu_user&password=123456&useSSL=true";
String hjcUser = "gpnu_user";
String hjcpassword = "123456";

// 创建表格
String[][] dataTable = new String[100][100];
String[] dataTitle = {"员工编号","员工姓名","员工性别","出生日期","部门编号","职务","工资","学历编号","状态"};
DefaultTableModel tableModel = new DefaultTableModel(dataTable, dataTitle);
JTable table = new JTable(tableModel);

JScrollPane sp = new JScrollPane(table);

Button selectButton= new Button("查询");
JComboBox<String> selectBox = new JComboBox<>();
JTextField inputTextField = new JTextField(25);

hjcSearch(){

FlowLayout flow = new FlowLayout();
setLayout(flow);

//设置输入框、列表框、按钮大小
inputTextField.setPreferredSize(new Dimension(250,30));
selectButton.setPreferredSize(new Dimension(250,30));

selectBox.setPreferredSize(new Dimension(250,30));
selectBox.addItem("员工编号");
selectBox.addItem("员工姓名");
selectBox.addItem("员工性别");
selectBox.addItem("出生日期");
selectBox.addItem("部门编号");
selectBox.addItem("员工职务");
selectBox.addItem("员工薪水");
selectBox.addItem("学历编号");
selectBox.addItem("员工状态");

//设置表格大小
sp.setPreferredSize(new Dimension(800,400));
table.setRowHeight(30);
this.add(inputTextField);
this.add(selectBox);
this.add(selectButton);
this.add(sp);

selectButton.addActionListener(new ActionSelectButton());
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
this.setVisible(true);
}

public class ActionSelectButton  implements ActionListener{
public void actionPerformed(ActionEvent e){
try {
con = DriverManager.getConnection(hjcUri,hjcUser,hjcpassword);
sql = con.createStatement();

}catch(SQLException ee) {
System.out.println("异常原因:"+ee);
JOptionPane.showMessageDialog(null,"连接失败",null,JOptionPane.INFORMATION_MESSAGE);
return;
}

String getInputTextField = inputTextField.getText().trim();
String comBoxListStr = selectBox.getSelectedItem().toString();
String sqlSearch = "";
int i = 0;
if(comBoxListStr.equals("员工编号")) {
sqlSearch = "sID = " ;
}
else if (comBoxListStr.equals("员工姓名")) {
sqlSearch = "sNAME = " ;
}
else if (comBoxListStr.equals("员工性别")) {
System.out.println("问题:");
sqlSearch = "sSEX = " ;
}
else if (comBoxListStr.equals("出生日期")) {
sqlSearch = "sBIRTH = " ;
}
else if (comBoxListStr.equals("部门编号")) {
sqlSearch = "dID = " ;
}
else if (comBoxListStr.equals("员工职务")) {
sqlSearch = "sPOST = " ;
}
else if (comBoxListStr.equals("员工薪水")) {
sqlSearch = "sSALARY = " ;
}
else if (comBoxListStr.equals("学历编号")) {
sqlSearch = "eID = " ;
}
else if (comBoxListStr.equals("员工状态")) {
sqlSearch = "sSTATE = " ;
}

String hjcSql = "SELECT * FROM hjcTable where " + sqlSearch +"'"+getInputTextField+ "'";
System.out.println(hjcSql);
try {
rs = sql.executeQuery(hjcSql);
tableModel.getDataVector().clear();

//查询之前清空表中所有数据
dataTable = new String[100][100];

while(rs.next()) {
for(int j = 1; j < dataTitle.length; j++) {
dataTable[i][j-1] = rs.getString(j);
}
i++;
}
tableModel.setDataVector(dataTable, dataTitle);

} catch (SQLException ee) {

System.out.println("异常原因:"+ee);
JOptionPane.showMessageDialog(null,"没有找到该查询数据,请检查填写是否有误",null,JOptionPane.INFORMATION_MESSAGE);
ee.printStackTrace();
}

}

}
}

##运行效果
员工登记界面

添加数据


数据库表

查询功能界面

查询


注意事项
添加JDBC库


以上内容是课堂作业要求实现的功能,如有错误可以指正学习!!!!!!!!!

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: