// 项目目标:建立员工管理系统第一个版本。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/ [ |