|
// 项目目标:建立员工管理系统第一个版本。
3 k: y3 H5 `- R( r, o// 实现图形用户界面的员工信息查询、增、删、改;与数据库建立,并将修改结果时时保存到数据库。
( Q9 b& I" G9 K' C2 y1 W; I// 模块:1.GUI界面 2.数据库连接 3.查询 4.增、5删 6改(操作数据库) import java.sql.*;! g( l' Z( h0 S- q
import java.awt.*;
4 G' R1 }5 r) U0 i9 Y8 s3 X. c* ^import java.awt.event.*; import javax.swing.*; public class EmpManageSys implements ActionListener {
+ }" V/ J9 [% ?+ K; wConnection con;
4 o* f5 u) x" M0 {" a6 M8 n2 A+ QStatement stmt;
% v% T4 g/ F9 [. Q. OString sql; \# p6 R2 n: L4 T4 ^; m
ResultSet rs;6 M- v% d) t9 J, i8 p+ m8 e
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) {
, n) B. M5 K" DString str = e.getActionCommand();5 e1 k" _! F- k/ o2 g: {' W. `
if ("查询".equals(str)) { //if check the button of 查询,then go to method of searchEmp()
4 \' N7 g3 A O! m4 w9 k+ UsearchEmp();
% U( s" L0 r c9 `* j. g} else if ("增加".equals(str)) { //if check the button of 增加,then go to method of addEmp()
. O$ a; O$ d9 T* t6 xaddEmp();3 Z6 T @% Q; c% M5 V& G
} else if ("修改".equals(str)) { //if check the button of 修改,then go to method of alterEmp()/ T c( Q+ k8 X T0 }2 W
alterEmp();
1 S* e/ Y4 O, W# B6 |1 k/ z} else if ("撤除".equals(str)) { //if check the button of 撤除,then go to method of deleteEmp()4 ?% c3 |3 J" F% ~- t: t
deleteEmp();
3 v6 ?9 `/ W" Y! u6 E}4 F" j& {' {+ \% w) ^& m
} //this is the constructor. EmpManageSys() {0 X6 k6 `7 r: v& F8 e% Q- d
createGUI();- Q2 t9 ?: ]4 R0 T4 H8 ]. X
connectToDataBase();
/ p; ~' a- R6 B- ~2 E- A' @// searchEmp();
0 t; {* `9 m6 I& P// addEmp();
" C8 w ?" b) @: e, I. E// deleteEmp();1 A9 f+ b9 e& g; ^! x4 ]4 Q2 c3 [
alterEmp();
9 b5 X- |! b5 {; a} //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() {
* u2 x2 B t6 g; |* Q0 x7 Ycon = JdbcUtil.getConnection();5 d$ ?4 W$ g, U; |4 c
System.out.println(con);1 v6 l0 t B* T& Q' E& n+ G# }
return con; } public void searchEmp() {
, A( o F$ m' f# [jta10.setText("");* g- v, E1 Q" t
try {& Z' T7 N; U( v
stmt = con.createStatement();7 M% m9 w. b* n6 Q6 |; v, L) W
// sql = "select * from Mstar where id=" + jtf3.getText();! V0 U$ n* e I( `# }
// sql1="select * from sd100343 where ="+jtf3.getText();. B$ _" x# }- K ?
sql = "select * from Mstar where chineseName="
7 @0 t" J% S. g8 K- C+ jtf2.getText().toLowerCase().trim() + "or id="
2 [* y" P1 H, n7 B8 Q5 h7 x+ jtf3.getText().toLowerCase().trim() + "or engName="
% `' P7 O+ D" p- R5 Y+ jtf4.getText().toLowerCase().trim() + "or UNIT="% v4 P9 A# L1 g2 V6 i a3 p
+ jtf5.getText().toLowerCase().trim() + "or TEAM="% g& M& Q$ Z7 k7 Y9 S, N# @6 c
+ jtf6.getText().toLowerCase().trim() + "or Phone="0 v+ d0 Z1 U1 U* l8 c) h
+ jtf7.getText().toLowerCase().trim() + "or region="
$ W; S6 X$ S3 p) s8 `: t+ jtf8.getText().toLowerCase().trim() + "or busStation="
: m: ~4 J$ P, `0 r# U! ?+ jtf9.getText().toLowerCase().trim();
: d! {0 y1 a R2 R# F' hstmt.executeQuery(sql);' h7 k# x; g& r" N
rs = stmt.getResultSet();
# z2 ?; ^* `6 u( J* TResultSetMetaData meta = rs.getMetaData();; n( P' V. U, G4 G$ o% w
int cols = meta.getColumnCount();
# Q6 r+ T' k9 j3 hwhile (rs.next()) {# q! Z; B$ Y/ s e' j5 k% H* a
for (int i = 1; i <= cols; i++) {
, v" H$ W+ a" e. Dsb.append(" " + meta.getColumnName(i) + " =");" `% Q+ |4 T4 e0 P* x- e
sb.append(rs.getString(i));
D6 u( [7 ^% ^8 J p8 l% R}
( |1 f5 s" ?: B' ?$ e1 Asb.append("\n");" f. V/ h; B3 W- ^3 p7 m
jta10.setText(sb.toString());8 W* r, z% P& t5 x4 H
}* N/ z, L2 w6 J: U' k4 u
} catch (SQLException e11) {. `' \+ Y. A0 Q/ k/ l! n
e11.printStackTrace();/ F) b: ?7 @; D+ e" P2 W5 c# |( r
} } public void addEmp() {
# j2 k: s1 C- Y" Z" M0 t8 Ftry {
% w. H5 z1 d7 }" ~7 d; E$ B6 E# gstmt = con.createStatement();
5 j& z2 }7 n' A9 N" Rsql = "update Mstar values(" + jtf2.getText() + jtf3.getText(): U" Q+ m, e3 x; U
+ jtf4.getText() + jtf5.getText() + jtf6.getText(); S; Z5 q( ?' \0 \& F! ~ a6 n5 n
+ jtf7.getText() + jtf8.getText() + jtf9.getText() + ")";
: n& k0 U: j4 f# f/ ?% f' Jint i = stmt.getUpdateCount();
! t( X4 l2 L! Y! l2 ?if ((jtf2.getText() != null) && (jtf4.getText() != null)
' ?" T- e+ {- q L&& (jtf6.getText() != null) && (jtf7.getText() != null)) {
$ _. X8 Y( C3 t, H. u: v/ j* astmt.executeUpdate(sql);
9 A e3 Y5 o wjta10.setText("添加记录成功" + i + "条");
- w# V, Y ?5 @} else {
# R" l* M2 x) L6 i1 Jjta10.setText("带*号项为添加记录时不能为空");$ ~4 d. R8 {5 J& K% v4 G- E. E1 \
}9 F$ g# ]5 [' ^4 {
} catch (SQLException e1) {
& S, }& c F) m& W2 F( ~0 J( ae1.printStackTrace();
# O+ u: Y0 E* E9 L}
6 r- t1 X6 q! m} public void deleteEmp() {
+ [/ \) Y5 G( ~$ I! K3 FsearchEmp();8 `# }; k( u! E( W" V
try {# u, {- ] D- z/ \ J3 h# Y
stmt = con.createStatement();, f( f/ X. m( t8 o( d
sql = "delete from Mstar where chineseName="3 X& Q% O* N; p5 P6 `9 j6 j
+ jtf2.getText().toLowerCase().trim() + "or id=". W0 G9 @3 V+ G4 L
+ jtf3.getText().toLowerCase().trim() + "or engName="( r* H1 A$ z) f$ O4 t
+ jtf4.getText().toLowerCase().trim() + "or UNIT="
5 f( `' m# k! S, Z7 I% W: Z+ jtf5.getText().toLowerCase().trim() + "or TEAM="8 |. y$ K1 ~& h& _ M: S7 u
+ jtf6.getText().toLowerCase().trim() + "or Phone=": N+ h) }0 j6 a A3 k! B D
+ jtf7.getText().toLowerCase().trim() + "or region="
9 r x$ a/ [7 n) s9 F+ jtf8.getText().toLowerCase().trim() + "or busStation="
9 Q0 h- n; |) A1 e+ jtf9.getText().toLowerCase().trim();0 X6 W" H) T1 D. S- \ t7 b
stmt.executeUpdate(sql);. U" m6 }9 j. c) G# y% s
int i = stmt.getUpdateCount(); `9 g; ]0 y' z. Q6 r: j
jta10.setText("撤除操作成功" + i + "条");$ T7 x0 ]" G5 B8 @
} catch (SQLException e) {
8 k' t4 }, b9 B! d. ~e.printStackTrace();
$ R' u/ p$ Z8 m9 q} } public void alterEmp() {. ^+ u- m& m: c1 H, k2 v- @2 {; @8 R
searchEmp();
) Y8 W! r/ q: L8 \$ n3 q% h) fsql = "update Mstar set chineseName=" I: \# e: k) p. X# }% E. @ q/ h, @
+ jtf2.getText().toLowerCase().trim() + "and id="
# _3 j* R* j j) E- W. t+ jtf3.getText().toLowerCase().trim() + "and engName="
2 W5 W J7 K" I0 F8 z+ jtf4.getText().toLowerCase().trim() + "and UNIT="+ {8 _5 [$ @; T
+ jtf5.getText().toLowerCase().trim() + "and TEAM="' z: m. B N3 ?, O1 x) ~' Y" y% `
+ jtf6.getText().toLowerCase().trim() + "and Phone="+ r: J! S* G r4 @# }( q
+ jtf7.getText().toLowerCase().trim() + "and region="0 w) @) k" H* T5 U4 K
+ jtf8.getText().toLowerCase().trim() + "and busStation="5 n: q- c* M% T. s4 q( _2 q
+ jtf9.getText().toLowerCase().trim(); int i = 0;0 y" }) ~7 q5 [: k; ]# b: O% _
try {. S* C4 O+ R& \: z4 \3 u5 X' Y$ I( M3 a
stmt.executeUpdate(sql);3 E; |9 U. \; N; y
i = stmt.getUpdateCount();
$ F0 Y1 M6 x4 U} catch (SQLException e) {
6 i ]/ u! Q8 [& L& we.printStackTrace();! O! c6 L& B2 s) @9 _# V+ \& \( f& K
}
$ K3 v) Z$ n, x6 S* Zjta10.setText("修改操作成功" + i + "条");3 Y/ T" N! a& f
} public void createGUI() {
K- Y7 ~. k& [6 f* J( W" k& TJFrame jf = new JFrame("员工信息管理系统");, G, s$ v0 S* `' h5 g
jf.setLayout(new GridLayout(2, 1));3 K8 f1 n2 k! k6 H, D2 d- [ s2 L: z
// jf.setLayout(new GridLayout(10,2));! z: j# r- h) C9 K# o
JPanel jp00 = new JPanel(new GridLayout(5, 4)); JPanel jp1 = new JPanel();$ E$ U! @2 \8 R1 {7 d t
JButton jb11 = new JButton("查询");/ N: Z% o" _" ^4 E" m
jb11.addActionListener(this);
?8 L3 @0 c2 V! N2 y; _* f5 F; kJButton jb12 = new JButton("增加");
, ]9 g# ]' T( j7 U# ]+ `jb12.addActionListener(this);+ ?9 w$ {, D9 G0 t) E
jp1.add(jb11);
/ I* z2 |9 _3 J9 c [+ X! f/ s! pjp1.add(jb12);
" m% p V$ K) [jp00.add(jp1);1 w, S: G0 k. H; G
JPanel jp11 = new JPanel();/ Y5 }" B# u: o! B# I& I8 A
JButton jb111 = new JButton("修改");
! S8 i1 Q9 w2 b. n) Yjb111.addActionListener(this);, ^9 f, D1 V( X5 D! u/ ?
JButton jb112 = new JButton("撤除");
# B) b( L, o3 F% Hjb112.addActionListener(this);( M8 I6 R& \* F6 c6 t
jp11.add(jb111);- D+ x; N, R( {" Q* x
jp11.add(jb112);
! e5 q) n( g9 ~/ X Ojp00.add(jp11);
; P; d/ z9 U! K" H$ p+ bJPanel jp2 = new JPanel();- w' B. ?# e1 O8 ]! W1 [; ^
JLabel jl2 = new JLabel("中 文 名 * ");( | _, [+ Y0 P" h: \
jp2.add(jl2);
( O- N% R* I; \" o Ejp2.add(jtf2);
/ p7 ]6 M6 k8 v8 o! ]- s/ Mjp00.add(jp2);- S4 t- x8 G1 @2 ^) e" ~2 H
JPanel jp3 = new JPanel();; g$ u! V# v0 ^( }
JLabel jl3 = new JLabel("工 号 ");) m& a' d y% o
jp3.add(jl3);5 G( w) P0 z" G h7 w: l3 Q: d
jp3.add(jtf3);
5 u6 S3 L: S! Mjp00.add(jp3);* n' Y! k) z9 B6 T6 u. c: L
JPanel jp4 = new JPanel();+ j" Y. s0 \" n+ t I9 L1 B
JLabel jl4 = new JLabel("EngName*");
$ M/ N% l- P6 M! z& Q2 t+ djp4.add(jl4);2 O' u( h/ v( L0 z5 V6 K* ]4 F# G
jp4.add(jtf4);
0 R9 d7 u f* ]% _+ y3 B4 `; Hjp00.add(jp4);. r1 r' W3 Y& x% ?
JPanel jp5 = new JPanel();
: [+ R6 Q0 v% ^9 t$ X! dJLabel jl5 = new JLabel("UNIT ");
( o8 |0 O' S y; \jp5.add(jl5);
2 t" f+ F5 h! V$ `jp5.add(jtf5);. ]0 N/ j3 O+ u
jp00.add(jp5);
' W2 y" E% y& b9 S5 ^JPanel jp6 = new JPanel();
g! B4 u% ?; sJLabel jl6 = new JLabel("TEAM* ");) p" i& e) n' k1 }) ~1 p# E
jp6.add(jl6);
1 p0 j: Q7 O1 v5 w3 _4 l3 q$ f4 ujp6.add(jtf6);
+ m$ i& l! F1 A1 u0 jjp00.add(jp6);9 y0 A: T' X1 `% K: ~ J& X; H- n# ], X
JPanel jp7 = new JPanel();
; j# t' [6 Q0 pJLabel jl7 = new JLabel("Phone* ");" X$ U% i8 I7 C! }6 Q6 f$ \% I
jp7.add(jl7);
$ q6 H$ R4 H) {) A' G/ S Zjp7.add(jtf7);
* y6 P. F1 q8 ?6 njp00.add(jp7);
9 N* @/ a8 ?% R6 ~6 U# Y5 K1 dJPanel jp8 = new JPanel();4 F( |7 k8 v9 d1 r, D, M! [
JLabel jl8 = new JLabel("区域 ");
" G& x5 r8 o( x0 Mjp8.add(jl8);
/ a$ X1 J6 F5 \2 n, a. n0 E/ Ojp8.add(jtf8);
2 T) U. R* I% N% c) g' U. xjp00.add(jp8);4 Q: ?3 Z2 V, p" ?) V+ z1 u/ z/ a
JPanel jp9 = new JPanel();1 ]8 L$ A6 R4 L, [2 J( l$ c
JLabel jl9 = new JLabel("公交站 ");+ q" O0 E! n' Y, U% Q/ F* i
jp9.add(jl9);
. u8 i0 W5 l( v6 v7 |9 {jp9.add(jtf9);* ~7 D, L U, I' O% `7 }7 m5 i
jp00.add(jp9);
; W3 R1 J. e1 I6 Tjf.add(jp00);
# X) T2 {6 {5 I) `3 xJPanel jp01 = new JPanel();2 y+ U$ n! L% l9 a p- s8 z
jta10.setText("--用户使用手册-- \n1、查询:选择一个字段,如 EngName 在文本框中输入相应内容,点击查询\n2、增加:在各个文本框中输入相应内容后,点击增加。\n3、修改:先查询到你要的记录,在更改相应内容,点击修改。\n4、撤除:类似于修改操作");5 U$ q4 }2 F. X
jp01.add(jta10);
4 B4 C5 C5 h; y! `5 ~# f7 Ujf.add(jp01);4 \, c) u1 x9 X6 q$ @! Y( [
jf.setSize(700, 500);; _/ ^( Z. A3 |7 ~
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
3 L: P- i# o! @2 m+ l+ _6 Y! _2 hjf.setVisible(true); } public static void main(String[] args) {
% D4 p" h0 S F, z: _+ @% ?7 inew EmpManageSys();
3 D/ \+ P6 ~* S4 m7 A7 y- z6 R}
/ ]! d) ~3 t- R9 n: ?} 7 ?5 F( f/ S8 A( A+ K3 `; ?
|