本学期的数据库课设作业,时间有限界面还有一些基本表没有建立完善。
但还是第一次完整地完成数据库以及相关系统程序设计。
还有JAVA链接所需包(JDBC以及xml.bind.jar)的安装放在下一条博客。
一.定义主界面类
package edu;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Test15 {
public static void main(String[] args) {
JFrame jframe = new JFrame("人力资源管理系统") ;
Dimension d = new Dimension(300,400);
Point p = new Point (250,350);
jframe.setSize(d);
jframe.setLocation(p);
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jframe.setVisible(true);
JButton button1 = new JButton("添加");
JButton button2 = new JButton("修改");
JButton button3 = new JButton("查询");
JButton button4 = new JButton("删除");
JButton button5 = new JButton("浏览");
JButton button6 = new JButton("部门");
FlowLayout flow = new FlowLayout(FlowLayout.LEFT,20,30);
JPanel panel = new JPanel(flow);
panel.setSize(300,400);
panel.setBackground(Color.red);
panel.add(button1);
panel.add(button2);
panel.add(button3);
panel.add(button4);
panel.add(button5);
panel.add(button6);
jframe.getContentPane().add(panel);
button1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
Add add =new Add();
}
});
button2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
Change change =new Change();
}
});
button3.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
Ask ask =new Ask();
}
});
button4.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
Delete delete =new Delete();
}
});
button5.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
Look look =new Look();
}
});
button6.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
Db db =new Db();
}
});
}
}
package edu;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
public class Add extends JFrame {
private static final long serialVersionUID = -1928970409928880648L;
JLabel jlpno = new JLabel("工 号:");
JLabel jlname = new JLabel("姓 名:");
JLabel jlsex = new JLabel("性 别:");
JLabel jldepartment = new JLabel("部 门:");
JLabel jldno = new JLabel("部 门 号:");
JLabel jlduty = new JLabel("职 务:");
JLabel jlrank = new JLabel( "职务等级:");
JLabel jlexperience = new JLabel("工作经验:");
JTextField jtpno = new JTextField("",20);
JTextField jtname = new JTextField("",20);
JTextField jtsex = new JTextField("",20);
JTextField jtdepartment = new JTextField("",20);
JTextField jtdno = new JTextField("",20);
JTextField jtduty = new JTextField("",20);
JTextField jtrank = new JTextField("",20);
JTextField jtexperience = new JTextField("",20);
JButton buttonadd = new JButton("添加");
JButton buttonreturn = new JButton("返回");
public Add() {
JPanel jppno = new JPanel();
JPanel jpname = new JPanel();
JPanel jpsex = new JPanel();
JPanel jpdno = new JPanel();
JPanel jpdepartment = new JPanel();
JPanel jpduty = new JPanel();
JPanel jprank = new JPanel();
JPanel jpexperience = new JPanel();
JPanel jpforbutton = new JPanel(new GridLayout(1,1));
jppno.add(jlpno);
jppno.add(jtpno);
jpname.add(jlname);
jpname.add(jtname);
jpsex.add(jlsex);
jpsex.add(jtsex);
jpdepartment.add(jldepartment);
jpdepartment.add(jtdepartment);
jpdno.add(jldno);
jpdno.add(jtdno);
jpduty.add(jlduty);
jpduty.add(jtduty);
jprank.add(jlrank);
jprank.add(jtrank);
jpexperience.add(jlexperience);
jpexperience.add(jtexperience);
jpforbutton.add(buttonadd);
jpforbutton.add(buttonreturn);
buttonadd.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
Connection conn = null;
PreparedStatement ps=null;
String xdno= jtdno.getText();
String xdepart = jtdepartment.getText();
String xduty = jtduty.getText();
String xname = jtname.getText();
String xpno = jtpno.getText();
String xrank =jtrank.getText();
String xsex =jtsex.getText();
String sql = "INSERT INTO staff(pno,name,sex,dno,department,duty,rank,experience) "
+ "values(?,?,?,?,?,?,?,?)";
String sql3 = "INSERT INTO salary(pno,pname,sex,dno,department,duty,rank,salary) "
+ "values(?,?,?,?,?,?,?,?)";
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
System.out.println("JBDC 加载成功!");
}catch(Exception a){
System.out.println("JBDC 狗带!");
a.printStackTrace();
}
try{
conn=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;datebaseName=source","admin.","123456");
ps=conn.prepareStatement(sql);
ps.setString(1,xpno);
ps.setString(2,xname);
ps.setString(3,xsex);
ps.setString(4,xdno);
ps.setString(5,xdepart);
ps.setString(6,xduty);
ps.setString(7,xrank);
ps.setString(8,jtexperience.getText());
ps.executeUpdate();
int i=Integer.parseInt(xrank);
ps=conn.prepareStatement(sql3);
ps.setString(1,xpno);
ps.setString(2,xname);
ps.setString(3,xsex);
ps.setString(4,xdno);
ps.setString(5,xdepart);
ps.setString(6,xduty);
ps.setString(7,xrank);
ps.setInt(8,i*1000+5000);
ps.executeUpdate();
}catch (SQLException b){
System.out.println("1 ");
b.printStackTrace();
}finally{
try{
conn.close();
System.out.println("MySQL 关闭成功");
}catch (SQLException c){
System.out.println("MySQL 关闭失败 ");
c.printStackTrace();
}
}
}
});
buttonreturn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
}
});
this.setTitle("添加员工信息");
this.setLayout(new GridLayout(9,1));
this.add(jppno);
this.add(jpname);
this.add(jpsex);
this.add(jpdno);
this.add(jpdepartment);
this.add(jpduty);
this.add(jprank);
this.add(jpexperience);
this.add(jpforbutton);
this.setLocation(400,300);
this.setSize(350,300);
this.setVisible(true);
}
}
package edu;
import java.sql.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Change extends JFrame {
private static final long serialVersionUID = -1928970409928880648L;
JLabel jlpno = new JLabel("工 号:");
JLabel jlname = new JLabel("姓 名:");
JLabel jlsex = new JLabel("性 别:");
JLabel jldepartment = new JLabel("部 门:");
JLabel jldno = new JLabel("部 门 号:");
JLabel jlduty = new JLabel("职 务:");
JLabel jlrank = new JLabel( "职务等级:");
JLabel jlexperience = new JLabel("工作经验:");
JTextField jtpno = new JTextField("",20);
JTextField jtname = new JTextField("",20);
JTextField jtsex = new JTextField("",20);
JTextField jtdepartment = new JTextField("",20);
JTextField jtdno = new JTextField("",20);
JTextField jtduty = new JTextField("",20);
JTextField jtrank = new JTextField("",20);
JTextField jtexperience = new JTextField("",20);
JButton buttonchange = new JButton("修改");
JButton buttonreturn = new JButton("返回");
public Change() {
JPanel jppno = new JPanel();
JPanel jpname = new JPanel();
JPanel jpsex = new JPanel();
JPanel jpdno = new JPanel();
JPanel jpdepartment = new JPanel();
JPanel jpduty = new JPanel();
JPanel jprank = new JPanel();
JPanel jpexperience = new JPanel();
JPanel jpforbutton = new JPanel(new GridLayout(1,1));
jppno.add(jlpno);
jppno.add(jtpno);
jpname.add(jlname);
jpname.add(jtname);
jpsex.add(jlsex);
jpsex.add(jtsex);
jpdepartment.add(jldepartment);
jpdepartment.add(jtdepartment);
jpdno.add(jldno);
jpdno.add(jtdno);
jpduty.add(jlduty);
jpduty.add(jtduty);
jprank.add(jlrank);
jprank.add(jtrank);
jpexperience.add(jlexperience);
jpexperience.add(jtexperience);
jpforbutton.add(buttonchange);
jpforbutton.add(buttonreturn);
buttonchange.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String xdno= jtdno.getText();
String xdepart = jtdepartment.getText();
String xduty = jtduty.getText();
String xname = jtname.getText();
String xpno = jtpno.getText();
String xrank =jtrank.getText();
String xsex =jtsex.getText();
String xexperience =jtexperience.getText();
int num = Integer.parseInt(xrank)*1000+5000;
String xsalary=Integer.toString(num);
Connection conn = null;
ResultSet res = null;
Statement stat = null;
String sql = "SELECT pno,name,sex,dno,department,duty,rank,experience FROM staff;";
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
}catch(Exception d){
System.out.println("jdbc fall");
d.printStackTrace();
}
try{
conn=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;datebaseName=source","admin.","123456");
stat=conn.createStatement();
res=stat.executeQuery(sql);
while (res.next())
{
String i = res.getString(1).trim();
if (i.equals(jtpno.getText()))
{
String sql2="UPDATE staff SET name='"+xname+"' WHERE pno='"+jtpno.getText()+"'";
String sql3="UPDATE staff SET sex='"+xsex+"' WHERE pno='"+jtpno.getText()+"'";
String sql4="UPDATE staff SET dno='"+xdno+"' WHERE pno='"+jtpno.getText()+"'";
String sql5="UPDATE staff SET department='"+xdepart+"' WHERE pno='"+jtpno.getText()+"'";
String sql6="UPDATE staff SET duty='"+xduty+"' WHERE pno='"+jtpno.getText()+"'";
String sql7="UPDATE staff SET rank='"+xrank+"' WHERE pno='"+jtpno.getText()+"'";
String sql8="UPDATE staff SET experience='"+xexperience+"' WHERE pno='"+jtpno.getText()+"'";
String sql9="UPDATE salary SET pname='"+xname+"' WHERE pno='"+jtpno.getText()+"'";
String sql10="UPDATE salary SET sex='"+xsex+"' WHERE pno='"+jtpno.getText()+"'";
String sql11="UPDATE salary SET dno='"+xdno+"' WHERE pno='"+jtpno.getText()+"'";
String sql12="UPDATE salary SET department='"+xdepart+"' WHERE pno='"+jtpno.getText()+"'";
String sql13="UPDATE salary SET duty='"+xduty+"' WHERE pno='"+jtpno.getText()+"'";
String sql14="UPDATE salary SET rank='"+xrank+"' WHERE pno='"+jtpno.getText()+"'";
String sql15="UPDATE salary SET salary='"+xsalary+"' WHERE pno='"+jtpno.getText()+"'";
stat=conn.createStatement();
stat.executeUpdate(sql2);
stat.executeUpdate(sql3);
stat.executeUpdate(sql4);
stat.executeUpdate(sql5);
stat.executeUpdate(sql6);
stat.executeUpdate(sql7);
stat.executeUpdate(sql8);
stat.executeUpdate(sql9);
stat.executeUpdate(sql10);
stat.executeUpdate(sql11);
stat.executeUpdate(sql12);
stat.executeUpdate(sql13);
stat.executeUpdate(sql14);
stat.executeUpdate(sql15);
try{
stat.close();
conn.close();
}catch(SQLException ar){
ar.printStackTrace();
}
break;
}
}
}catch (SQLException e1) {
e1.printStackTrace();
}
finally{
try{
conn.close();
}catch(SQLException ar){
ar.printStackTrace();
}
}
}
});
buttonreturn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
}
});
this.setTitle("修改员工信息");
this.setLayout(new GridLayout(9,1));
this.add(jppno);
this.add(jpname);
this.add(jpsex);
this.add(jpdno);
this.add(jpdepartment);
this.add(jpduty);
this.add(jprank);
this.add(jpexperience);
this.add(jpforbutton);
this.setLocation(400,300);
this.setSize(350,300);
this.setVisible(true);
}
}
package edu;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
import edu.Test15;
public class Ask extends JFrame {
private static final long serialVersionUID = -1928970409928880648L;
JLabel jlpno = new JLabel("工 号:");
JLabel jlname = new JLabel("姓 名:");
JLabel jlsex = new JLabel("性 别:");
JLabel jldepartment = new JLabel("部 门:");
JLabel jldno = new JLabel("部 门 号:");
JLabel jlduty = new JLabel("职 务:");
JLabel jlrank = new JLabel( "职务等级:");
JLabel jlsalary = new JLabel("工 资:");
JTextField jtpno = new JTextField("",20);
JLabel jname = new JLabel();
JLabel jsex = new JLabel();
JLabel jdepartment = new JLabel();
JLabel jdno = new JLabel();
JLabel jduty = new JLabel();
JLabel jrank = new JLabel();
JLabel jsalary= new JLabel();
JButton buttonask = new JButton("查询");
JButton buttonreturn = new JButton("返回");
public Ask() {
JPanel jppno = new JPanel();
JPanel jpname = new JPanel();
JPanel jpsex = new JPanel();
JPanel jpdno = new JPanel();
JPanel jpdepartment = new JPanel();
JPanel jpduty = new JPanel();
JPanel jprank = new JPanel();
JPanel jpsalary = new JPanel();
JPanel jpforbutton = new JPanel(new GridLayout(1,1));
jppno.add(jlpno);
jppno.add(jtpno);
jpname.add(jlname);
jpname.add(jname);
jpsex.add(jlsex);
jpsex.add(jsex);
jpdno.add(jldepartment);
jpdno.add(jdepartment);
jpdepartment.add(jldno);
jpdepartment.add(jdno);
jpduty.add(jlduty);
jpduty.add(jduty);
jprank.add(jlrank);
jprank.add(jrank);
jpsalary.add(jlsalary);
jpsalary.add(jsalary);
jpforbutton.add(buttonask);
jpforbutton.add(buttonreturn);
buttonask.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
Connection conn = null;
ResultSet res = null;
Statement stat = null;
String sql = "SELECT pno,pname,sex,dno,department,duty,rank,salary FROM salary;";
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
System.out.println("jdbc haha");
}catch(Exception d){
System.out.println("jdbc fall");
d.printStackTrace();
}
try{
conn=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;datebaseName=source","admin.","123456");
stat=conn.createStatement();
res=stat.executeQuery(sql);
while (res.next())
{
String i = res.getString(1).trim();
if (i.equals(jtpno.getText()))
{
jname.setText(res.getString(2));
jsex.setText(res.getString(3));
jdno.setText(res.getString(4));
jdepartment.setText(res.getString(5));
jduty.setText(res.getString(6));
jrank.setText(res.getString(7));
jsalary.setText(res.getString(8));
break;
}
}
}catch (SQLException e1) {
e1.printStackTrace();
}
finally{
try{
conn.close();
}catch(SQLException ar){
ar.printStackTrace();
}
}}}
);
buttonreturn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
}
});
this.setTitle("员工工资条");
this.setLayout(new GridLayout(9,1));
this.add(jppno);
this.add(jpname);
this.add(jpsex);
this.add(jpdno);
this.add(jpdepartment);
this.add(jpduty);
this.add(jprank);
this.add(jpsalary);
this.add(jpforbutton);
this.setLocation(400,300);
this.setSize(350,300);
this.setVisible(true);
}
}
package edu;
import java.sql.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class Look extends JFrame {
private static final long serialVersionUID = -1928970409928880648L;
Connection conn = null;
PreparedStatement ps = null;
ResultSet res = null;
JTable jtable;
JScrollPane jscrollpane = new JScrollPane();
Vector columnNames = null;
Vector rowData = null;
public Look() {
JPanel jpforbutton = new JPanel(new GridLayout(1,1));
columnNames = new Vector();
columnNames.add("工 号:");
columnNames.add("姓 名:");
columnNames.add("性 别: ");
columnNames.add("部 门 号:");
columnNames.add("部 门 :");
columnNames.add("职 务:");
columnNames.add("职务等级:");
columnNames.add("工 资:");
rowData = new Vector();
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
conn = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;datebaseName=source","admin.","123456");
ps = conn.prepareStatement("SELECT * FROM salary");
res = ps.executeQuery();
while (res.next())
{
Vector hang = new Vector();
hang.add(res.getString(1));
hang.add(res.getString(2));
hang.add(res.getString(3));
hang.add(res.getString(4));
hang.add(res.getString(5));
hang.add(res.getString(6));
hang.add(res.getString(7));
hang.add(res.getString(8));
rowData.add(hang);
}
System.out.println("load ok!");
}catch (Exception q){
q.printStackTrace();
System.out.println("go die");
}finally{
try{
res.close();
ps.close();
conn.close();
System.out.println("close ok");
}catch (SQLException o){
o.printStackTrace();
System.out.println("go die 2");
}
}
jtable = new JTable(rowData,columnNames);
jscrollpane = new JScrollPane(jtable);
this.add(jscrollpane);
this.setTitle("员工信息表");
this.setLayout(new GridLayout(2,5));
this.add(jpforbutton);
this.setLocation(300,300);
this.setSize(500,300);
this.setVisible(true);
this.setResizable(false);
}
}
package edu;
import java.sql.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Delete extends JFrame {
private static final long serialVersionUID = -1928970409928880648L;
JLabel jlpno = new JLabel("工 号:");
JTextField jtpno = new JTextField("",20);
JButton buttondelete = new JButton("删除");
JButton buttonreturn = new JButton("返回");
public Delete() {
JPanel jpnumber = new JPanel();
JPanel jpforbutton = new JPanel(new GridLayout(1,1));
jpnumber.add(jlpno);
jpnumber.add(jtpno);
jpforbutton.add(buttondelete);
jpforbutton.add(buttonreturn);
buttondelete.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String pno = jtpno.getText();
Connection conn = null;
ResultSet res = null;
Statement stat = null;
String sql = "DELETE FROM salary WHERE pno='"+pno+"'";
String sql2 = "DELETE FROM staff WHERE pno='"+pno+"'";
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
}catch(Exception a){
a.printStackTrace();
}
try{
conn=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;datebaseName=source","admin.","123456");
stat = conn.createStatement();
stat.executeUpdate(sql);
stat.executeUpdate(sql2);
}catch(SQLException h){
h.printStackTrace();
}finally{
try{
conn.close();
System.out.println("close success!");
}catch(SQLException j){
System.out.println("close go die!");
j.printStackTrace();
}
}
}
});
buttonreturn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
}
});
this.setTitle("删除员工信息");
this.setLayout(new GridLayout(9,1));
this.add(jpnumber);
this.add(jpforbutton);
this.setLocation(400,300);
this.setSize(350,300);
this.setVisible(true);
}
}
package edu;
import java.sql.*;
import java.util.Vector;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Db extends JFrame {
private static final long serialVersionUID = -1928970409928880648L;
Connection conn = null;
PreparedStatement ps = null;
ResultSet res = null;
JTable jtable;
JScrollPane jscrollpane = new JScrollPane();
JButton buttonask = new JButton("查询");
JLabel jldepartment = new JLabel("部 门:");
JLabel jldno = new JLabel("部 门 号:");
Vector columnNames = null;
Vector rowData = null;
JTextField jtdno = new JTextField("",20);
JTextField jtdepartment = new JTextField("",20);
public Db() {
JPanel jpforbutton = new JPanel(new GridLayout(1,1));
JPanel jpdno = new JPanel();
JPanel jpdepartment = new JPanel();
jpdno.add(jldno);
jpdno.add(jtdno);
jpdepartment.add(jldepartment);
jpdepartment.add(jtdepartment);
jpforbutton.add(buttonask);
columnNames = new Vector();
columnNames.add("工 号:");
columnNames.add("姓 名:");
columnNames.add("性 别: ");
columnNames.add("职 务:");
columnNames.add("职务等级:");
rowData = new Vector();
buttonask.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
conn = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;datebaseName=source","admin.","123456");
ps = conn.prepareStatement("SELECT pno,pname,sex,dno,duty,rank FROM salary ");
res = ps.executeQuery();
while (res.next())
{
String i = res.getString(4).trim();
System.out.println(jtdno.getText()+res.getString(4).trim()+i.equals(jtdno.getText()));
if (i.equals(jtdno.getText()))
{
Vector hang = new Vector();
hang.add(res.getString(1));
hang.add(res.getString(2));
hang.add(res.getString(3));
hang.add(res.getString(5));
hang.add(res.getString(6));
rowData.add(hang);
System.out.println(hang.add(res.getString(1))+res.getString(1));
}
}
System.out.println("load ok!");
}catch (Exception q){
q.printStackTrace();
System.out.println("go die");
}
finally{
try{
res.close();
ps.close();
conn.close();
}catch(SQLException ar){
ar.printStackTrace();
}
}
}}
);
jtable = new JTable(rowData,columnNames);
jscrollpane = new JScrollPane(jtable);
this.add(jscrollpane);
this.setTitle("部门信息表");
this.setLayout(new GridLayout(4,1));
this.add(jpdno);
this.add(jpdepartment);
this.add(jpforbutton);
this.setLocation(300,300);
this.setSize(500,300);
this.setVisible(true);
this.setResizable(false);
}
}