// 项目目标:建立员工管理系统第一个版本。
2 M! u: R) X4 d// 实现图形用户界面的员工信息查询、增、删、改;与数据库建立,并将修改结果时时保存到数据库。
' D; D' \0 Y2 x$ u) P// 模块:1.GUI界面 2.数据库连接 3.查询 4.增、5删 6改(操作数据库) import java.sql.*;
' Q! V8 p! Y9 s" \/ B$ Cimport java.awt.*;$ z# A! X: `' [9 I% G" \
import java.awt.event.*; import javax.swing.*; public class EmpManageSys implements ActionListener {: P! Q( U5 f. h; L/ P
Connection con;
9 N- l5 J& ^* p9 @' C) B. LStatement stmt;3 g& U8 b3 ^# i% `
String sql;& i$ w1 t, W3 E* u* m
ResultSet rs;
1 G, r. G( ]+ B) q0 |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) {* ?5 Q. ]! \, O
String str = e.getActionCommand();; ?4 H/ v; v9 b+ C
if ("查询".equals(str)) { //if check the button of 查询,then go to method of searchEmp()- n4 Z# Z \: F& Z2 R6 J! a
searchEmp();. {9 j( }8 {. o& \& W$ `& G
} else if ("增加".equals(str)) { //if check the button of 增加,then go to method of addEmp()9 a# ?, ?" Q9 ]" P' W, Y( D! w
addEmp();# Y3 V$ m8 ^1 |) z7 y* l
} else if ("修改".equals(str)) { //if check the button of 修改,then go to method of alterEmp()
8 Q9 c% O) w* C+ z0 h- o# \# [alterEmp();& `& A. g% Z$ B
} else if ("撤除".equals(str)) { //if check the button of 撤除,then go to method of deleteEmp()5 l0 _" M$ S! A, {
deleteEmp();, o; y A+ r! ?! i& b# s
}
* Y2 j9 c; D5 F} //this is the constructor. EmpManageSys() {# H! n% ~9 S# f/ {7 M" s
createGUI();
7 _3 i$ H [* S# s$ cconnectToDataBase();
8 Y! o3 ]: _2 s, ]& y, d// searchEmp();7 ^% t+ p; V& I7 M w
// addEmp();- O9 j) X) {$ L" x
// deleteEmp();
1 Q$ Y7 B8 C# L/ }' ~/ YalterEmp();
5 P& L! T I: D! o" S5 _- 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() {
+ N- b* U6 I* t7 | H. Gcon = JdbcUtil.getConnection();
/ Y' ^# R2 U2 b& r! S( jSystem.out.println(con);( X y2 [# ~* G: `, n2 }5 p) m
return con; } public void searchEmp() {8 n7 a! l. c' k3 V! n0 G m
jta10.setText(""); X4 Y- c/ [9 R7 M* Z# Z$ a! F
try {
- B9 l8 P9 x0 p" _4 {stmt = con.createStatement();
1 M" J- s7 Z+ U& S6 [1 G) W// sql = "select * from Mstar where id=" + jtf3.getText();
5 a) O8 W2 @: z M; z9 c0 O// sql1="select * from sd100343 where ="+jtf3.getText();
$ _" [' q) f/ E* w1 z" M9 o( P/ t5 E3 gsql = "select * from Mstar where chineseName="2 Q5 P$ j% n; C3 T# {+ t
+ jtf2.getText().toLowerCase().trim() + "or id="4 g) O5 L$ C, M" h: D- d
+ jtf3.getText().toLowerCase().trim() + "or engName="' p( j0 b' t; G; y2 }& {- C
+ jtf4.getText().toLowerCase().trim() + "or UNIT="
/ v. k; P9 K/ q+ G+ jtf5.getText().toLowerCase().trim() + "or TEAM="
6 T; n' V3 M: o Q- n$ g+ jtf6.getText().toLowerCase().trim() + "or Phone="$ z8 U: g- e% D6 S$ v. G
+ jtf7.getText().toLowerCase().trim() + "or region=" `$ M0 x& ]; {7 n" i
+ jtf8.getText().toLowerCase().trim() + "or busStation=". r' I. C/ Q9 p) ^2 @/ o2 X! U
+ jtf9.getText().toLowerCase().trim();
$ \5 M# N( ?& a" @ ostmt.executeQuery(sql);' Q* u% Z1 j; E b3 z+ `* \! G
rs = stmt.getResultSet();2 k) Y S! u6 m( b6 ]2 c
ResultSetMetaData meta = rs.getMetaData();, E1 L) [5 _5 p# ?5 x9 t" v0 K9 j
int cols = meta.getColumnCount();
0 }8 C# V( u. _1 h# hwhile (rs.next()) {
% s) p9 ~( \8 h3 T$ b- M4 S0 yfor (int i = 1; i <= cols; i++) {+ I* G3 O$ d, c+ y
sb.append(" " + meta.getColumnName(i) + " =");
+ o) C) S7 P2 U6 k. q: F1 B6 q. |% Lsb.append(rs.getString(i));7 ^1 y& e: z6 U1 s- {. p) K8 v
}
/ w3 |) H% u5 Ksb.append("\n");4 Z3 c# c4 P2 ^# M/ t; {
jta10.setText(sb.toString());, C3 C1 S7 Y" ~7 N
}
2 x4 E5 W$ R1 t ~! v+ M$ q! G} catch (SQLException e11) {
' _3 M- a( E! T2 a4 |2 q& be11.printStackTrace();3 E) z5 G3 A1 X0 A- e
} } public void addEmp() {
. ^' a q% L% P! m/ h, Dtry {5 Q& T$ ]* Y( @$ J
stmt = con.createStatement();
/ ?& w! @) H# z8 Y1 Xsql = "update Mstar values(" + jtf2.getText() + jtf3.getText(). b3 i0 f: M Q( D; _
+ jtf4.getText() + jtf5.getText() + jtf6.getText(), K2 a) N7 L8 ]9 n) @. C8 S7 m6 _
+ jtf7.getText() + jtf8.getText() + jtf9.getText() + ")";# t" V6 L4 I# I7 _
int i = stmt.getUpdateCount();! s7 Q2 w& A- H0 u* {
if ((jtf2.getText() != null) && (jtf4.getText() != null)
0 J i w: z3 K3 ]&& (jtf6.getText() != null) && (jtf7.getText() != null)) {
: b" a% {$ Y( T( \ Dstmt.executeUpdate(sql);
2 v9 S- w+ X9 D! R/ gjta10.setText("添加记录成功" + i + "条");
$ w. `# \4 g6 D( Q `3 E6 \. t} else {1 O* k8 D x# }* l0 q. D
jta10.setText("带*号项为添加记录时不能为空");
' A; C0 U3 o n: W" `}, L7 `' r+ I, M! m
} catch (SQLException e1) {
" S. R& s1 q. V2 @* @# j5 d6 fe1.printStackTrace();* u+ u6 x' W* ?" ~, m5 w
}
. |7 F1 v/ l* Q# K* L1 Q& C: o% |% l. l} public void deleteEmp() {) p% p$ E0 r8 a! c8 I
searchEmp();5 d: L$ {) P' O: K g5 {+ P
try {2 r. }$ i6 |$ A' N1 r5 v
stmt = con.createStatement();
- A. D& N, A4 B9 gsql = "delete from Mstar where chineseName="; l8 E" [4 m6 n; Z
+ jtf2.getText().toLowerCase().trim() + "or id="
0 r# j5 G* }% `, B: A$ E ]! K7 h+ jtf3.getText().toLowerCase().trim() + "or engName="
8 m2 P) N5 a$ [* Q; D2 h" t+ jtf4.getText().toLowerCase().trim() + "or UNIT="
' h7 K @+ X6 g6 \6 z; A+ jtf5.getText().toLowerCase().trim() + "or TEAM="
( M7 {0 B( ^- ]5 N; [& B: _; z+ jtf6.getText().toLowerCase().trim() + "or Phone="
; D1 f# p0 Y* x1 T" g+ jtf7.getText().toLowerCase().trim() + "or region="" D' U2 p0 l% u) i
+ jtf8.getText().toLowerCase().trim() + "or busStation="
" K$ J' d- V" h+ jtf9.getText().toLowerCase().trim();
1 k+ K% c: q; F, Pstmt.executeUpdate(sql);: Q& }+ M- j2 F3 W5 `7 Q$ e& G
int i = stmt.getUpdateCount();
% g' x* O: d( s* v' k' pjta10.setText("撤除操作成功" + i + "条");
- `6 ~0 A! Y; f) u4 |* i/ r) f} catch (SQLException e) {
2 C4 B2 R8 x# {/ h- i6 ee.printStackTrace();2 p) Y; Q0 R( s0 B6 m2 N" ^& g9 O' ]
} } public void alterEmp() {6 ]0 X/ ]/ v, D* `
searchEmp();
. V5 a m7 @5 O! Wsql = "update Mstar set chineseName="5 F5 z5 U/ j x* H1 t* V: `, y& F% H
+ jtf2.getText().toLowerCase().trim() + "and id="( G1 k, A% e7 ^3 q
+ jtf3.getText().toLowerCase().trim() + "and engName="7 t3 G; i l% B+ }) C0 Y5 b3 v
+ jtf4.getText().toLowerCase().trim() + "and UNIT="4 q1 o0 Z& i3 O* ^2 j& C
+ jtf5.getText().toLowerCase().trim() + "and TEAM="
! s( @8 u( _8 C+ R9 D6 C+ jtf6.getText().toLowerCase().trim() + "and Phone="
% p+ P/ W1 s# {- H2 q5 M( t+ jtf7.getText().toLowerCase().trim() + "and region="7 q* ]; P" [' y* I
+ jtf8.getText().toLowerCase().trim() + "and busStation="
0 Z k& o$ U, x: Z& i3 w+ jtf9.getText().toLowerCase().trim(); int i = 0;
1 d8 F9 B- u( X l* z. _9 btry {, k) J9 J: l9 B t
stmt.executeUpdate(sql);
8 ?) _/ D6 w; ?i = stmt.getUpdateCount();/ ^9 F8 m0 q5 N% A: Y1 q
} catch (SQLException e) {
( X$ E6 n9 j Te.printStackTrace();+ V [$ x. ^# f6 K' k! H
}# `7 r$ |8 @( _* t9 @9 a
jta10.setText("修改操作成功" + i + "条");; |+ }8 o& E: w. f
} public void createGUI() {& Z- F0 K' q' q6 _& B* q8 x
JFrame jf = new JFrame("员工信息管理系统");
) [: j: j" E9 T8 [; Z! @$ Z% K7 ^; Rjf.setLayout(new GridLayout(2, 1)); F) q5 c7 c/ [) F w, Y2 |1 S
// jf.setLayout(new GridLayout(10,2));
- M4 H% E; p" I- mJPanel jp00 = new JPanel(new GridLayout(5, 4)); JPanel jp1 = new JPanel();
9 {0 U$ R1 [$ y6 A7 T" U. fJButton jb11 = new JButton("查询");# ]5 h% B/ S- f' F7 u8 ]
jb11.addActionListener(this);' U8 t7 c v* _2 z: o# k5 F: f# c
JButton jb12 = new JButton("增加");
% k! M. ?7 G7 u# R$ B: X$ ?jb12.addActionListener(this);% ~0 Y) P% }# L( V) l1 t9 g
jp1.add(jb11);& ^2 I% C6 |8 I$ n" G
jp1.add(jb12);
/ |3 R5 @1 t8 |6 i) ?% H& g. S9 tjp00.add(jp1);8 {. ]* s: b0 H* a7 g) T# J5 }8 k
JPanel jp11 = new JPanel();
* ^% |& d( _4 l5 u, Z3 H& _JButton jb111 = new JButton("修改");+ {+ G, i- L6 y4 `
jb111.addActionListener(this);
) o$ _& f7 E/ C+ u& n# ]3 tJButton jb112 = new JButton("撤除"); p$ d: M4 \$ i
jb112.addActionListener(this);4 b% h! ?/ E* p" N3 p
jp11.add(jb111);
3 `' P& A; y5 A" Vjp11.add(jb112);* v, z" y: D x2 l
jp00.add(jp11);& N# i6 c. h+ s9 i! e& t
JPanel jp2 = new JPanel();
0 J3 [$ j% Q6 B: A4 f: P- l" WJLabel jl2 = new JLabel("中 文 名 * ");
2 J* I# [: Z- I3 K# |0 U* Mjp2.add(jl2);8 q! E: j3 U0 N$ ~7 P- l
jp2.add(jtf2);, P8 W+ ~3 s% n2 c- t4 I( h6 F
jp00.add(jp2);
+ C W/ l8 ~) D$ b+ YJPanel jp3 = new JPanel();
) l( p4 H8 a/ H- b1 K ?; |JLabel jl3 = new JLabel("工 号 ");+ A/ C! B" y) t% c' f K% d
jp3.add(jl3);
! y7 G- U/ f& ?jp3.add(jtf3);
U8 D0 ^. k! p! u5 y+ rjp00.add(jp3);6 I3 j, i. e( n) D/ y4 p
JPanel jp4 = new JPanel();
0 H5 ^' x h+ S& hJLabel jl4 = new JLabel("EngName*");4 r/ _+ C E$ W3 t+ A6 @# [
jp4.add(jl4);
% e3 a0 w' U6 `" @jp4.add(jtf4);
# s& n% c0 O: L1 ajp00.add(jp4);4 F( U! i8 D& a* r* d3 ?2 q; y; q* A: y
JPanel jp5 = new JPanel();/ w6 H6 Y) n" } d. O9 e% y
JLabel jl5 = new JLabel("UNIT ");
5 G4 q7 `4 D+ h. t+ k" q5 Ijp5.add(jl5);) r9 n$ t' y1 X8 {% M, Y
jp5.add(jtf5);
8 M4 D) N6 f6 W i! l) o& r4 \jp00.add(jp5);
$ U. t5 ]% Z! DJPanel jp6 = new JPanel();3 B& N/ ]; d( s! p0 N: Q+ M
JLabel jl6 = new JLabel("TEAM* ");$ q+ a& f$ R$ a9 Q
jp6.add(jl6);9 H$ _, X# P4 I) k
jp6.add(jtf6);$ S2 b( b# ~; m2 V8 Y
jp00.add(jp6);
0 e3 R% h# h8 x5 k5 a6 |JPanel jp7 = new JPanel();
* `/ L ]! A. W6 R& kJLabel jl7 = new JLabel("Phone* ");8 K6 d# R" x5 M' u* A$ B) Y& U
jp7.add(jl7);
$ B, B% B4 {( F% Tjp7.add(jtf7);
2 |5 n+ u7 U0 p0 Wjp00.add(jp7);6 S8 x# R/ A4 b3 x3 I6 {
JPanel jp8 = new JPanel();
; b, X+ A5 Q& L# n- d w& o& Z# gJLabel jl8 = new JLabel("区域 ");; H9 h: J; R# E: ?" ~ H4 y/ q
jp8.add(jl8); B/ j4 R0 B0 ] j5 w" V. X& d
jp8.add(jtf8);( Q( r, o {; K, k# D% z
jp00.add(jp8);% @1 h6 M: p4 K, L
JPanel jp9 = new JPanel();1 D1 W8 S) v) @! @: T) D, Z
JLabel jl9 = new JLabel("公交站 ");' h) I; I& n$ I$ t; Y
jp9.add(jl9);$ `. I5 O( ~& h" b7 P
jp9.add(jtf9);
& } _' a7 i4 q0 u$ A( b4 @2 Njp00.add(jp9);
! n. i/ \/ a4 n. bjf.add(jp00);
( S2 H; A/ G2 N$ C( ^JPanel jp01 = new JPanel();4 T7 K0 o# p3 R& T. C5 c
jta10.setText("--用户使用手册-- \n1、查询:选择一个字段,如 EngName 在文本框中输入相应内容,点击查询\n2、增加:在各个文本框中输入相应内容后,点击增加。\n3、修改:先查询到你要的记录,在更改相应内容,点击修改。\n4、撤除:类似于修改操作");% x- M0 ?3 I2 e8 e/ Y/ \
jp01.add(jta10);
0 C* e) @( `6 }9 }) ^jf.add(jp01);- T2 t ^: G% x' ~3 o7 m
jf.setSize(700, 500);
& k* N3 ^* y# m0 ?# w3 ~: ~jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
; X5 K1 n; L9 v3 a! U" {# X! s+ Jjf.setVisible(true); } public static void main(String[] args) {/ T, N( E3 `8 @% T( Q3 d7 r
new EmpManageSys();, M6 K& N& r2 V/ @& J6 |% K
}7 A4 T$ W+ l/ U$ l U% b; d( G0 X6 a
} 8 u$ Q* ]$ n' r; c) p: T+ o1 n$ K
|