// 项目目标:建立员工管理系统第一个版本。
. ~5 [! y2 |$ H// 实现图形用户界面的员工信息查询、增、删、改;与数据库建立,并将修改结果时时保存到数据库。
' o* E G; [+ j4 C3 F2 I2 i! L; `// 模块:1.GUI界面 2.数据库连接 3.查询 4.增、5删 6改(操作数据库) import java.sql.*;5 d3 P$ a# t3 B! I1 N2 d
import java.awt.*;
0 v* S" z6 B8 O7 A; gimport java.awt.event.*; import javax.swing.*; public class EmpManageSys implements ActionListener {4 C5 y6 t- A0 }. l. j! B/ x
Connection con;
5 B' @- @& b/ ]9 V5 T& EStatement stmt;
0 U, O! E4 F# c0 y1 ]String sql;2 h6 ^$ U3 G M* U1 `* g
ResultSet rs;
+ m& {1 U* H2 i; H4 |6 O' n& 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) {) y4 _8 l% @, m4 p$ T, L6 Y
String str = e.getActionCommand();: z; I8 C% O! F! R% v2 n% c! s# j( S5 n
if ("查询".equals(str)) { //if check the button of 查询,then go to method of searchEmp()
' k( L, ?' ]7 {8 [: k+ YsearchEmp();% b- {9 V1 q6 [& q _& W
} else if ("增加".equals(str)) { //if check the button of 增加,then go to method of addEmp()
5 y, T6 k' t# M" OaddEmp();/ x0 Q5 x( s. V* p7 n' Q, f1 s
} else if ("修改".equals(str)) { //if check the button of 修改,then go to method of alterEmp()2 w5 @/ ^4 ~2 c5 N
alterEmp();
0 m0 ^* X2 m v' n} else if ("撤除".equals(str)) { //if check the button of 撤除,then go to method of deleteEmp()
, r5 [$ o( ` r8 ]4 tdeleteEmp();
/ x# s4 f( h) m: h) m6 u}
% o0 n- \( d) v4 H7 v! @} //this is the constructor. EmpManageSys() {
4 ~# }: q4 q2 l) c2 o$ ~createGUI();
5 ? `& y6 Q2 r8 n( I. I8 F3 BconnectToDataBase();
8 Y" A& B5 l5 t; f// searchEmp();2 R. L0 N6 E+ u( j
// addEmp();
. S% {9 D7 V4 R0 W% @! Q// deleteEmp();
! n) a8 k9 ~- ~9 V& RalterEmp();
6 x7 B2 Y2 R+ B# M! Z/ y' s$ 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() {$ p; s$ Z5 C( E
con = JdbcUtil.getConnection();4 |( x( R; B$ p
System.out.println(con);
# h9 I) x/ L( a: |return con; } public void searchEmp() {
* E5 K3 O) b" d, d& [jta10.setText("");9 K8 G. v/ Z8 F5 U2 I2 Q) c6 m2 [
try {
4 p& `' r! Y, D* C# L, }) Ostmt = con.createStatement();. m+ w) o; U5 X$ |) V* I/ I' G
// sql = "select * from Mstar where id=" + jtf3.getText();
3 |& M7 ]' a: h( d( ~// sql1="select * from sd100343 where ="+jtf3.getText();
) Y6 l* }% k! N1 {4 t" ]sql = "select * from Mstar where chineseName="
" I5 p6 v6 _2 \. c% K+ jtf2.getText().toLowerCase().trim() + "or id="
+ \2 [; G" `5 Z7 c/ g: [; j) E! D7 Z+ jtf3.getText().toLowerCase().trim() + "or engName="
, I3 @# W3 V9 E) S J" O- L2 M+ jtf4.getText().toLowerCase().trim() + "or UNIT="
% S5 z# C& @9 M$ ~+ jtf5.getText().toLowerCase().trim() + "or TEAM="
- K! o/ }1 j: v# X' s+ jtf6.getText().toLowerCase().trim() + "or Phone="8 N! r+ X- L% `' G% R/ M6 F/ D
+ jtf7.getText().toLowerCase().trim() + "or region="
# @. ^% {/ O) R }3 a8 v. K+ jtf8.getText().toLowerCase().trim() + "or busStation=": k5 ]2 p& n5 q& m' y* M1 }5 b
+ jtf9.getText().toLowerCase().trim();) R( h0 X* t7 q+ C5 L
stmt.executeQuery(sql);
0 X9 z" H6 ]9 {& L7 Crs = stmt.getResultSet();3 s9 i" {2 i) q
ResultSetMetaData meta = rs.getMetaData();
7 y* S5 _1 l' L+ s( uint cols = meta.getColumnCount();* l. ?4 I6 u) k7 U
while (rs.next()) {( f. g& |. a& _ Z3 _' a
for (int i = 1; i <= cols; i++) {
_% x; M. |3 F! c; _3 Isb.append(" " + meta.getColumnName(i) + " =");# Q6 O: o; |4 W/ o
sb.append(rs.getString(i));
. ^6 C5 n: Q' T, A, Q. M1 D# w5 b}$ O" `4 v* H& k; x9 q6 Z
sb.append("\n");
/ g/ N2 N' g6 o2 \6 a8 G2 \7 g: Vjta10.setText(sb.toString());) U7 x0 K) s1 |/ Y! Q
}2 D( Q) V( E n* ^* y
} catch (SQLException e11) {7 i0 {$ ^' v" k
e11.printStackTrace();
- ~. n4 A% N" O2 e} } public void addEmp() {4 c; S/ H+ \! ~2 l5 t% W( ` c9 r
try {" G% w- N X* a W% s" P8 ?
stmt = con.createStatement();" V7 u5 F+ O, v3 K' x5 u) w
sql = "update Mstar values(" + jtf2.getText() + jtf3.getText()
" O7 K* D2 u h# F& e" Z+ jtf4.getText() + jtf5.getText() + jtf6.getText()$ W+ P9 t) M0 }9 \
+ jtf7.getText() + jtf8.getText() + jtf9.getText() + ")";9 q& R9 T, ~ v3 W! [
int i = stmt.getUpdateCount();
5 l0 h+ ^! ?! Cif ((jtf2.getText() != null) && (jtf4.getText() != null)
) E! i# S" z7 m# ?4 r- x&& (jtf6.getText() != null) && (jtf7.getText() != null)) {/ C1 }* K# [5 e1 N. u& E
stmt.executeUpdate(sql);& h0 @7 Y9 G0 z& u0 \1 x" {
jta10.setText("添加记录成功" + i + "条");
/ M# s- j. N, i A" o} else {8 F# F+ \: W) g! d0 t
jta10.setText("带*号项为添加记录时不能为空");
6 z6 M7 J% k7 n6 J/ _}; w% E( R) \4 D% }/ b
} catch (SQLException e1) {, ^1 Z; z7 `) _$ t0 b/ l
e1.printStackTrace();' W: O# z- V. M% \6 U/ [
}& Q( m4 I# a. b
} public void deleteEmp() {
* y3 ?% v# S: \. MsearchEmp();
' H5 T+ M) W3 wtry {
8 E* i* D9 `7 ~8 U/ T3 R& e2 qstmt = con.createStatement();
8 J, U" B& f/ osql = "delete from Mstar where chineseName="
! B# }; R6 M# `' _. E1 y+ jtf2.getText().toLowerCase().trim() + "or id="2 X$ J* n$ x- f" d; _& |& w
+ jtf3.getText().toLowerCase().trim() + "or engName="% p: G+ E* k, e
+ jtf4.getText().toLowerCase().trim() + "or UNIT="$ Q8 x' V5 u3 U6 M% `
+ jtf5.getText().toLowerCase().trim() + "or TEAM="
- B& b8 e, ^) z! i3 S+ jtf6.getText().toLowerCase().trim() + "or Phone="1 w; Q+ V8 T" o- a) }
+ jtf7.getText().toLowerCase().trim() + "or region="
; L; u5 p9 Y4 y+ jtf8.getText().toLowerCase().trim() + "or busStation="% ^% T F2 \: R* T1 U
+ jtf9.getText().toLowerCase().trim();% h$ m9 @( @7 X, D
stmt.executeUpdate(sql);% N. O: w6 m+ T( s2 x
int i = stmt.getUpdateCount();
0 B5 i' P8 R) E, f3 u( ejta10.setText("撤除操作成功" + i + "条");9 X4 }* R1 d. y. G6 T! o
} catch (SQLException e) {
+ w+ t) z# e' d; B ?e.printStackTrace(); T% C( q; Z) b0 W1 K
} } public void alterEmp() {( K* V Q: t1 N6 u
searchEmp();
; D D' c9 u6 Q9 ^$ K4 p9 c1 [sql = "update Mstar set chineseName="
3 g' G6 u1 ]) v* y# D* G+ jtf2.getText().toLowerCase().trim() + "and id="+ o* h5 _6 x& T7 l. I3 v* j
+ jtf3.getText().toLowerCase().trim() + "and engName="
0 T: v4 t% v/ q9 d; m: y/ @" Z+ jtf4.getText().toLowerCase().trim() + "and UNIT="+ w4 G( {% q& t' T
+ jtf5.getText().toLowerCase().trim() + "and TEAM=": w2 N. a) [0 B& E# J
+ jtf6.getText().toLowerCase().trim() + "and Phone="
+ d* z T4 d6 ]" j0 |+ jtf7.getText().toLowerCase().trim() + "and region="
5 l: P/ R3 u8 J. s% ^' O+ jtf8.getText().toLowerCase().trim() + "and busStation="
5 L- ^0 ~( t3 }6 ^7 [. z+ jtf9.getText().toLowerCase().trim(); int i = 0;( ^ P; h3 W5 A
try {
' @& f. W- A9 L9 V2 R+ g+ s# Fstmt.executeUpdate(sql);7 F. c m+ w5 F0 M
i = stmt.getUpdateCount();
, o4 C0 \$ K2 c& U} catch (SQLException e) {9 t0 I5 R9 [8 M# N! C
e.printStackTrace();
& r) v: V, I; M/ W0 X}
+ l- U8 x9 V) c0 @/ ?" w- wjta10.setText("修改操作成功" + i + "条");
& ]- W% f- D; |: v2 g' b# ]" m} public void createGUI() {
5 ~" E. h9 t; c1 Q1 |( e* I: v8 TJFrame jf = new JFrame("员工信息管理系统");& `; n; T& M- o7 G! R8 J7 X
jf.setLayout(new GridLayout(2, 1));! e2 p" h5 b+ D" f! g
// jf.setLayout(new GridLayout(10,2));( i R$ y P& E) X) \) m1 c+ j
JPanel jp00 = new JPanel(new GridLayout(5, 4)); JPanel jp1 = new JPanel();
; O$ h+ t" C! n* i5 \JButton jb11 = new JButton("查询");
# w- ?+ @& d- l! b& ?9 P" Xjb11.addActionListener(this);
! e2 `* z: ^" L j, O2 e1 LJButton jb12 = new JButton("增加");8 ~% Q: p' V$ s( p; T; S- C* V
jb12.addActionListener(this);6 v7 u. m( Q) J6 z1 T$ d, d
jp1.add(jb11);( a2 ]# v2 K7 W" x! e8 c; N
jp1.add(jb12);
?% b3 @1 s7 y! k P( @8 ejp00.add(jp1);
( W6 n" J, w7 t5 a4 Z F, M, f0 J3 SJPanel jp11 = new JPanel();
! v1 G& W- }( H8 y# XJButton jb111 = new JButton("修改");! } Z7 t% H8 T' B9 T
jb111.addActionListener(this);# L2 Y5 ]. o1 P! m+ y
JButton jb112 = new JButton("撤除");
q; j1 j0 m3 n* ]# ~2 q0 Njb112.addActionListener(this);, M1 A$ x# z! g6 |& Q
jp11.add(jb111);
! A" B3 U. U: T( R+ sjp11.add(jb112);
- u4 Q# w, }) v( o0 |jp00.add(jp11);# p1 z" z. {! D# v/ s0 c
JPanel jp2 = new JPanel();. ~. |" f. J, i7 E$ A
JLabel jl2 = new JLabel("中 文 名 * ");% [* m, }' O; h% p$ j
jp2.add(jl2);
- r8 q; `! `& F3 K% ^/ mjp2.add(jtf2);
% c' K5 A, g2 d5 d% |6 V( ajp00.add(jp2);
" r) W' v W" w( P! t; TJPanel jp3 = new JPanel();3 U8 H/ D6 }5 a6 h/ d* W
JLabel jl3 = new JLabel("工 号 ");
$ T- Z3 O' A) c( e1 ~4 ]jp3.add(jl3);
0 q2 i# I4 d' T- l1 Cjp3.add(jtf3);
* `/ E/ B' A! j) x. [" W2 K) y# gjp00.add(jp3);
- Y4 C5 k3 {- y) g7 u, E+ l9 VJPanel jp4 = new JPanel();
/ \9 N* D, p6 QJLabel jl4 = new JLabel("EngName*");) A$ ?. c9 y5 E) [& u
jp4.add(jl4);
O- k2 }2 C0 o. C9 cjp4.add(jtf4);
+ U+ Y6 j8 Q8 ^jp00.add(jp4);- i6 J0 z/ k% v, e# U4 q2 l9 `
JPanel jp5 = new JPanel();
% H7 u3 V; U4 g# h$ ?JLabel jl5 = new JLabel("UNIT ");; E2 a3 C, @8 Z* D
jp5.add(jl5);; K4 i, O6 ~& a- N. C' S: V) Q
jp5.add(jtf5);
% ~8 `4 N/ x5 a% |jp00.add(jp5);
& g8 c( `; Q: gJPanel jp6 = new JPanel();; [9 {- Q$ l' e( o7 p5 S& O
JLabel jl6 = new JLabel("TEAM* ");
# q6 v2 {0 ~% q+ ljp6.add(jl6);! f* [1 M8 X- f2 M$ Q8 m/ A% k3 X
jp6.add(jtf6);
" F* b* |$ P- y" @. v+ s3 kjp00.add(jp6);% V2 n7 T8 b. N, {( O
JPanel jp7 = new JPanel();
9 z. n0 U5 z- AJLabel jl7 = new JLabel("Phone* ");
6 F7 [3 T3 Y4 l* w8 c* @' G( Jjp7.add(jl7);; T& j H- H( Y* `6 w* @/ S" n" X
jp7.add(jtf7);& A0 ], }, _# M, ]
jp00.add(jp7);9 @& U% ]" R K5 K \, `! |
JPanel jp8 = new JPanel();; U+ C4 X$ I9 Q3 g6 N
JLabel jl8 = new JLabel("区域 ");
& F: n4 K4 I; P8 n1 X& S0 W5 b% gjp8.add(jl8);
6 \' X+ K6 @& L' Ojp8.add(jtf8);
1 Q- Q+ ]1 O2 R/ e, l; i7 i3 Mjp00.add(jp8);0 y d1 ?! S/ ^( H. x1 d8 ?* r( E
JPanel jp9 = new JPanel();
6 ~' u( T( |8 H9 z$ oJLabel jl9 = new JLabel("公交站 ");
2 v' L5 h- R6 M+ Bjp9.add(jl9);
# K3 a5 a+ x1 M; ]7 Z6 ijp9.add(jtf9); n0 Z1 I0 N% L* a) P7 T1 M/ b
jp00.add(jp9);2 I, `7 w2 t9 O+ O3 E! o
jf.add(jp00);: \$ \$ f2 b, m+ I4 P7 G1 o& r; x) b
JPanel jp01 = new JPanel();
- h! V; B9 N2 kjta10.setText("--用户使用手册-- \n1、查询:选择一个字段,如 EngName 在文本框中输入相应内容,点击查询\n2、增加:在各个文本框中输入相应内容后,点击增加。\n3、修改:先查询到你要的记录,在更改相应内容,点击修改。\n4、撤除:类似于修改操作");
; K, n/ x$ D( m0 R, Ljp01.add(jta10);# m* f1 P, K2 a
jf.add(jp01);) _' F+ T. y6 S4 \6 g8 W4 A
jf.setSize(700, 500);8 I6 o, G0 G* g9 }: K. E* Z( S% J
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
" \0 X* v- P+ v! \% jjf.setVisible(true); } public static void main(String[] args) {; W5 o. B- d; g1 d4 t7 l0 H# p8 V
new EmpManageSys();8 a6 N s8 T# E5 Z2 W4 L. K
}
# b4 d3 [- ]0 U {} J' y: n. u4 W* |
|