// 项目目标:建立员工管理系统第一个版本。 l. O% ?: ^, o
// 实现图形用户界面的员工信息查询、增、删、改;与数据库建立,并将修改结果时时保存到数据库。" b+ B6 S9 m+ X; d8 K2 D5 d3 J b
// 模块:1.GUI界面 2.数据库连接 3.查询 4.增、5删 6改(操作数据库) import java.sql.*;# E; x- @1 H4 h
import java.awt.*;
0 J7 T$ |* i7 w6 C1 y' Fimport java.awt.event.*; import javax.swing.*; public class EmpManageSys implements ActionListener {
4 w+ U+ w/ T0 K+ a7 QConnection con;7 G" J. h, U/ R+ _
Statement stmt;& J+ V2 g' m2 T8 V
String sql;& _0 k8 S9 \/ X, F5 D) H1 M: K% h
ResultSet rs;
5 G \( r0 W8 ?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) {7 ~5 w: o4 I" v# K
String str = e.getActionCommand();9 ]5 n) u) E& D% {5 ?$ m
if ("查询".equals(str)) { //if check the button of 查询,then go to method of searchEmp()0 i$ f2 M; ?9 ], j/ g' S u$ {* o
searchEmp();
: Z9 d8 P, K x3 n5 \' C} else if ("增加".equals(str)) { //if check the button of 增加,then go to method of addEmp()
8 v3 n( p' e5 k: e% \0 S$ YaddEmp();' z& q# K, L2 z
} else if ("修改".equals(str)) { //if check the button of 修改,then go to method of alterEmp()! h! n; h# s2 ^- L# v X
alterEmp();
) X7 i8 C: N6 g2 m7 U: J} else if ("撤除".equals(str)) { //if check the button of 撤除,then go to method of deleteEmp()0 \$ s0 \/ ]' G5 ]( o
deleteEmp();8 T3 t( @! ^) b
}6 [3 R* S' x( \9 }. i" @" U! o' O
} //this is the constructor. EmpManageSys() {" |" E' ^4 [: L p: p% W/ y9 l
createGUI();
& S! h, }$ ?# }; AconnectToDataBase();
/ m) b( S# x+ C5 H3 I! b2 y// searchEmp();
+ d E; r' j( k1 _) ^& a7 F6 {// addEmp();
8 a0 Z$ A' O3 z// deleteEmp();
. g3 t. @% |" K& }7 P+ y4 R4 E, jalterEmp();
: D+ z) T V1 j/ 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() {! i( C4 `3 |6 U5 `( N3 Z, J6 L( G
con = JdbcUtil.getConnection();& W0 A- E9 M7 q! M
System.out.println(con);
+ C+ D, ?( J% B. ]3 Qreturn con; } public void searchEmp() {/ Y; Q$ E2 D4 G1 n4 j N. E
jta10.setText("");
* ~3 k6 {: }& Dtry {
& H# [; P6 f2 Sstmt = con.createStatement();5 N- z3 f* H2 i& B
// sql = "select * from Mstar where id=" + jtf3.getText();; V F0 s' U! H& J8 _3 [
// sql1="select * from sd100343 where ="+jtf3.getText();1 J! @( o* b0 d5 ?0 I, P
sql = "select * from Mstar where chineseName="
$ h% m9 W2 B6 U+ jtf2.getText().toLowerCase().trim() + "or id="9 a; Q! r* t4 b' b& F
+ jtf3.getText().toLowerCase().trim() + "or engName="
, J3 C4 M- F8 A1 \) b' w. J+ jtf4.getText().toLowerCase().trim() + "or UNIT="$ O% }( i$ i1 b6 K6 e7 l/ Q7 H
+ jtf5.getText().toLowerCase().trim() + "or TEAM="
' O* ]/ j5 a4 Q5 r! S- ?+ jtf6.getText().toLowerCase().trim() + "or Phone="
& V2 p) Y$ ^# s; a$ c/ d% L+ jtf7.getText().toLowerCase().trim() + "or region="! ~ S" F# g3 M7 d% b& v' g3 d
+ jtf8.getText().toLowerCase().trim() + "or busStation="
z* x. k: G4 `6 O) A* Q+ jtf9.getText().toLowerCase().trim();
9 }- O% l" }$ Z: i a5 `' w+ f( _stmt.executeQuery(sql);6 k1 e# j' a8 t) h
rs = stmt.getResultSet();/ O# t% o0 ?) E6 M) F2 E0 m- Z
ResultSetMetaData meta = rs.getMetaData();
# Q9 S' g: ^( V/ ?2 c2 gint cols = meta.getColumnCount();
& [- O+ \7 W o' Q Fwhile (rs.next()) {
% D2 K& D2 R% M% Ifor (int i = 1; i <= cols; i++) {1 {5 m+ S8 T6 z Y4 R( k: y
sb.append(" " + meta.getColumnName(i) + " =");' m" Q, `& V+ x% m# b; D
sb.append(rs.getString(i));
# Y0 P7 G" h( K* v% D6 o} E. P: y! o) k+ b; z$ J
sb.append("\n");) ~, B1 _8 F% a: A$ G! B
jta10.setText(sb.toString());
5 V6 m- R! I1 `( x9 D% F# S}6 c8 `0 a s( x! e
} catch (SQLException e11) {
/ X8 E/ J4 D. C# Be11.printStackTrace();2 T( t/ _& x/ d3 S
} } public void addEmp() { I# [* V: Y% X, Z4 g
try {! U ^ c' i% z1 S
stmt = con.createStatement();9 {! g0 v8 h: k4 Z
sql = "update Mstar values(" + jtf2.getText() + jtf3.getText()6 y1 r: x+ @* n( \
+ jtf4.getText() + jtf5.getText() + jtf6.getText()
( {- l& S, D7 Y5 i, {; y+ jtf7.getText() + jtf8.getText() + jtf9.getText() + ")";6 E& A* \( J. e+ c4 K
int i = stmt.getUpdateCount();/ X) w7 L* v9 |8 r/ v7 X
if ((jtf2.getText() != null) && (jtf4.getText() != null)
. t! }6 u- _6 _# V0 Z7 r&& (jtf6.getText() != null) && (jtf7.getText() != null)) {$ t/ e! A1 R4 J
stmt.executeUpdate(sql);, O% ]& @% G9 F; q3 t- C! i
jta10.setText("添加记录成功" + i + "条");
7 N- q& B. h4 b: ^! ~} else {
" h/ m ~7 x& [* D* }! mjta10.setText("带*号项为添加记录时不能为空");+ F& v( N! m: R* u: ^% |
}% O7 O6 E7 q6 c1 D
} catch (SQLException e1) {
( W; G2 x$ A8 |e1.printStackTrace();& K1 p6 K( t* j1 c9 f, `6 F& v P
}! {$ `! ^3 s! {0 U. y
} public void deleteEmp() {' x) O' J( k! m; q7 M2 i
searchEmp();/ {: h0 z q. ~% r5 _: \" i- a
try {5 A8 I8 [( L6 c$ a. L" }/ a, |
stmt = con.createStatement();5 ^ c0 K* S) r& @2 e
sql = "delete from Mstar where chineseName="+ I& P9 g5 m/ P# P3 s0 D' Y- B
+ jtf2.getText().toLowerCase().trim() + "or id="
" s0 l/ L% H, p4 B2 c* a, ^, B+ jtf3.getText().toLowerCase().trim() + "or engName=", i- l& d+ C- P6 f* P! y* N% j
+ jtf4.getText().toLowerCase().trim() + "or UNIT="% V2 l2 i2 ?3 n2 ~0 _
+ jtf5.getText().toLowerCase().trim() + "or TEAM="
$ ]7 `* X3 f. Z5 `) ?6 S+ jtf6.getText().toLowerCase().trim() + "or Phone="/ k6 f6 ~3 f% B; s
+ jtf7.getText().toLowerCase().trim() + "or region="
# d& R% a3 {7 \; l: V4 c+ jtf8.getText().toLowerCase().trim() + "or busStation="
1 R7 ^* |* g. C+ jtf9.getText().toLowerCase().trim();5 t0 ]0 a1 `% [
stmt.executeUpdate(sql);
8 D( c R0 r& A+ c, R; ?' u/ ?* T3 gint i = stmt.getUpdateCount();; ~% V" E( X4 e, ~7 e& P9 N) e$ X
jta10.setText("撤除操作成功" + i + "条");
0 f# B" l- r- A} catch (SQLException e) {
/ M9 u; `1 c! x# _. We.printStackTrace();# b8 N* M' `$ b
} } public void alterEmp() {5 m' T& l- c3 `0 C; u, A
searchEmp();( I* t: v5 L/ ~2 d, T2 l# h
sql = "update Mstar set chineseName="
4 ^1 e7 t" {) _ v+ jtf2.getText().toLowerCase().trim() + "and id=". D1 ^# |' u3 }( u, J2 E9 S
+ jtf3.getText().toLowerCase().trim() + "and engName="" D- z$ |3 X9 y8 D
+ jtf4.getText().toLowerCase().trim() + "and UNIT="
4 k& b& B- U& T+ r& ^5 ?+ jtf5.getText().toLowerCase().trim() + "and TEAM="& v+ g- Z1 ]% a
+ jtf6.getText().toLowerCase().trim() + "and Phone="
# W6 u, j/ ^ _! u' C) i+ jtf7.getText().toLowerCase().trim() + "and region="* Q0 x" h3 p" A! [( T% [( M. U9 V
+ jtf8.getText().toLowerCase().trim() + "and busStation="5 V9 e. L I1 y8 h! {! k
+ jtf9.getText().toLowerCase().trim(); int i = 0;9 Q: o) f+ _1 g1 |5 E
try {" T0 Q h; r g' C
stmt.executeUpdate(sql);
. `2 b/ b/ N+ C9 _2 }i = stmt.getUpdateCount();6 L/ f) @* H; L) b- F
} catch (SQLException e) {
1 s( K9 a) N( V: De.printStackTrace();' ?1 ]" J9 |' g& \" ^% F; `
}
4 P* q: ]9 M: }* zjta10.setText("修改操作成功" + i + "条");7 g/ G0 j; R6 f/ X! v) {
} public void createGUI() {% j7 J8 D; k; ` w3 Z2 I
JFrame jf = new JFrame("员工信息管理系统");. B& H2 Z) P# k6 w" g' }" ]
jf.setLayout(new GridLayout(2, 1));
3 \7 t( ~* U% F a9 o* {// jf.setLayout(new GridLayout(10,2));
: z3 x2 y% g. K9 \( jJPanel jp00 = new JPanel(new GridLayout(5, 4)); JPanel jp1 = new JPanel();
) {3 u9 `# C6 ?% `6 \ B5 L/ OJButton jb11 = new JButton("查询");: ]# s& F1 k1 C; {3 k
jb11.addActionListener(this);" t# r K5 I' F: V6 ^% X0 ?# }
JButton jb12 = new JButton("增加");: m1 E$ Z6 }) y% e, k& N) [
jb12.addActionListener(this);
5 ^! {4 @0 b" H2 Q1 q: m4 ?jp1.add(jb11);
N& R6 J! H& [8 U/ T0 W, s! ^0 zjp1.add(jb12);
; z- o8 ?& K& v2 w+ L; @jp00.add(jp1);+ l' p* S x& ]# `
JPanel jp11 = new JPanel(); U& j J' a, R S
JButton jb111 = new JButton("修改");
# x# t3 m$ O# e" V6 Ejb111.addActionListener(this);% N, z1 U8 |% b& w1 Q+ Z6 n* v
JButton jb112 = new JButton("撤除");0 F% Z8 h5 y$ T3 @ y# k
jb112.addActionListener(this);# o- F# X% B: _/ i$ E; I$ ^
jp11.add(jb111);
4 a3 X1 A5 r' n3 `1 ] c2 Ejp11.add(jb112);
2 H/ K* C5 [. x0 W" Wjp00.add(jp11);9 a3 v! D: K* t: z3 G
JPanel jp2 = new JPanel();
5 `; y# T L7 t0 a5 }5 lJLabel jl2 = new JLabel("中 文 名 * ");6 X* Q4 L7 e9 ]. o z G& u
jp2.add(jl2);1 ]: c; r, S l$ d
jp2.add(jtf2);
3 v" v, N; [5 c8 y, pjp00.add(jp2);
! Z, u. t' P5 i# \JPanel jp3 = new JPanel();) ^, g. }# p6 @8 l2 d+ ~$ r3 t
JLabel jl3 = new JLabel("工 号 ");
2 d! q( `8 u; i8 b: m4 P# Ajp3.add(jl3);
* J( |" ?3 }2 s& S% H) ]! N& b/ Ujp3.add(jtf3);
" z1 p [! g4 z0 _$ u2 @jp00.add(jp3);) \: \% Y' \! K7 m
JPanel jp4 = new JPanel();* n D" I9 V0 G. j
JLabel jl4 = new JLabel("EngName*");$ |; J1 ?6 D% c& k; a1 E
jp4.add(jl4);9 J) Y- F, L& w1 w
jp4.add(jtf4);) z& d7 F1 Q5 A* Z7 b/ l6 E
jp00.add(jp4);4 ]8 X+ h+ }- O
JPanel jp5 = new JPanel();
) a' n" U" f5 k" ZJLabel jl5 = new JLabel("UNIT ");' j7 n _5 k, G4 ~6 {/ {4 w
jp5.add(jl5);, I( W q/ o3 W9 L' D- K
jp5.add(jtf5);2 z6 q! y! l. e2 Z
jp00.add(jp5);! j3 c# u7 X& g& V, E4 f5 b, e3 V
JPanel jp6 = new JPanel();
' f. \! I4 H1 kJLabel jl6 = new JLabel("TEAM* ");7 c N i) B* j2 Z
jp6.add(jl6);
. f- V1 w, S7 j9 k mjp6.add(jtf6);
$ S9 \; X F# G. `+ l- F3 j% O" Qjp00.add(jp6);7 G" D0 I3 n6 h( Q9 ] X
JPanel jp7 = new JPanel();
0 p; Z# g1 E, uJLabel jl7 = new JLabel("Phone* ");( g3 { N6 I. \1 j u% {
jp7.add(jl7);% y1 i5 ?5 e9 F0 w
jp7.add(jtf7);
! @0 s. E! t$ q4 T, o2 k* [jp00.add(jp7);
1 Y# d0 R5 Q3 l* X& E8 a; y# k$ IJPanel jp8 = new JPanel();
% u" }/ Z8 w8 O5 oJLabel jl8 = new JLabel("区域 ");9 ~8 E/ z8 u O u' I- b# o
jp8.add(jl8);
2 ^4 B* q3 L2 D* s1 D1 _1 Gjp8.add(jtf8);! j, Q5 @' @( [, b' f5 W! @6 S3 o
jp00.add(jp8);' E$ o$ o! k4 p& P6 M& e
JPanel jp9 = new JPanel();6 M% K4 B3 q! I: Z: i( c% k2 Z/ e
JLabel jl9 = new JLabel("公交站 ");
7 ~8 m% I; h8 C; ^; Q# W1 I, c2 bjp9.add(jl9);% L0 h$ c2 N2 g, Z+ s% K
jp9.add(jtf9);
# t2 }1 Y! f. ^* Ajp00.add(jp9);& O5 G7 c1 z* \# z
jf.add(jp00);5 G" e( ]- l, K$ J" B4 q/ D+ D
JPanel jp01 = new JPanel();
, p }7 m v' A8 Wjta10.setText("--用户使用手册-- \n1、查询:选择一个字段,如 EngName 在文本框中输入相应内容,点击查询\n2、增加:在各个文本框中输入相应内容后,点击增加。\n3、修改:先查询到你要的记录,在更改相应内容,点击修改。\n4、撤除:类似于修改操作");% |$ _. j; ^: W% C& B- p G" @9 w
jp01.add(jta10);' F* B% E& D2 B, V1 Y* X
jf.add(jp01);
5 j2 W' C$ ?/ y3 K, njf.setSize(700, 500);
, |! I" g6 \3 p M, C7 Tjf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
( r8 V# l2 K( W; l% K8 W2 Njf.setVisible(true); } public static void main(String[] args) {$ e% w' O, T- M+ e6 i+ z. m0 r! n
new EmpManageSys();1 Z9 O! \& _7 n
}
2 X1 Q: {1 f) n4 A) g}
! F9 g5 E9 ~$ i9 [0 q3 q |