// 项目目标:建立员工管理系统第一个版本。
0 G1 i6 s, f$ k. r' a: o* ?// 实现图形用户界面的员工信息查询、增、删、改;与数据库建立,并将修改结果时时保存到数据库。: k7 s8 B) X8 M. X
// 模块:1.GUI界面 2.数据库连接 3.查询 4.增、5删 6改(操作数据库) import java.sql.*;0 {6 X& N. I9 [8 F; y8 k3 M
import java.awt.*;
$ x% d4 U5 ^) o' P0 Fimport java.awt.event.*; import javax.swing.*; public class EmpManageSys implements ActionListener {7 b7 u E5 q" p f4 N3 [
Connection con;
" \5 m+ V @$ U5 bStatement stmt;
* n, s; a* T& F! a4 L0 _; \String sql;6 i% z- ?$ @" r1 t1 |; z! F
ResultSet rs;1 X2 ?3 a9 Q& w" t. ~; h
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) {
, g# L0 |, V4 h0 U* wString str = e.getActionCommand();
* {# m3 `8 A+ O+ e% R1 Hif ("查询".equals(str)) { //if check the button of 查询,then go to method of searchEmp()
' Z; ?/ u$ j8 A$ K. r# K* rsearchEmp();
) @8 E) ]7 K0 @' J! _. \" f. f+ N! D} else if ("增加".equals(str)) { //if check the button of 增加,then go to method of addEmp()! u' `# g7 t/ }$ H/ o
addEmp();& `; C' d3 F4 |+ Y& ~/ `( q
} else if ("修改".equals(str)) { //if check the button of 修改,then go to method of alterEmp()
! N. `$ ]* }/ Q5 lalterEmp();: ~% U: j6 n1 g7 F0 U. k
} else if ("撤除".equals(str)) { //if check the button of 撤除,then go to method of deleteEmp()2 W1 ?4 A1 u0 D+ V# t
deleteEmp();9 _8 y- Y8 ~( Y1 A
}
1 k# G% B2 l1 D$ ?} //this is the constructor. EmpManageSys() {9 F# {0 s5 ]6 h! o
createGUI();7 n7 u' C# H. b. c! O! e
connectToDataBase();
. j7 e: w$ {& t* u' ]/ @8 y# z1 _' [// searchEmp();
; \( P$ S V* o! u// addEmp();
, v& h4 g. u3 S! j- x// deleteEmp();% |; W, W( e0 ]* R. f: [
alterEmp();
: p$ y6 C4 t; X0 y} //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() {
$ F/ A. {% ]- B9 l" q1 Qcon = JdbcUtil.getConnection();
- A' I0 T6 t( ?1 Y/ wSystem.out.println(con);
8 C: W7 Z" F. mreturn con; } public void searchEmp() {
, ^2 D, j- \$ M" e! Z2 c5 {. Ljta10.setText("");
( l: o7 C; y) t6 h- O( Stry {
/ [6 s" F) [- C5 V; b# x1 ~! ?stmt = con.createStatement();
9 T w( F8 U. d// sql = "select * from Mstar where id=" + jtf3.getText();
3 } R; y" K; w9 I E// sql1="select * from sd100343 where ="+jtf3.getText();
" ]4 y+ E, {; j) W" G; U$ m6 zsql = "select * from Mstar where chineseName="
! M( [3 c5 n: Y+ [/ G& h2 s+ jtf2.getText().toLowerCase().trim() + "or id="
8 g8 w' L8 p3 K9 J# O; @/ P+ jtf3.getText().toLowerCase().trim() + "or engName="
/ g$ d t1 o x# Q+ jtf4.getText().toLowerCase().trim() + "or UNIT="8 X/ b% e+ ~5 R4 P# Q8 E0 O
+ jtf5.getText().toLowerCase().trim() + "or TEAM="
% e. Q8 _8 e+ D9 p+ jtf6.getText().toLowerCase().trim() + "or Phone="& E6 m/ C% U1 z$ ?! U7 Y
+ jtf7.getText().toLowerCase().trim() + "or region="
* {5 T7 P& s$ s. x+ k ]1 t+ jtf8.getText().toLowerCase().trim() + "or busStation="
8 q" l. m2 n+ S7 \$ |+ jtf9.getText().toLowerCase().trim();4 l) D* [4 W$ ~: ^* s
stmt.executeQuery(sql);
7 Z; l, \5 ]. `* m2 m T/ m Prs = stmt.getResultSet();
3 H/ s. b, m1 v+ AResultSetMetaData meta = rs.getMetaData();- n! P4 }7 ?2 M9 p
int cols = meta.getColumnCount();, }' @) f# @+ @ F# a/ P- j$ y% E5 j
while (rs.next()) {
6 K3 p' a/ ?5 P5 m5 V! gfor (int i = 1; i <= cols; i++) {
[' P' M% N+ e* z" hsb.append(" " + meta.getColumnName(i) + " =");
3 W" G1 G* V, |% X+ c) gsb.append(rs.getString(i));
( w+ G& q' o# Q7 m. R}& k9 d# z' ?' N6 u; _
sb.append("\n");; m& c9 {# }" y" X
jta10.setText(sb.toString());1 ]& q+ l, C% t. Y4 z2 c0 m6 e$ v
}( Y; x) O V" D& X/ u
} catch (SQLException e11) {; R% e; k4 b& r3 K6 V
e11.printStackTrace();
" m+ B* j+ G. N% c% G} } public void addEmp() {
# \7 k! Z: ]9 x. R: F; i% Ytry {
# g: @# e5 \$ `5 A/ O) Qstmt = con.createStatement();1 R& I2 ?* T" s1 J! x% Q
sql = "update Mstar values(" + jtf2.getText() + jtf3.getText()# X# L& d8 C- R
+ jtf4.getText() + jtf5.getText() + jtf6.getText()
4 Y* K4 n5 _) I) G+ jtf7.getText() + jtf8.getText() + jtf9.getText() + ")";
. p. y5 g/ k- s8 {int i = stmt.getUpdateCount();
. J7 U8 _5 m( Lif ((jtf2.getText() != null) && (jtf4.getText() != null)" z. l. R' M. f) ~$ ~
&& (jtf6.getText() != null) && (jtf7.getText() != null)) {
+ Q; S" {/ K( f/ s+ Cstmt.executeUpdate(sql);4 q% U4 O* E9 B3 Q1 `
jta10.setText("添加记录成功" + i + "条");
; g: _3 E" b. k: X$ g' R$ @} else {0 o) o2 k3 L. ^7 V& \
jta10.setText("带*号项为添加记录时不能为空");
& G2 [, I2 q# B# l {( n; l- c6 d}3 U4 M1 B0 k* E3 V8 V
} catch (SQLException e1) {
+ x- j" O$ N n4 F5 xe1.printStackTrace();
2 N' R$ ~; h* \" v: w" ~}% r2 [% N+ M$ ~: O" O
} public void deleteEmp() {
& {2 g% ~9 U) e; EsearchEmp();3 }# E" H }) O" g8 v* N5 `
try {: T$ z5 U9 l5 `" e
stmt = con.createStatement();
" E$ v7 f3 p' Z' C: i3 w msql = "delete from Mstar where chineseName="; B! I& [2 @1 V" `* o5 n
+ jtf2.getText().toLowerCase().trim() + "or id="
5 J4 `5 T) e0 O5 p. r$ [/ D+ jtf3.getText().toLowerCase().trim() + "or engName="
( H. `( u! z3 |/ |, }+ jtf4.getText().toLowerCase().trim() + "or UNIT="/ v/ l- q* T. Q2 C. d
+ jtf5.getText().toLowerCase().trim() + "or TEAM="% u0 D! G0 R1 W( r8 ~
+ jtf6.getText().toLowerCase().trim() + "or Phone="
# c0 ^# Y9 \9 f6 y+ jtf7.getText().toLowerCase().trim() + "or region="
* h( g, ~0 U% {, V% W0 M1 `) l4 G0 p+ jtf8.getText().toLowerCase().trim() + "or busStation="
) Z u9 J+ F) T/ @+ jtf9.getText().toLowerCase().trim();" {9 f9 ?; M( W( i$ G
stmt.executeUpdate(sql);) D& ?) k) s' v
int i = stmt.getUpdateCount();6 y' k }. Y* a! V1 h# |. H
jta10.setText("撤除操作成功" + i + "条");+ O5 J' Z0 H' E
} catch (SQLException e) {$ n* }. f( j# A% P6 F- M2 t
e.printStackTrace();
. \. c* M. e9 r# ~, ]3 N% n2 j4 Q$ x} } public void alterEmp() {
, s9 T1 U4 v9 c1 Y9 V, ~( H! _' m5 PsearchEmp();( @% h, h% W! S6 V1 S2 C" `
sql = "update Mstar set chineseName="* j: ^* I+ @: o* x
+ jtf2.getText().toLowerCase().trim() + "and id="
: ?2 i) C6 W8 R' V' L/ l. ]- e+ jtf3.getText().toLowerCase().trim() + "and engName="8 N2 g% j3 v) e% V. D
+ jtf4.getText().toLowerCase().trim() + "and UNIT="0 ?) r! a; k& @
+ jtf5.getText().toLowerCase().trim() + "and TEAM="
: j! h# v7 A3 i, t* E$ L+ jtf6.getText().toLowerCase().trim() + "and Phone="' O% h8 V# T6 y/ Q: y
+ jtf7.getText().toLowerCase().trim() + "and region="
' p6 L+ `# Z# n$ y: q7 j# g+ jtf8.getText().toLowerCase().trim() + "and busStation="
# q; j- _/ h$ i+ jtf9.getText().toLowerCase().trim(); int i = 0;
0 D& u* ^/ K/ _1 I8 \try {
- w* N, Q# h* t" v! U- wstmt.executeUpdate(sql);
! _/ S, K/ x$ s1 ki = stmt.getUpdateCount();# k3 k/ p: ~2 U: {' ?: o4 ?
} catch (SQLException e) {$ U# _: V* J- J6 O: a+ Y7 {# a% O
e.printStackTrace();
6 J M+ O# U z! R}
& K+ Z$ _ h) `; Qjta10.setText("修改操作成功" + i + "条");
; `1 U o7 V& K/ \5 n} public void createGUI() {$ X* M' @3 K7 M% I
JFrame jf = new JFrame("员工信息管理系统");
; O0 K# h2 ^. q6 ujf.setLayout(new GridLayout(2, 1));9 x) u( J5 q! m: e
// jf.setLayout(new GridLayout(10,2));- l$ P. r5 @! A, Q" p1 e4 A; o
JPanel jp00 = new JPanel(new GridLayout(5, 4)); JPanel jp1 = new JPanel();. s* E, Y! h4 q/ l% }1 c4 o
JButton jb11 = new JButton("查询");) s- O! k' A& l, N
jb11.addActionListener(this);
+ K, W& {; o# D4 z( K7 FJButton jb12 = new JButton("增加");
# V0 x) }" g5 O- ajb12.addActionListener(this);
) @0 X7 j+ l3 e! M6 E" b' ujp1.add(jb11);
$ y7 f2 d5 h* i" k3 p3 g" }jp1.add(jb12);
& _! N$ j8 x. A8 vjp00.add(jp1);
& N5 j' Y* r* L/ K' QJPanel jp11 = new JPanel();2 k+ x3 w0 x4 Z1 s% A7 ]
JButton jb111 = new JButton("修改");: y/ x& N% Y/ ^$ X1 z# ?% j
jb111.addActionListener(this);
% D. |; W: B! A: d) \ PJButton jb112 = new JButton("撤除");2 e" a) o% B' W2 X( f
jb112.addActionListener(this);" _. q8 k7 o6 e0 p
jp11.add(jb111);) P% _; w) i/ ]9 u
jp11.add(jb112);
1 V) X$ U# n# C) r" Mjp00.add(jp11);/ s) ]$ i( V# m5 [* q4 x0 ]; D/ L G# K
JPanel jp2 = new JPanel();" s; d( b& }, N
JLabel jl2 = new JLabel("中 文 名 * ");2 p' _9 Q+ ~7 x$ Y, z
jp2.add(jl2);
, ~3 P) x, F, R5 l p4 ejp2.add(jtf2);
6 u) |' t( ]2 N6 d, m; vjp00.add(jp2);4 t# E9 N4 V% o3 N: i9 Z
JPanel jp3 = new JPanel();# f# @5 F# i0 W! t9 I1 h5 R) c4 t
JLabel jl3 = new JLabel("工 号 ");
( I+ j, h2 ]9 w; d% Ejp3.add(jl3);. X# D2 l' b6 k
jp3.add(jtf3);/ i* c+ O, d) l& o: Y
jp00.add(jp3);
, V) J& u5 @1 J# f0 @# ?JPanel jp4 = new JPanel();
+ H. j/ U) O+ L) D' i; xJLabel jl4 = new JLabel("EngName*");
^0 v$ i# H# f. Q% t8 c4 D4 z/ ujp4.add(jl4);+ W/ E! {0 O& r. ?& @
jp4.add(jtf4);
# x4 I0 S2 [& B( A: L! A, H+ djp00.add(jp4);; ^9 d) t2 K) U3 \+ }
JPanel jp5 = new JPanel();: g1 Q! f* Q L, [: v, K3 j; H
JLabel jl5 = new JLabel("UNIT ");# `& o% Q) N) V1 E! Z
jp5.add(jl5);; t8 ^5 F9 J# g' ^
jp5.add(jtf5);
* v! G W, k( X7 I6 Ijp00.add(jp5);7 c. q; E. X! y- g- A* r$ b
JPanel jp6 = new JPanel();% j4 l$ `/ j, V8 s+ k% q
JLabel jl6 = new JLabel("TEAM* ");
/ U8 ` I. s7 D/ z$ H" k' g1 K- Tjp6.add(jl6);
7 p9 O: B4 X+ F0 {, A% n' Q6 M# cjp6.add(jtf6);
5 G& O* H, y$ y8 F0 g8 O; ijp00.add(jp6);! S. a# w5 a+ ]
JPanel jp7 = new JPanel();
, a {6 M T9 K2 X6 a# z2 kJLabel jl7 = new JLabel("Phone* ");
; W5 b$ c3 y& yjp7.add(jl7);
; ]9 u$ u1 r% |jp7.add(jtf7);
( r; _) Q2 X+ l, z' g4 s6 qjp00.add(jp7);% H$ z# }9 g) x+ d* T
JPanel jp8 = new JPanel();, r, E; S' }, W! P2 m7 Q+ L
JLabel jl8 = new JLabel("区域 ");
0 j y3 B) ~4 L- Z- U2 C. Qjp8.add(jl8);
$ x; ?4 A. f. w5 |8 i/ f* C8 n* Wjp8.add(jtf8);
# E0 ~5 P. x( s& x/ j( ?jp00.add(jp8);
) F! M+ b2 Y" bJPanel jp9 = new JPanel();
0 q& l, O' g8 K5 b& ?! |JLabel jl9 = new JLabel("公交站 ");1 h* ]8 y) i2 |) R; S# M
jp9.add(jl9);; ?: Z. Z/ ^. [% k* d; t9 `
jp9.add(jtf9);) _2 b2 i2 r4 S9 R' T, J
jp00.add(jp9);
/ I6 }! b; k( `% jjf.add(jp00);+ `* C! \7 L( s% ^
JPanel jp01 = new JPanel();2 S$ p) D7 e3 c7 ^3 X
jta10.setText("--用户使用手册-- \n1、查询:选择一个字段,如 EngName 在文本框中输入相应内容,点击查询\n2、增加:在各个文本框中输入相应内容后,点击增加。\n3、修改:先查询到你要的记录,在更改相应内容,点击修改。\n4、撤除:类似于修改操作");
5 J+ O. T' B( [7 y0 w7 E, njp01.add(jta10);# x D |! M6 s- I% E& y
jf.add(jp01);
+ |: u7 ^* |! w3 T: C: R3 r l7 fjf.setSize(700, 500);/ @ D$ l; W, Y" O2 K" B1 r" o3 q
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+ [" E& Q8 T) o' E8 H. vjf.setVisible(true); } public static void main(String[] args) {5 d) N( T0 \$ |. V7 ]1 B
new EmpManageSys();- T$ r: @& a: Z+ k0 Z% ` o
}
w: N3 b. a5 G) w* c) k}
9 B |3 }+ ]2 w+ x) N |