我的日常

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

动态微博

查看: 2806|回复: 1

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

[复制链接]

29

主题

5

听众

183

金钱

三袋弟子

该用户从未签到

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

// 项目目标:建立员工管理系统第一个版本。! {- [& G+ h1 j' J; S1 b) V& g. F' ^
// 实现图形用户界面的员工信息查询、增、删、改;与数据库建立,并将修改结果时时保存到数据库。
; j: l8 d0 V6 j- F// 模块:1.GUI界面 2.数据库连接 3.查询 4.增、5删 6改(操作数据库)

import java.sql.*;
% y( \2 g- k9 pimport java.awt.*;
% x; y! M  Y5 Y  y6 q* j' h( d: simport java.awt.event.*;

import javax.swing.*;

public class EmpManageSys implements ActionListener {
) ~/ `- q, u9 V; t$ x5 T& GConnection con;! p  Q4 q2 P' K: |  y- a) M
Statement stmt;
& M5 P7 P, H+ SString sql;
* n' H( n; z% n2 |+ JResultSet rs;
: ~& q+ k0 u+ g( R3 yStringBuffer 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) {9 n! l! m! o" C( d* J5 A7 I# ^! L/ C
String str = e.getActionCommand();4 o3 f, ~# p1 ?- o- ?
if ("查询".equals(str)) {

//if check the button of 查询,then go to method of searchEmp(); ?5 v5 m4 w! `' t. T
searchEmp();0 W; A: ~9 ]( g$ ~
} else if ("增加".equals(str)) {

//if check the button of 增加,then go to method of addEmp()
- @! ~, W7 k# m# ^! DaddEmp();
0 t- [3 O. d9 B' j% @, Q3 }} else if ("修改".equals(str)) {

//if check the button of 修改,then go to method of alterEmp()& z; b2 w( u) P# F# N- w  C# R, t
alterEmp();/ b8 Z$ C* y* T( P. f7 P9 y' u
} else if ("撤除".equals(str)) {

//if check the button of 撤除,then go to method of deleteEmp()
8 u. ?. Z! Q" B/ G. f7 d! KdeleteEmp();+ w5 e2 l! A+ y! ~* W) i: x9 H
}) j2 n5 q+ a) `$ W1 ?
}

//this is the constructor.

EmpManageSys() {
) R+ E9 O4 z* fcreateGUI();# p) _- u5 g9 V/ ~/ C
connectToDataBase();
6 D% o% ^6 x. n3 B% G// searchEmp();4 T+ H# _- Q( H% B
// addEmp();5 Q9 e# P( e: e8 A. a
// deleteEmp();
+ z' [4 A, `- qalterEmp();4 n, N2 V, e+ @& d: C6 ]5 a2 I! V! t
}

//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() {
7 u) [+ ^) t4 {+ m& e3 d7 S$ ]con = JdbcUtil.getConnection();
* S* o8 Q; x/ }3 tSystem.out.println(con);7 X  @- y- o, f1 B9 F( Y' `
return con;

}

public void searchEmp() {
+ \5 I  b9 ]4 [8 o+ bjta10.setText("");
7 X+ z5 {3 {! H5 M& Wtry {
: c7 c0 P; [$ x+ d; K! Q2 I2 P- Vstmt = con.createStatement();" |- ~( L5 Q: }
// sql = "select * from Mstar where id=" + jtf3.getText();
+ ~% @4 W9 X4 h% J5 e1 l// sql1="select * from sd100343 where ="+jtf3.getText();+ j* C8 [7 {! ^' m
sql = "select * from Mstar where chineseName="4 S3 o$ w5 {- A% p$ I- X
+ jtf2.getText().toLowerCase().trim() + "or id="
& q1 w5 d' K9 i4 i+ jtf3.getText().toLowerCase().trim() + "or engName="
* }5 s7 O2 K9 B; T/ c* q4 N+ jtf4.getText().toLowerCase().trim() + "or UNIT="& b2 Z6 |; E" ]+ k; D
+ jtf5.getText().toLowerCase().trim() + "or TEAM="
: m7 m0 `$ S- y+ jtf6.getText().toLowerCase().trim() + "or Phone=": H5 O: z/ {8 J; }4 ]5 p
+ jtf7.getText().toLowerCase().trim() + "or region="5 |% p4 N0 U! Z+ Z( W0 A% B: E
+ jtf8.getText().toLowerCase().trim() + "or busStation="' O- b: [; U8 f4 J" Y% e
+ jtf9.getText().toLowerCase().trim();; C& A! P# n; Z
stmt.executeQuery(sql);
9 O% u3 y2 K4 s) B" crs = stmt.getResultSet();
9 ^. Z8 @& W) Q( z! OResultSetMetaData meta = rs.getMetaData();; s: {/ _5 [  K# k
int cols = meta.getColumnCount();0 P3 b) @$ I. f
while (rs.next()) {
: P5 d* {1 C: d, S$ U+ Zfor (int i = 1; i <= cols; i++) {
4 A8 \2 O; P- E! V' _4 x4 Asb.append(" " + meta.getColumnName(i) + " =");: V2 i6 M7 r- {' P& a* y
sb.append(rs.getString(i));$ Z, A: Y2 d/ g2 _+ V
}* p2 _7 q1 _! z2 i0 `
sb.append("\n");
/ l8 D  {& u: i# J; `6 g( V; Fjta10.setText(sb.toString());0 g3 x5 O- ]' O, L& g/ q2 z
}5 c) t: ~3 G  p) b  Y% _) t* {
} catch (SQLException e11) {) ^9 s7 p. q, P* ^* }5 ]
e11.printStackTrace();
" j1 S; i1 u2 u! `: m( T2 U7 h" k}

}

public void addEmp() {
6 M! x4 ?2 J! k8 J; ^try {) P+ f" S/ }- m2 @4 v0 g& c
stmt = con.createStatement();
& q9 w4 z: F' H/ Lsql = "update Mstar values(" + jtf2.getText() + jtf3.getText()( y0 H3 R; ~; i, {3 g
+ jtf4.getText() + jtf5.getText() + jtf6.getText()
8 t2 Q; M- r# x. E, ?5 u+ jtf7.getText() + jtf8.getText() + jtf9.getText() + ")";
% y2 o3 g+ ?- ^. f, gint i = stmt.getUpdateCount();
7 a# F9 V* C* J7 tif ((jtf2.getText() != null) && (jtf4.getText() != null); h7 `% y( `( t! ?& [2 r
&& (jtf6.getText() != null) && (jtf7.getText() != null)) {
" x2 a4 A% R# vstmt.executeUpdate(sql);- r# ~+ l, C5 ]& A$ q5 H
jta10.setText("添加记录成功" + i + "条");
& M! g4 _3 \. O; X# w5 e! L} else {
1 A, p& r7 E9 ]  I2 [) _jta10.setText("带*号项为添加记录时不能为空");1 d6 w* M  m5 C5 ]" e
}: T2 f' o' [5 w1 b
} catch (SQLException e1) {
0 }$ c* y  F# P4 me1.printStackTrace();
+ Z& `' G# Q2 _/ O, j! C5 W. Q}
* ?, U+ {2 Q, ^8 o, O' Z# u}

public void deleteEmp() {
8 W; Q. u; \; H; {7 Y: m3 w6 S; wsearchEmp();( K; x) \6 w, F+ c
try {# |% b+ m* Q" V8 Q' \3 M2 E
stmt = con.createStatement();) `+ Y; w' I! ~% ]$ e" E
sql = "delete from Mstar where chineseName="
6 a& x0 V1 m, P  l4 H0 c/ N+ jtf2.getText().toLowerCase().trim() + "or id="" Q7 o. J3 o% O% f. s
+ jtf3.getText().toLowerCase().trim() + "or engName="5 E( k; f2 e# c, D/ f- m( j3 l
+ jtf4.getText().toLowerCase().trim() + "or UNIT="  n* ~8 e! I# \3 Y2 [. p- S: H  Y% ^
+ jtf5.getText().toLowerCase().trim() + "or TEAM="
6 L9 i9 E6 m' U& x$ `+ jtf6.getText().toLowerCase().trim() + "or Phone=". L  V& h& u# R( H1 V
+ jtf7.getText().toLowerCase().trim() + "or region="
* a& h( d  `/ v& \7 v8 V+ jtf8.getText().toLowerCase().trim() + "or busStation=": T" F0 ^5 [# L& P4 W# ^
+ jtf9.getText().toLowerCase().trim();" W' f# w  o; E; }& W( @
stmt.executeUpdate(sql);' @& H( `* T6 ?, l- }3 |6 z
int i = stmt.getUpdateCount();" g  q* K3 i$ b
jta10.setText("撤除操作成功" + i + "条");
1 E& A+ }3 A& z$ r  G: p} catch (SQLException e) {- \4 t9 f2 g+ @7 G3 n. A+ m. u( I
e.printStackTrace();  m# i  d% K; O# t0 s
}

}

public void alterEmp() {
, O. x' E/ K2 ysearchEmp();! \7 @# L8 Z  C5 N1 D
sql = "update Mstar set chineseName="
" n% T; }8 A1 X! J0 ]+ jtf2.getText().toLowerCase().trim() + "and id="( z3 v( O/ V2 e3 I; M$ y
+ jtf3.getText().toLowerCase().trim() + "and engName="$ c% c5 B$ L8 G* Z! @
+ jtf4.getText().toLowerCase().trim() + "and UNIT="
% ]: c3 ]5 Y& i9 e+ jtf5.getText().toLowerCase().trim() + "and TEAM="
0 ~7 _! {! t) j7 @; o1 j+ jtf6.getText().toLowerCase().trim() + "and Phone="
) L/ Z' q; F: o1 G* N: `" G+ jtf7.getText().toLowerCase().trim() + "and region="5 s7 [" ~( G4 f# \- H  }/ D! R( N
+ jtf8.getText().toLowerCase().trim() + "and busStation="* I8 z  W; g: C' U/ b$ u
+ jtf9.getText().toLowerCase().trim();

int i = 0;$ l1 U1 V8 ]3 H4 n4 |, N1 u
try {4 h# A& [5 P) z
stmt.executeUpdate(sql);
# g3 U( {0 K- d; ?# S' Pi = stmt.getUpdateCount();
6 k4 }: J0 U3 [! z# V/ w} catch (SQLException e) {
0 c2 v$ g$ U, j4 He.printStackTrace();$ q3 _! K/ ^7 j) b1 P
}/ p7 [( w6 c$ o; @! F" h
jta10.setText("修改操作成功" + i + "条");3 Z2 M3 l7 y' r7 o$ z
}

public void createGUI() {9 L% ~+ N2 z/ G) f" w
JFrame jf = new JFrame("员工信息管理系统");" Z/ h  I% U4 [5 O9 M
jf.setLayout(new GridLayout(2, 1));) m& d* y$ y6 I3 P; t( ?( }) g! b3 U
// jf.setLayout(new GridLayout(10,2));; ~4 x7 D$ o( v1 K8 N* Q% N. h
JPanel jp00 = new JPanel(new GridLayout(5, 4));

JPanel jp1 = new JPanel();
3 [1 k9 H4 u! @: m! SJButton jb11 = new JButton("查询");: h0 u0 O" t! e( |/ A
jb11.addActionListener(this);
& R! L. ~6 _7 O9 q3 H. U1 dJButton jb12 = new JButton("增加");
, b0 D: E6 b: X1 s# Yjb12.addActionListener(this);+ }- D' e4 I9 `0 w9 m2 I+ C
jp1.add(jb11);
+ A$ a2 {& r7 _% _. Vjp1.add(jb12);& E' \) h0 G7 P  l) v, V
jp00.add(jp1);
; y4 U, s' Z: Q4 b$ }3 q- aJPanel jp11 = new JPanel();
, v& ~2 W5 B* r! q" q) YJButton jb111 = new JButton("修改");
- r( e& z3 ?, ]1 ^' \, u1 tjb111.addActionListener(this);
  E: \3 _1 L/ C) Y* QJButton jb112 = new JButton("撤除");0 W5 c" F  [) j& P1 S
jb112.addActionListener(this);
( k7 Y$ ^: e+ a. N$ u& t! b/ e% gjp11.add(jb111);
9 M3 _/ l- x) Y1 X& T* Ojp11.add(jb112);, A7 e6 c+ L' v, w4 R" t
jp00.add(jp11);+ _; V; j/ k8 R4 s8 r
JPanel jp2 = new JPanel();' t% Z+ W* t; t4 m, L- ]$ t) p3 ?
JLabel jl2 = new JLabel("中 文 名 * ");
7 r" ?- q0 O. y1 k2 H" [0 s$ ajp2.add(jl2);7 ^" E; D9 E7 I1 [4 M* V
jp2.add(jtf2);
0 c; B: V' l4 N7 {$ U$ }1 k' Yjp00.add(jp2);+ _; P2 Y9 U1 H4 h! N
JPanel jp3 = new JPanel();
* i  c) J7 [2 M; u6 X: e' @+ O2 \JLabel jl3 = new JLabel("工 号 ");
) E9 y2 R: V& U1 v' G2 ]jp3.add(jl3);* n; ?% Q6 s& s* F8 c* j" R- S% P
jp3.add(jtf3);
& _. U5 P3 a5 Vjp00.add(jp3);
3 M4 Q' @7 s# P  {4 DJPanel jp4 = new JPanel();
1 K' Q* N) s& `, J/ a1 p8 |JLabel jl4 = new JLabel("EngName*");
) |: G  I' K7 \3 u0 y8 I! njp4.add(jl4);
9 y; X8 X' @3 B3 V+ Ajp4.add(jtf4);
8 w2 ]) U  \" j( O6 \, ~+ Q/ L% |* y: ajp00.add(jp4);
8 V/ v% d4 D, t( {; {2 HJPanel jp5 = new JPanel();
: n! V5 Y# n! S- ]4 LJLabel jl5 = new JLabel("UNIT ");( k% M* e$ E4 ^. g
jp5.add(jl5);
' v9 O, ^5 ^* |( i: e8 Bjp5.add(jtf5);
( n  M5 e/ g# n: Pjp00.add(jp5);
8 [( H& s$ b2 S- T1 j$ JJPanel jp6 = new JPanel();% S% }) e2 z0 D* Q; ^
JLabel jl6 = new JLabel("TEAM* ");
" ?9 ^) v; p3 }0 G% f5 Pjp6.add(jl6);
1 A7 l8 e2 j+ v. K7 k' A9 ?3 Tjp6.add(jtf6);* i" l2 r$ T6 w. r
jp00.add(jp6);
, }! @2 ~5 y' l5 s' w0 x3 GJPanel jp7 = new JPanel();
* r& Z. X- Z. r9 S: V% a: d! N: i0 cJLabel jl7 = new JLabel("Phone* ");) R5 `$ ]) b; }# z/ i/ c
jp7.add(jl7);
, X% _" L: M& m0 W1 tjp7.add(jtf7);
# j1 Y* G+ p  |" `1 I2 ojp00.add(jp7);1 ?! m& F. U, ?- U
JPanel jp8 = new JPanel();3 z" F! x* k- Y6 V
JLabel jl8 = new JLabel("区域 ");
0 i0 I0 w4 c% G: ]jp8.add(jl8);
) C4 A5 d& C4 e1 \jp8.add(jtf8);( {  R, T2 q! S1 w( |/ O) c* U
jp00.add(jp8);: C- e0 ^5 x' V
JPanel jp9 = new JPanel();: b. i$ U" S. q+ ^3 |) k9 c6 G
JLabel jl9 = new JLabel("公交站 ");
0 J2 u7 y# d1 K3 Cjp9.add(jl9);
' o6 v5 p# R9 W# I8 i. B4 ijp9.add(jtf9);3 Q1 P+ ]/ Q& M: y) z
jp00.add(jp9);, j2 }$ Z1 n; z% a  k; ]
jf.add(jp00);. `4 I: ?; l0 U/ \) Y
JPanel jp01 = new JPanel();
* b& n+ K% a( g: M5 ~jta10.setText("--用户使用手册-- \n1、查询:选择一个字段,如 EngName 在文本框中输入相应内容,点击查询\n2、增加:在各个文本框中输入相应内容后,点击增加。\n3、修改:先查询到你要的记录,在更改相应内容,点击修改。\n4、撤除:类似于修改操作");
6 `% A5 U9 s6 bjp01.add(jta10);
( K+ q) D( G: H( U! _. R6 }jf.add(jp01);
7 V5 A5 T2 {9 m5 p' q( V5 x/ vjf.setSize(700, 500);
( x: O' C2 X) J0 R5 h! y! ?jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);: L5 O8 M# r8 w* L
jf.setVisible(true);

}

public static void main(String[] args) {% A1 U* }2 r8 G
new EmpManageSys();
. c7 a' o) l7 F1 k  s, k}
, G: H/ J% w/ N& H0 X' k- p}


# ]  Y. F- j: t# h8 `1 J

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


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

29

主题

5

听众

183

金钱

三袋弟子

该用户从未签到

沙发
发表于 2014-07-28 19:36:24 |只看该作者
//---------------------------------------------------------------------
; I" B& _- [$ O+ rSQL工具类JdbcUtil:用于实现数据库连接和数据库关闭。上面程序用到了这个类
& C# b* Z# U8 E- p0 l. P% @import java.sql.*;
: ?2 d% y3 j, Q3 j1 Z! T6 Cpublic class JdbcUtil
/ q! |1 R4 U1 p2 N+ n5 g{+ P# F# c6 E0 H' |, x0 e/ ~; d
//load driver from file 5 ]8 O' c  [- P8 {( F" K) b: d
static{* w  F0 E: I" W- a6 R! f+ N
try{2 w! g5 ~& [$ r; ^4 \) r0 w/ U
String driverName =
7 E8 J4 D/ \& H# a"oracle.jdbc.driver.OracleDriver";
# m1 W7 \3 U7 @. oClass.forName(driverName);
9 e. b. O: M" z}catch(Exception e){
- R6 W% ?* j/ Xe.printStackTrace();$ N. `+ O! y! s6 c# P! M- c
}
5 `: \' ^7 s, }7 [}5 Q0 ^) H7 g; W- N" n4 n
//establish connection
! _: E- C: s+ [& h/ Qpublic static Connection getConnection(){) b4 h9 ^& v2 E9 F9 m" F, F
Connection con = null;
9 D7 ~* w& }- xtry{
( Z9 Z2 u8 C, p" NString url = 5 c+ L8 C( y  b* K& C+ m
"jdbcracle:thin192.168.0.26:1521:tarena";7 j5 H' k3 b+ K6 C5 R! l6 o! J8 X2 A! H
String usr = "openlab";
  C0 o9 N) Q0 X  ^$ j- rString pwd = "open123";4 @5 p4 A! G  q. d& J) P
con = DriverManager.getConnection(
, E  U0 z1 S: k' Durl,usr,pwd);
0 W' |; T3 E3 L* ~4 l}catch(Exception e){- F( Q) @+ ^: r8 O9 ^) H) h/ S
e.printStackTrace();
9 ^: q/ A, S/ G. n}; [3 m6 F. e( J: k1 L6 d
return con;# C/ q3 {& Z8 Q+ I( [' r6 G
}( d5 ]: s' R/ B6 C

+ }( a% ~5 E7 ~$ c3 B3 O; g, \* q: v//close resultSet,statement and connection
' p5 \+ G9 e' ^. a& Gpublic static void close(ResultSet rs,Statement stmt,Connection con){
9 ^4 U5 n: S, u" {1 V, ytry{3 R5 w1 I, t3 N1 b9 A
if(rs!=null) rs.close();
; Q' H7 b) @( z9 H0 R! N}catch(Exception ex){& B9 ~: ~7 j5 i. q+ q. w; T3 i
ex.printStackTrace();$ N5 B7 P: f# R8 Z& Q
}" o$ k) K- v/ ~* r1 b
try{- ~7 Q6 z% |* M
if(stmt!=null) stmt.close();; _5 z$ e/ l6 ~: T/ q8 |
}catch(Exception ex){
/ F2 \3 M! ?% Z5 ]5 c9 o  a  Cex.printStackTrace();
0 U+ D5 `1 A, |& }- I( w  I}
7 q& H- j8 q% ^& O& N9 s9 [) btry{# W' H7 H# H! l4 Y
if(con!=null) con.close();
' J+ S* j3 [3 t, ?. M4 C: L) H}catch(Exception ex){
# {8 T# Y$ Z! ]; aex.printStackTrace();$ V9 i1 z* }9 q* J$ i
}
7 Y4 V# g/ E! a; q; s}4 O* D0 E: }% {7 X/ j5 {
}
回复

使用道具 举报

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

   

关闭

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

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