// 项目目标:建立员工管理系统第一个版本。) c# G& o! D& q4 X
// 实现图形用户界面的员工信息查询、增、删、改;与数据库建立,并将修改结果时时保存到数据库。
+ K4 J/ I8 s0 F/ t' Q& R// 模块:1.GUI界面 2.数据库连接 3.查询 4.增、5删 6改(操作数据库) import java.sql.*;; q; T4 w2 x: l2 A
import java.awt.*;
- w8 ^: u& G/ G, r3 H& [: `import java.awt.event.*; import javax.swing.*; public class EmpManageSys implements ActionListener {
- d+ G0 u* |% ]Connection con;( V0 m% o1 l" I& }5 g6 w
Statement stmt;
& |# X2 v) x/ U4 ~String sql;
. g- b1 Y5 Q9 F1 Y! n$ pResultSet rs;
$ _9 B$ f; F3 Y' f: }- \* VStringBuffer 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) { H, I; b) N2 ^
String str = e.getActionCommand();, G* r8 F6 a# Q& F- @1 E4 R! f7 K
if ("查询".equals(str)) { //if check the button of 查询,then go to method of searchEmp()
# c/ U. o0 D2 E9 E9 Q# w y; KsearchEmp();
- G7 q/ J; W. [- G% `} else if ("增加".equals(str)) { //if check the button of 增加,then go to method of addEmp()
3 j( W2 C* @) G2 A( vaddEmp();8 ~$ d/ L+ U: g8 v& E6 _
} else if ("修改".equals(str)) { //if check the button of 修改,then go to method of alterEmp()
( `8 D5 E! O! y7 _alterEmp();
+ ]* @1 ]9 G' F5 ]. G z7 _} else if ("撤除".equals(str)) { //if check the button of 撤除,then go to method of deleteEmp()
9 t1 R0 |& N" }: z9 _7 ~deleteEmp();
- O* e; f: ~3 `/ }9 z* z; O}
4 u$ [5 y3 C3 H2 C, d' v, m} //this is the constructor. EmpManageSys() {
) U0 d# }$ p, GcreateGUI();; V6 F2 C/ Y8 t9 i7 U3 r: m
connectToDataBase();+ V2 b8 F2 w( H
// searchEmp();: u: j- L7 I0 f- }
// addEmp();
* N: Q% ^$ Y& F+ m* Q _" @# P// deleteEmp();
+ S; g2 s' Q* Q7 malterEmp();1 D% j3 r0 q) Z3 o
} //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() {
* f2 U0 {+ W A$ ]6 {con = JdbcUtil.getConnection();
, p% f( v1 f& l6 W' n7 L/ z U# ^System.out.println(con);8 g( S7 N) a- j5 c. G1 k
return con; } public void searchEmp() {
% Z6 M3 B2 {; } njta10.setText("");6 H; }0 M/ V% C' R& @' e5 Z
try {; J8 d; C2 p' N: |. p2 d
stmt = con.createStatement(); W! T! t6 x5 q+ \7 ^
// sql = "select * from Mstar where id=" + jtf3.getText();
( V3 @ i+ Y3 c0 l// sql1="select * from sd100343 where ="+jtf3.getText();* ^; q( I* e! C
sql = "select * from Mstar where chineseName="
' P; N2 i% b/ J+ a! D4 m+ G+ jtf2.getText().toLowerCase().trim() + "or id="
. S3 b5 Y" J1 L7 p9 a1 z+ jtf3.getText().toLowerCase().trim() + "or engName="
) ?8 ]9 L, D; [& S9 z# z+ jtf4.getText().toLowerCase().trim() + "or UNIT=", l6 R! c1 l k6 u" W `
+ jtf5.getText().toLowerCase().trim() + "or TEAM="
* j7 ^9 _, M' Z5 V+ jtf6.getText().toLowerCase().trim() + "or Phone="
8 }. L/ R7 W' R" ?9 \& z! u- p+ jtf7.getText().toLowerCase().trim() + "or region="2 }2 ?% D" R( o5 I
+ jtf8.getText().toLowerCase().trim() + "or busStation="
n: E2 P6 e+ z' C4 P6 L+ jtf9.getText().toLowerCase().trim();
, o$ |5 r. m5 U4 ?( N0 [stmt.executeQuery(sql);5 @) z/ V! g) |4 j+ L2 o
rs = stmt.getResultSet();" h/ g9 p+ C4 l5 \. ]; \, h+ ~
ResultSetMetaData meta = rs.getMetaData();
: Z8 g( L& H' g2 |5 g' G. }$ }int cols = meta.getColumnCount();
! s+ n, y R) A2 c$ y5 z ~- dwhile (rs.next()) {
8 y) Y. V9 x1 Z! O, cfor (int i = 1; i <= cols; i++) {( A# h* b1 E& L( U9 a; e( z: ]
sb.append(" " + meta.getColumnName(i) + " =");/ }* F, M: b" g/ R, \$ E' Q
sb.append(rs.getString(i));
4 y3 l9 ^: V7 d1 e$ l}
+ n) s$ T7 V4 R( u0 \) C) isb.append("\n");; |. O# ?7 p* Q% \+ m' }+ w1 _
jta10.setText(sb.toString());
p% w6 y' O% h3 Z9 ^- }}' M8 D9 j) {0 q5 p4 z& U
} catch (SQLException e11) {
1 Y4 w2 v P( {2 U+ y" b We11.printStackTrace();, O7 S4 g: ~8 C
} } public void addEmp() {
* V9 k$ O, d7 K" C/ v% ctry {! E6 m9 }# F7 P- a4 C/ `
stmt = con.createStatement();' x: [- [. g3 i# {% [& @
sql = "update Mstar values(" + jtf2.getText() + jtf3.getText()+ S9 a/ s0 A' p
+ jtf4.getText() + jtf5.getText() + jtf6.getText()
& ?1 B/ w c5 p( S+ jtf7.getText() + jtf8.getText() + jtf9.getText() + ")";
3 c; R3 j1 Z) eint i = stmt.getUpdateCount();% i D1 L6 ?4 u$ O6 ]+ w6 e
if ((jtf2.getText() != null) && (jtf4.getText() != null)1 {( {9 V2 C9 m& q7 {; M
&& (jtf6.getText() != null) && (jtf7.getText() != null)) {
( d9 ]! P+ @5 o8 o2 B7 `stmt.executeUpdate(sql);
5 a+ H; H8 S h& x0 x6 X+ wjta10.setText("添加记录成功" + i + "条");
3 e# x! E7 z8 l. F8 [/ E} else {
* U0 v9 K+ v" l7 y' X hjta10.setText("带*号项为添加记录时不能为空");( c. ~" Q7 V3 j5 @" @0 |) @
}
6 [/ n3 |- U! J6 [! K3 \} catch (SQLException e1) {1 O! N8 w! i% T1 X
e1.printStackTrace();+ g* K% ~0 o6 Z9 f* e
}
! X% k: z, p7 T; ]7 }} public void deleteEmp() {' A$ O: u# [! }7 {
searchEmp();
' g) O' D; {' W, D* }try {1 O& H6 w: D6 n2 M
stmt = con.createStatement();
$ \2 n d x% C4 U+ hsql = "delete from Mstar where chineseName="% R C H8 V) P5 F
+ jtf2.getText().toLowerCase().trim() + "or id="9 S) `1 A8 a K0 X, }+ w
+ jtf3.getText().toLowerCase().trim() + "or engName="3 y0 A( W' {* H
+ jtf4.getText().toLowerCase().trim() + "or UNIT="
- L% V0 O8 b. A) p/ K- x+ jtf5.getText().toLowerCase().trim() + "or TEAM="0 ?6 J& V/ |/ J
+ jtf6.getText().toLowerCase().trim() + "or Phone="7 s" P2 Q/ }. R! h d
+ jtf7.getText().toLowerCase().trim() + "or region="
, e! ]( s8 j6 ^) M( L+ jtf8.getText().toLowerCase().trim() + "or busStation="
& C) Q$ x3 d/ v$ |; @. L+ jtf9.getText().toLowerCase().trim();
7 b6 @5 G% r; U1 [8 tstmt.executeUpdate(sql);
6 a& p) |' A! c6 J* I3 H( F) Cint i = stmt.getUpdateCount();
# h) L8 ~$ M4 [. m+ W1 fjta10.setText("撤除操作成功" + i + "条");
- o+ j# R& k) ]8 E} catch (SQLException e) {2 F0 g5 h3 ~+ L, ?. V( h
e.printStackTrace();
. A: M& d5 ]1 Y4 q9 P8 M( G( z} } public void alterEmp() {, {( Z/ D: Y( V: U# q
searchEmp();
) q( {' N5 y6 j- A# Z6 Isql = "update Mstar set chineseName="
$ w9 `8 B% K3 h: j- P4 Y+ jtf2.getText().toLowerCase().trim() + "and id="
; Q0 t5 h* j0 \; t# F: Q( h3 n+ k+ jtf3.getText().toLowerCase().trim() + "and engName="
! {+ c; I, t2 _1 y7 i+ jtf4.getText().toLowerCase().trim() + "and UNIT="4 B; [& z# R1 y& v
+ jtf5.getText().toLowerCase().trim() + "and TEAM="
! d B) s, k! _8 s" o3 V- B+ jtf6.getText().toLowerCase().trim() + "and Phone="6 h* O# \$ @% Y9 G* @
+ jtf7.getText().toLowerCase().trim() + "and region="
0 G0 I6 Y! w$ K6 l+ jtf8.getText().toLowerCase().trim() + "and busStation="9 y5 K1 n9 l. ]" j
+ jtf9.getText().toLowerCase().trim(); int i = 0;2 h6 F% J9 k3 ~+ Y
try {
3 K* O6 ~1 t- C" z, H0 y9 Wstmt.executeUpdate(sql);
5 v. h2 `3 }2 @1 q- i+ vi = stmt.getUpdateCount();+ g* @6 i" T+ b8 y. \
} catch (SQLException e) {
8 u: x0 X+ s5 R. `" B) r, y& x& j: W- @0 q2 Ae.printStackTrace();
+ d: a' ~; K4 h$ t) n' R! e}! Z5 s- u8 R, H# t& T, d2 {$ i
jta10.setText("修改操作成功" + i + "条");
5 T& L' h' C8 | V} public void createGUI() {$ h" G' y; }4 @# \) `% L
JFrame jf = new JFrame("员工信息管理系统");( b1 ~' v" ^; f. u
jf.setLayout(new GridLayout(2, 1));
! o4 U( g) ?! s& s// jf.setLayout(new GridLayout(10,2));
3 [1 U2 f1 i3 i1 _1 N1 |9 LJPanel jp00 = new JPanel(new GridLayout(5, 4)); JPanel jp1 = new JPanel();
3 u; F* s4 n! E1 _' _2 tJButton jb11 = new JButton("查询");* ~7 O9 a/ Q* l& L. R2 m9 r
jb11.addActionListener(this);. ~, C* e# V# o; c; q
JButton jb12 = new JButton("增加");
3 j, T, ?) a) k7 gjb12.addActionListener(this);
6 R: R. V+ H' N( j/ kjp1.add(jb11);
, e2 }% o% Z/ r6 p* m$ E7 L ejp1.add(jb12);) d, U! `4 p/ d" M
jp00.add(jp1);
! ?* q4 d6 K+ f. sJPanel jp11 = new JPanel();. D2 H$ y+ W% L2 `4 t/ w" g6 p0 _
JButton jb111 = new JButton("修改");! m* s6 w8 H$ Y5 m
jb111.addActionListener(this);
5 B- v3 ?# G2 m: {: cJButton jb112 = new JButton("撤除");5 K1 f# ^! l" z
jb112.addActionListener(this);
3 f0 e/ M, J0 v& c; ?) K: tjp11.add(jb111);
5 r4 P. a2 m7 Y1 W% g x; Jjp11.add(jb112);/ Y! Z K* X6 i+ {, b% P) x& o
jp00.add(jp11);- X* K2 ~1 X F
JPanel jp2 = new JPanel();
' C$ U9 A0 m' n# E( {- z) n) P; R' L; sJLabel jl2 = new JLabel("中 文 名 * ");
L- g" `& t6 b) ^* e3 J% U w3 v# ?jp2.add(jl2);2 u7 i9 H! ?! l4 A! A# r2 l
jp2.add(jtf2);
4 l2 Y: T* m; m8 a! Rjp00.add(jp2); n3 J2 E5 u2 C
JPanel jp3 = new JPanel();
, m* D$ r4 g( N- g6 ]. S- v ~3 ZJLabel jl3 = new JLabel("工 号 ");# z& f0 S) w& P
jp3.add(jl3);# E: E3 h$ }- f/ c# [( Z
jp3.add(jtf3);
7 j" c3 ]. y3 w c' [jp00.add(jp3);/ {- t. m$ ^7 K7 ?7 X
JPanel jp4 = new JPanel();
) P; e7 A0 k( ^JLabel jl4 = new JLabel("EngName*");
9 h0 `$ D, N( W1 Ejp4.add(jl4);
& U/ \4 x2 Z8 R+ rjp4.add(jtf4);
. z; V' l0 Q- u" rjp00.add(jp4);$ B3 X. n! I: n& L1 D5 y: F
JPanel jp5 = new JPanel();% D' _) ]" W: b* L0 F
JLabel jl5 = new JLabel("UNIT ");
1 ~* L& g& H: U5 d3 d1 }1 l& L; P! hjp5.add(jl5);
+ M. ~- U- P$ Q) q# H% ajp5.add(jtf5);$ r3 X9 g" j1 u1 ~* ?/ f
jp00.add(jp5);2 w. ~8 e3 g5 }2 c4 [
JPanel jp6 = new JPanel();
9 Q, Y+ o ?5 T C1 i2 X: \JLabel jl6 = new JLabel("TEAM* ");
! v% }9 f& m4 u4 A5 u' }4 pjp6.add(jl6);( i+ n8 f9 Z5 ?6 m R
jp6.add(jtf6);& i1 U4 V& K' X1 }: z
jp00.add(jp6);" W7 V6 p) P5 h( l" R
JPanel jp7 = new JPanel();
0 V I8 r( L) u" g9 QJLabel jl7 = new JLabel("Phone* ");
: O y" r6 J6 y2 ^( H: mjp7.add(jl7);
! ^, m3 C% X7 o! X7 Qjp7.add(jtf7);
) i" z1 c l6 x0 z* {- Qjp00.add(jp7);
0 N4 q7 j" p( m/ bJPanel jp8 = new JPanel();
$ [6 o8 E1 l( ~+ J3 d: a0 y9 T8 bJLabel jl8 = new JLabel("区域 ");
j; z/ q" s& \( c0 ^jp8.add(jl8);5 D0 J% a, r& B" F
jp8.add(jtf8);4 ]% v0 e, `' S' Q. l
jp00.add(jp8);0 }' O: l9 Y6 ] w1 G" l
JPanel jp9 = new JPanel();
2 a) K4 H# x9 ]9 A5 FJLabel jl9 = new JLabel("公交站 ");, K% t3 @, J# ^
jp9.add(jl9);
D7 f3 c) g9 o/ }jp9.add(jtf9);
, c8 v1 |7 I. q0 _' gjp00.add(jp9);0 L( x2 l: d% x
jf.add(jp00);
: D% d% e7 ~2 }* @) u& WJPanel jp01 = new JPanel();! }/ Y: s: d+ y- _2 J
jta10.setText("--用户使用手册-- \n1、查询:选择一个字段,如 EngName 在文本框中输入相应内容,点击查询\n2、增加:在各个文本框中输入相应内容后,点击增加。\n3、修改:先查询到你要的记录,在更改相应内容,点击修改。\n4、撤除:类似于修改操作");
4 w& n5 ^8 I2 t( j2 L0 A# Rjp01.add(jta10);9 w7 I" h- r. K) [/ Z. s4 G
jf.add(jp01);
+ i+ m2 S# l# j. r& Ljf.setSize(700, 500);
5 H( r+ ?( H9 m* @5 gjf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);! W. p7 q. t+ ]
jf.setVisible(true); } public static void main(String[] args) {' V4 T z: }+ G4 e
new EmpManageSys();% E$ r8 ]- n) b& X
}9 g+ ]$ d- Q) f* n$ t) w" ~6 |1 S
} b4 V+ H. r7 w! x4 t$ x
|