我的日常

登录/注册
您现在的位置:论坛 盖世程序员(我猜到了开头 却没有猜到结局) 盖世程序员 > 简单的JAVA员工信息管理系统源码
总共48086条微博

动态微博

查看: 2807|回复: 1

简单的JAVA员工信息管理系统源码

[复制链接]

29

主题

5

听众

183

金钱

三袋弟子

该用户从未签到

跳转到指定楼层
楼主
发表于 2014-07-28 19:36:08 |只看该作者 |倒序浏览

// 项目目标:建立员工管理系统第一个版本。3 u4 P: n$ O" j, i5 H, C
// 实现图形用户界面的员工信息查询、增、删、改;与数据库建立,并将修改结果时时保存到数据库。
, h# ^: h! `, U// 模块:1.GUI界面 2.数据库连接 3.查询 4.增、5删 6改(操作数据库)

import java.sql.*;2 q4 U& G2 x% W: D" A% Z0 [
import java.awt.*;: ^) n4 q- b2 S. n) \( z# D
import java.awt.event.*;

import javax.swing.*;

public class EmpManageSys implements ActionListener {
& h) i3 |+ A3 G  R4 Q( D- u- VConnection con;/ t( _* L& c0 B  V6 W
Statement stmt;' z0 b; k/ ~" t
String sql;
  n# @' y5 b! z" dResultSet rs;8 n5 e: i$ O' i5 w) N
StringBuffer sb = new StringBuffer();

JTextField jtf2 = new JTextField("张飞",10);

JTextField jtf3 = new JTextField(10);

JTextField jtf4 = new JTextField(10);

JTextField jtf7 = new JTextField(10);

JTextField jtf5 = new JTextField(10);

JTextField jtf6 = new JTextField(10);

JTextField jtf8 = new JTextField(10);

JTextField jtf9 = new JTextField(10);

JTextArea jta10 = new JTextArea(50, 40);

public void actionPerformed(ActionEvent e) {
  ^# Q- a/ z' S/ P' a2 R8 [# KString str = e.getActionCommand();1 n* r$ x7 ]' ~) U
if ("查询".equals(str)) {

//if check the button of 查询,then go to method of searchEmp()3 `. g& {7 @3 m, n; I
searchEmp();2 a  X: L$ s, y( e% X$ |
} else if ("增加".equals(str)) {

//if check the button of 增加,then go to method of addEmp()0 o% k& m0 v& |0 r8 d2 S
addEmp();5 m5 R' n/ S# P. a; ~" B% f6 ]# a
} else if ("修改".equals(str)) {

//if check the button of 修改,then go to method of alterEmp(); k6 |2 Z' ~; _+ O' M! U6 H% L$ P
alterEmp();* ]* ~6 L6 P& R5 R# P+ ^" F
} else if ("撤除".equals(str)) {

//if check the button of 撤除,then go to method of deleteEmp()
+ n5 d' S9 l; F% K+ gdeleteEmp();
9 {( T/ ^% c! C) n}
0 M- p# T  d( E% p}

//this is the constructor.

EmpManageSys() {" p1 I# q( ?6 s
createGUI();
4 D* d( T# F* H9 ~2 aconnectToDataBase();
4 R) [8 f; H% G; D5 Z* k8 I, Z( s5 @// searchEmp();) ^  z. b$ p- ^2 a- b
// addEmp();
: ?8 [2 b6 D9 z+ I  }3 W# {9 R7 F// deleteEmp();
6 f( ^$ W7 N; {0 S1 HalterEmp();" x( L% [) f5 B$ j
}

//connect to the database by using the method getConnection from the class of JdbcUtil.

//You can find the class of JdbcUtil at the button of this page.

public Connection connectToDataBase() {
, t; \3 j( [/ i# e) C: g/ Ucon = JdbcUtil.getConnection();
+ Z4 @( o$ D5 A- F' ^# u) ySystem.out.println(con);( Z1 m7 ]0 s3 E: O; ?8 X
return con;

}

public void searchEmp() {
; N1 s2 @) V2 G" N$ k: j0 q4 Bjta10.setText("");. t' h. q" s: c. D1 ]
try {
9 p# j2 ~  f' x8 I7 pstmt = con.createStatement();
0 f, F! d  Q. L/ Z, c// sql = "select * from Mstar where id=" + jtf3.getText();
3 e; n0 l& w* w4 l4 C4 }// sql1="select * from sd100343 where ="+jtf3.getText();. M2 H# S+ K" {4 u" R( {
sql = "select * from Mstar where chineseName=": P6 c. y0 X9 i- ^( D
+ jtf2.getText().toLowerCase().trim() + "or id="
, q. S3 x) T" V2 d+ jtf3.getText().toLowerCase().trim() + "or engName="% }$ }& Z9 v$ Y. W  U3 H1 _- p
+ jtf4.getText().toLowerCase().trim() + "or UNIT="
5 X( X8 _$ ]% Z7 [  D+ c+ jtf5.getText().toLowerCase().trim() + "or TEAM="
' e; ?9 X3 t! n& p3 V+ jtf6.getText().toLowerCase().trim() + "or Phone="
0 o1 P. X# y( n1 G  a+ jtf7.getText().toLowerCase().trim() + "or region="& W* v" C0 w8 |. p0 f" k  r9 |# ?
+ jtf8.getText().toLowerCase().trim() + "or busStation="
+ D  v! ~* Y, g" u5 z4 b+ jtf9.getText().toLowerCase().trim();' W. I3 |3 v# l! w4 x& P9 [
stmt.executeQuery(sql);! \+ e# T- D, q2 @8 C
rs = stmt.getResultSet();6 h! A0 H; K7 ~% L; A& H
ResultSetMetaData meta = rs.getMetaData();, F- j, B8 A7 ?3 k3 r' c" p  W+ c6 N
int cols = meta.getColumnCount();
* f, \/ W- W$ W8 u' u# dwhile (rs.next()) {
& y8 p  i$ p5 `  p6 B% Efor (int i = 1; i <= cols; i++) {' y# i' m$ ]* p+ N# i8 T, A0 }
sb.append(" " + meta.getColumnName(i) + " =");/ m% [5 r, H3 V  \/ @/ S5 ^
sb.append(rs.getString(i));; u0 O+ ?% {$ G
}
3 f: B: N1 U4 x4 o: Osb.append("\n");
4 C2 S' C9 H: X# a! {9 C( o" k; ojta10.setText(sb.toString());( t6 r0 c- a3 i- v6 q/ r6 J8 N  X
}
' U2 G: e$ S; a: Y/ Q$ g9 a8 U! a0 S} catch (SQLException e11) {
' _" M2 U" N6 f. c& Le11.printStackTrace();/ R6 ?. x5 a+ c
}

}

public void addEmp() {
2 U9 f2 u# J9 ctry {
( k+ K7 x! n$ u3 ostmt = con.createStatement();' D, Y. a; Y$ X) ^6 R
sql = "update Mstar values(" + jtf2.getText() + jtf3.getText()
2 @6 M/ H9 b6 v2 x$ x% N+ jtf4.getText() + jtf5.getText() + jtf6.getText()
1 k* K* ?4 r. Q+ jtf7.getText() + jtf8.getText() + jtf9.getText() + ")";
+ v6 b2 \. K( l! Y+ @4 @7 F8 I1 Hint i = stmt.getUpdateCount();
/ ^$ A% w+ X: Z8 U# k. Vif ((jtf2.getText() != null) && (jtf4.getText() != null)
6 |; z) ?5 z3 u&& (jtf6.getText() != null) && (jtf7.getText() != null)) {' C, `8 J. [2 O  R" \8 M3 t
stmt.executeUpdate(sql);2 I. ~, J: g" w! \/ n
jta10.setText("添加记录成功" + i + "条");
9 f1 J, V! ^$ o) }/ h  x} else {
+ x2 ?( S6 P( J# G4 Xjta10.setText("带*号项为添加记录时不能为空");
2 Y) Q5 W- q/ `1 t}
8 B% U5 v& [+ ]& F: ]( w, W} catch (SQLException e1) {
/ ^' T- @# c* n! I1 l& W- C  \e1.printStackTrace();9 Y. V& @$ I  ]7 y# j
}: ~6 g/ J5 ?. f$ n% O
}

public void deleteEmp() {; M& ~& ]/ S3 c$ Y. k! L6 Q+ [
searchEmp();
; l8 k- v4 U9 w7 W. stry {
8 }' U' g# \3 F2 M: v' U( Q0 Zstmt = con.createStatement();" t, `8 f" I5 T4 V, ]" I9 V5 T
sql = "delete from Mstar where chineseName=", E8 Z0 E+ s8 O
+ jtf2.getText().toLowerCase().trim() + "or id="
' `- F! x, p' O/ l1 Y8 V2 h' x+ jtf3.getText().toLowerCase().trim() + "or engName="3 l8 J: A" w5 q8 k) i
+ jtf4.getText().toLowerCase().trim() + "or UNIT=": ]! y; o/ U! v7 Z# x' h2 e" d
+ jtf5.getText().toLowerCase().trim() + "or TEAM="7 @' n4 w0 [" D
+ jtf6.getText().toLowerCase().trim() + "or Phone="5 D- `) W/ M+ e$ w. U, f1 B) u
+ jtf7.getText().toLowerCase().trim() + "or region="
1 X$ h1 D/ S$ e1 g. o+ N+ jtf8.getText().toLowerCase().trim() + "or busStation="; n: j- l+ @. s/ t* [7 }% e0 u
+ jtf9.getText().toLowerCase().trim();
+ F: r) Z. W) L0 @stmt.executeUpdate(sql);
: C3 h. Z+ a  n: lint i = stmt.getUpdateCount();$ I+ F% H' e( ~& d
jta10.setText("撤除操作成功" + i + "条");
% ]2 P2 \7 F. j0 w) o} catch (SQLException e) {: j- V& I8 z7 l8 l
e.printStackTrace();  o6 H& @: p) v& Z4 L
}

}

public void alterEmp() {3 m6 c: ]: T8 d0 K! Q; I; m$ b/ }
searchEmp();
5 ?7 R, {# I* T4 j+ Q& N; Q8 h# Ssql = "update Mstar set chineseName="8 D) x1 ~( S' S) T4 y
+ jtf2.getText().toLowerCase().trim() + "and id="
1 [3 U' \, Y7 q+ jtf3.getText().toLowerCase().trim() + "and engName="9 O! w9 K" D3 D0 T  ]/ H. r
+ jtf4.getText().toLowerCase().trim() + "and UNIT="
" k' D$ l8 L0 Y+ @9 ?( C+ jtf5.getText().toLowerCase().trim() + "and TEAM="2 v& c/ N& Y- T# c) j; B
+ jtf6.getText().toLowerCase().trim() + "and Phone="- k% v, H2 b. f, ]
+ jtf7.getText().toLowerCase().trim() + "and region="
% |" c9 H! [7 |+ jtf8.getText().toLowerCase().trim() + "and busStation="
& ~& `! s" L( R% ~+ jtf9.getText().toLowerCase().trim();

int i = 0;7 I. M9 o0 \$ F  ]/ F7 r) u) o
try {. _0 v" Z: ~6 t9 y3 }
stmt.executeUpdate(sql);
$ K& t9 T; o: k& H2 o/ \. i* li = stmt.getUpdateCount();7 T# ~# y# k/ E& E' [
} catch (SQLException e) {/ I( j2 D7 C7 w) J! s8 Q
e.printStackTrace();
1 s2 R* S8 [% C1 u0 b}' g! i0 ]6 x9 j8 l1 `$ R' c
jta10.setText("修改操作成功" + i + "条");
* t5 E0 r. |: k4 k, d}

public void createGUI() {
8 {  l: r. M) n0 J) ^. }) cJFrame jf = new JFrame("员工信息管理系统");3 [( }; F" N! ~
jf.setLayout(new GridLayout(2, 1));3 [9 f  P* l* e5 y: p: g- Y
// jf.setLayout(new GridLayout(10,2));0 P8 ?8 V, w8 E( q2 j* H
JPanel jp00 = new JPanel(new GridLayout(5, 4));

JPanel jp1 = new JPanel();  C0 j) c+ t# W, x$ g
JButton jb11 = new JButton("查询");! P! F: ^. t: h6 L
jb11.addActionListener(this);
" L% O8 g- J8 G- bJButton jb12 = new JButton("增加");! v- B, ~  Y/ l% N/ U/ h- Z
jb12.addActionListener(this);
8 V+ J" z8 B2 M( k4 kjp1.add(jb11);
5 W& P, [8 j- V: R3 J3 q/ @jp1.add(jb12);
* W5 _! Z- w* G, V! djp00.add(jp1);
+ I/ Q& K  `: [2 k% X1 nJPanel jp11 = new JPanel();
7 V1 G( H: d8 h6 m) wJButton jb111 = new JButton("修改");
2 M+ ^1 j2 F; o' njb111.addActionListener(this);
8 t/ |/ E! N/ N  N/ a8 k5 F1 y) UJButton jb112 = new JButton("撤除");
9 r& l; _  |  _( `) g9 Gjb112.addActionListener(this);
1 G- ~5 z2 I6 l' }4 b; J8 Qjp11.add(jb111);) Y7 K9 Y/ u7 d: p! U! D
jp11.add(jb112);5 o6 f) h8 V8 D/ |
jp00.add(jp11);! {) S9 I0 z4 ^% ~6 ?) N3 n
JPanel jp2 = new JPanel();% I, h; l( s" m0 k3 Q
JLabel jl2 = new JLabel("中 文 名 * ");
+ [( i. |6 [% R3 Vjp2.add(jl2);% T5 D0 p: z7 f; s9 k9 W; o
jp2.add(jtf2);, Z9 ~0 w3 D" @( S3 N  E
jp00.add(jp2);
2 @6 j2 l, ]8 f3 G( yJPanel jp3 = new JPanel();
0 |0 a/ Y0 N' lJLabel jl3 = new JLabel("工 号 ");
: W; p5 @4 F% V  p5 ^% q3 u& Djp3.add(jl3);
9 i; }! V: `8 sjp3.add(jtf3);+ g7 ^: e5 R6 d( d, m/ @' V; t
jp00.add(jp3);+ Y! }+ {1 q4 d. ^
JPanel jp4 = new JPanel();
* {9 e& s& I) Z2 ?. [5 vJLabel jl4 = new JLabel("EngName*");
+ u! {7 o6 s& g+ _2 ejp4.add(jl4);
$ u# ~( H7 H7 F  F; ?jp4.add(jtf4);( E: x0 R2 \$ s- S" A- K- H6 f+ Q
jp00.add(jp4);5 [) {1 t& D$ e1 v' ]0 V! k! p8 r, Y
JPanel jp5 = new JPanel();
: Y( V! C! ^& k  ?3 }! WJLabel jl5 = new JLabel("UNIT ");. k9 }  k7 \+ Y& m6 V
jp5.add(jl5);
. n; ^6 `9 r1 u7 n" _jp5.add(jtf5);+ n+ X. h( n& H  Y# w# W" c
jp00.add(jp5);7 I6 a6 J' ~) C1 J
JPanel jp6 = new JPanel();
# `0 K  y! c# WJLabel jl6 = new JLabel("TEAM* ");
, M$ F# O8 P+ }' O+ Zjp6.add(jl6);' P$ |2 S+ \- w- y4 f1 {
jp6.add(jtf6);
% X' ], c* ^7 |jp00.add(jp6);
: l' s: U( y  IJPanel jp7 = new JPanel();
; V+ F2 Q( F5 X! q6 E$ ]0 S, r' [1 ~JLabel jl7 = new JLabel("Phone* ");
& H$ P1 P% H$ N& B) I: djp7.add(jl7);
) z( k- ~6 g# t9 ]6 njp7.add(jtf7);4 K6 U! m. l. I2 T* ?7 S! r2 g
jp00.add(jp7);
# ]* r9 G, E3 Y$ L! D3 RJPanel jp8 = new JPanel();- n! }, y. d# _6 E
JLabel jl8 = new JLabel("区域 ");/ `. [- i7 C, G. c/ P8 e6 ?
jp8.add(jl8);9 P! [8 N# B' C1 F. o+ W
jp8.add(jtf8);
! t9 s' Y, r3 ~) c5 Rjp00.add(jp8);
# Q( {( |. l7 S, eJPanel jp9 = new JPanel();( `# }$ d5 E; x
JLabel jl9 = new JLabel("公交站 ");' J# F5 e  t: A/ M2 x5 h
jp9.add(jl9);% R) H, p" t5 y" j. b  O
jp9.add(jtf9);* c) {9 U; A# g: i7 e0 d& \, G# ~
jp00.add(jp9);
1 Z& G2 A+ [  p3 u! k  i3 k( Q/ Djf.add(jp00);; T. [2 z/ o5 w" R: T
JPanel jp01 = new JPanel();% T2 w. V: L2 F& ^8 }! h
jta10.setText("--用户使用手册-- \n1、查询:选择一个字段,如 EngName 在文本框中输入相应内容,点击查询\n2、增加:在各个文本框中输入相应内容后,点击增加。\n3、修改:先查询到你要的记录,在更改相应内容,点击修改。\n4、撤除:类似于修改操作");  p: N! B$ l' S5 n
jp01.add(jta10);" ]: O5 |! l9 W" T+ v3 y9 u
jf.add(jp01);9 U' M. c/ C, ~4 \& H2 B
jf.setSize(700, 500);
, z; F8 M. T1 {8 D* u, ^1 j5 bjf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);! u( W; _1 a. ]
jf.setVisible(true);

}

public static void main(String[] args) {& W! Y# {) S$ D0 U, g3 }
new EmpManageSys();
; L& v6 Y; U0 R% l( f}9 ]& V, Q2 d5 j) I( n
}


, U# }5 i( g, Q/ [

我的日常 1、本主题所有言论和图片纯属会员个人意见,与本社区立场无关
2、本站所有主题由该帖子作者发表,该帖子作者与我的日常享有帖子相关版权
3、其他单位或个人使用、转载或引用本文时必须同时征得该帖子作者和我的日常的同意
4、帖子作者须承担一切因本文发表而直接或间接导致的民事或刑事法律责任
5、本帖部分内容转载自其它媒体,但并不代表本站赞同其观点和对其真实性负责
6、如本帖侵犯到任何版权问题,请立即告知本站,本站将及时予与删除并致以最深的歉意
7、我的日常管理员和版主有权不事先通知发贴者而删除本文


JAVA爱好者①群:JAVA爱好者① JAVA爱好者②群:JAVA爱好者② JAVA爱好者③ : JAVA爱好者③

29

主题

5

听众

183

金钱

三袋弟子

该用户从未签到

沙发
发表于 2014-07-28 19:36:24 |只看该作者
//---------------------------------------------------------------------
4 I; @. e" p( \9 D9 _* hSQL工具类JdbcUtil:用于实现数据库连接和数据库关闭。上面程序用到了这个类
1 Z7 ?" }6 [* t% Bimport java.sql.*;
2 @6 X7 Z* _0 V  I/ Hpublic class JdbcUtil/ v* J! J: R' _" K/ v
{
; D3 U) c/ A( S, {( a//load driver from file 2 o1 d+ P0 N" U) @' Y- ?
static{1 F( W5 Q" V; O. X3 G
try{3 \3 {& p8 S) Z& Q
String driverName =4 g* N. g2 V6 t2 d
"oracle.jdbc.driver.OracleDriver";, v* |6 b. L: p) }
Class.forName(driverName);- S& \/ x  J3 V
}catch(Exception e){' z6 w- t5 m# N, X; G0 Z
e.printStackTrace();. ~: z( E" V" @  z6 G( V- x! f
}
& c1 b0 A4 v: l$ }: ?4 W}
- W9 q( W: h8 W1 f//establish connection
; n0 s  N% R* lpublic static Connection getConnection(){. X% S* Q, G1 |8 |2 j) O, f6 m2 d" ?  [
Connection con = null;
7 a% D/ k6 B! C7 B( j3 \; I- stry{$ T# j* {2 B- R* I. o; `, W0 P+ q4 y
String url =
! h# s6 L% P7 E* s"jdbcracle:thin192.168.0.26:1521:tarena";% U, f. s& ~! [/ i; v9 {; T
String usr = "openlab";
! o1 d- S; w8 m2 k( o" Y* sString pwd = "open123";
0 x! U( G! H, @6 T# U6 Gcon = DriverManager.getConnection(6 K& ~9 [9 \$ U9 P1 b. y
url,usr,pwd);
/ G. A% [4 K- a5 I4 g) Y( N}catch(Exception e){
+ p2 W4 M$ i# j" p+ w) I' N' I) Be.printStackTrace();8 x5 ^' i  }, `2 t. m
}
* P" \1 A6 |; E+ _! s! Areturn con;
! U+ p. M5 E; Z3 p6 y8 k}
8 @7 ^  \$ z& i, ~8 r7 p
7 ]0 t2 g; g" a( ?. {9 [5 P( D//close resultSet,statement and connection
1 y+ x% ^- Q, m/ Upublic static void close(ResultSet rs,Statement stmt,Connection con){6 q# g1 ]4 `& ?+ K+ W/ b7 c) Q/ c3 F
try{1 y4 y7 _8 n1 L5 P$ s
if(rs!=null) rs.close();
1 H- [6 a$ U3 J8 ]}catch(Exception ex){
& D( N- ?8 |6 D. dex.printStackTrace();
2 a6 ?, i9 Z+ T" A1 ^/ Z}
; {% }9 g! H4 Z/ Utry{
8 [: w, i  P  Z$ d1 H: f* k4 Kif(stmt!=null) stmt.close();
7 `' P! v" L$ G% W* a  d}catch(Exception ex){
4 H7 @' o& s5 m1 P/ Vex.printStackTrace();
$ f+ ~; U: F5 j' B}
  V6 M) S- a. n2 [- R# N# ftry{
2 g) R* W6 K+ w/ _" K7 O5 \& Iif(con!=null) con.close();
5 z1 s- Z7 y* K8 y/ s8 c" t}catch(Exception ex){" V$ Q; O7 U, f2 ^
ex.printStackTrace();3 E. L0 v1 @, y$ Q% F" J# m
}2 I9 Y6 |) T$ t/ m8 k
}
: r7 h; v2 A- g7 Z5 C8 n; g& K}
回复

使用道具 举报

快速回复
您需要登录后才可以回帖 登录 | 立即注册

   

关闭

站长推荐上一条 /1 下一条

发布主题 快速回复 返回列表 联系我们 官方QQ群 科帮网手机客户端
快速回复 返回顶部 返回列表