剛學完jdbc不久,做了一個簡單的學生管理系統,可能還有不完善的地方,望各路大神見諒。廢話不多說,我先貼個圖讓大家讓大家瞅瞅,覺得是你想要的再看下去吧。
我是以管理者的身份去做的,適合初學者去學習。
在做之前,先捋一遍思路,簡單來說分為三大步。
一、在數據庫里建student表存放學生信息
二、用jdbc來連接、操作數據庫
三、展示student數據,實現增刪改查功能。
思路是非常簡單的,但是要實現還是有很多細節需要注意,下面我就貼上我的代碼,結合著代碼給大家一步步的分析說明。
實現:
一、在數據庫建表:這個不用細說,直接貼圖。
二、用jdbc連接數據庫:這一塊對于剛剛學jdbc的同學來說可能比較繞,所以我把這一塊又分成了四部分(最后的db.properties跟com.student.db一起的),我會逐個說明。看圖。
(1)com.student.db包里有兩個類,一個是dbhelper 一個是dbmanager,這倆類是用jdbc連接數據庫的,固定寫法。
dbmanager類
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
|
package com.student.db; import java.sql.connection; import java.sql.preparedstatement; import java.sql.resultset; import java.sql.sqlexception; import java.util.arraylist; import java.util.list; import com.student.mapper.imapper; public class dbmanager { //這里把jdbc連接數據庫的步驟(找驅動,建連接,建通道,執行sql)封裝在dbhelper類里面,在dbmanager里用getconnection()調用。這樣寫的目的是方便 public connection getconnection(){ try { return dbhelper.getconnection(); //得到dbhelper類里面寫好的連接 } catch (classnotfoundexception e) { e.printstacktrace(); } catch (sqlexception e) { e.printstacktrace(); } return null ; } //增刪改結果集。因為sql語句是變化的,所以設為參數比較方便。params是占位符的,沒學的可以忽略。 public int executeupdate(string sql,object[] params){ connection conn= null ; preparedstatement pst= null ; try { conn=getconnection(); //連接 pst=conn.preparestatement(sql); //通道 if (params != null ){ //占位符的應用。 for ( int i= 0 ;i<params.length;i++){ pst.setobject(i+ 1 ,params[i]); //往通道里放數據,占位符下標從1開始。 } } return pst.executeupdate(); } catch (sqlexception e) { e.printstacktrace(); } return - 1 ; } //查詢結果集。比增刪改要復雜一些,慢慢看。 //這里的imapper是將所有可能的用到的類都放進去,方便以后繼承使用。(現在我們寫的是student信息,以后可能會有teacher信息,class信息等等) //用接口是因為接口多繼承,方便維護升級 public list executequery(string sql,imapper mapper,object []params){ connection conn= null ; preparedstatement pst= null ; resultset rst= null ; //查詢結果集 list list= new arraylist(); //用一個集合存放student信息 try { conn=getconnection(); pst=conn.preparestatement(sql); if (params != null ){ for ( int i= 0 ;i<params.length;i++){ pst.setobject(i+ 1 ,params[i]); } } rst=pst.executequery(); //把通道里的數據放入結果集 list=mapper.map(rst); //imapper里有個map接口,里面存著結果集數據。把結果集的數據放入list集合 } catch (sqlexception e) { e.printstacktrace(); } return list; } public int count(string sql){ //分頁查詢 count代表頁數。 connection conn= null ; preparedstatement pst= null ; resultset rst= null ; try { conn=getconnection(); pst=conn.preparestatement(sql); rst=pst.executequery(); while (rst.next()){ return rst.getint( 1 ); //sql語句是select count(*) from stu,顯示的是count值,就是一行。 } } catch (sqlexception e) { e.printstacktrace(); } return - 1 ; } } |
dbhelper類。在寫之前先建一個properties文件,名字為db.properties(如圖),注意不要建在包里面。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
package com.student.db; import java.io.ioexception; import java.io.inputstream; import java.sql.connection; import java.sql.drivermanager; import java.sql.sqlexception; import java.util.properties; public class dbhelper { private static string driver; private static string url; private static string user; private static string password; static { properties pro= new properties(); inputstream in=dbhelper. class .getclassloader() .getresourceasstream( "db.properties" ); //讀取文件數據 try { pro.load(in); } catch (ioexception e) { e.printstacktrace(); } driver=pro.getproperty( "driver" ); url=pro.getproperty( "url" ); user=pro.getproperty( "user" ); password=pro.getproperty( "password" ); } public static connection getconnection() throws classnotfoundexception, sqlexception{ class .forname(driver); //找驅動 return drivermanager.getconnection(url, user, password); //建連接 } } |
(2)com.student.vo包。這里面有一個vo類,我們是要把數據庫里的數據放到java里展示,用一個類對象把數據庫里的信息一一對應起來就可以很容易的操作。數據庫里的一個列對應類對象的一個屬性。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
package com.student.vo; public class student { private string stuid; private string name; private string age; private string sex; public string getstuid(){ return stuid; } public void setstuid(string stuid){ this .stuid=stuid; } public string getname() { return name; } public void setname(string name) { this .name = name; } public string getage() { return age; } public void setage(string age) { this .age = age; } public string getsex() { return sex; } public void setsex(string sex) { this .sex = sex; } public student(string stuid,string name,string sex,string age){ super (); this .stuid=stuid; this .name=name; this .age=age; this .sex=sex; } public student(){ super (); } } |
(3)com.student.mapper包。這里面一個接口,一個實現類。
接口:
1
2
3
4
5
6
7
8
|
package com.student.mapper; import java.sql.resultset; import java.util.list; public interface imapper { list map(resultset rst); //聲明一個方法存著結果集。 } |
實現類:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
package com.student.mapper; import java.sql.resultset; import java.sql.sqlexception; import java.util.arraylist; import java.util.list; import com.student.vo.student; public class stumapper implements imapper { //實現接口方法 public list map(resultset rst) { list<student> list= new arraylist<student>(); //建一個集合,里面是student類里的信息。 try { while (rst.next()){ // student stu= new student(); stu.setstuid(rst.getstring( "stuid" )); //類對象每一個屬性對應數據庫的每一列。 stu.setname(rst.getstring( "stuname" )); stu.setage(rst.getstring( "age" )); stu.setsex(rst.getstring( "sex" )); list.add(stu); //把類對象放到集合里 } } catch (sqlexception e) { e.printstacktrace(); } return list; } } |
(4)com.student.dao包:這里面的studao類放著增刪改查分頁等功能
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
package com.student.dao; import java.util.list; import com.student.db.dbmanager; import com.student.mapper.imapper; import com.student.mapper.stumapper; import com.student.vo.student; public class studao { public list<student> check(){ //查看 string sql= "select * from student" ; //sql語句 dbmanager db= new dbmanager(); imapper mapper= new stumapper(); //實現stumapper list<student> list=db.executequery(sql, mapper, null ); //null是指占位符為null,因為查看的是所有信息 return list; } public boolean add(student stu){ //添加 string sql= "insert into student values(?,?,?,?)" ; object[] params={stu.getstuid(),stu.getname(),stu.getage(),stu.getsex()}; dbmanager db= new dbmanager(); int i=db.executeupdate(sql, params); if (i>= 0 ){ system.out.println( "成功" ); } else { system.out.println( "失敗" ); } return true ; } public boolean update(student stu){ //修改 string sql= "update student set stuname=?,age=?,sex=? where stuid=?" ; object params[]={stu.getname(),stu.getage(),stu.getsex(),stu.getstuid()}; dbmanager db= new dbmanager(); int i=db.executeupdate(sql, params); if (i>= 0 ){ system.out.println( "成功" ); } else { system.out.println( "失敗" ); } return true ; } public boolean delete(student stu){ //刪除 string sql= "delete from student where stuid=?" ; object params[]={stu.getstuid()}; dbmanager db= new dbmanager(); int i=db.executeupdate(sql, params); if (i>= 0 ){ system.out.println( "成功" ); } else { system.out.println( "失敗" ); } return true ; } public list<student> findpage( int pagesize, int pagenow){ //分頁 string sql= "select * from (select rownum rn ,stu .* from stu) " + "where rownum<=? and rn>?" ; //分頁公式 object []params={pagesize,(pagenow- 1 )*pagesize}; dbmanager db= new dbmanager(); imapper mapper= new stumapper(); return db.executequery(sql, mapper, params); } public int findcount(){ string sql= "select count(*) from stu" ; dbmanager db= new dbmanager(); return db.count(sql); } } |
當把這一塊寫完之后,其實大部分就已經完成了,jdbc連接數據庫基本上是固定的,多寫幾遍就明白了。
三、展示student信息,實現增刪改查。看圖:
(1)com.student.show包,展示界面:這里面內容比較多,但是都很容易理解
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
|
package com.student.show; import java.awt.event.actionevent; import java.awt.event.actionlistener; import java.sql.sqlexception; import java.sql.statement; import java.util.arraylist; import java.util.list; import javax.swing.jbutton; import javax.swing.jframe; import javax.swing.joptionpane; import javax.swing.jscrollpane; import javax.swing.jtable; import com.student.add.add; import com.student.check.check; import com.student.dao.studao; import com.student.delete.delete; import com.student.update.update; import com.student.vo.student; public class show extends jframe { public static int pagesize= 5 ; //每頁顯示5條信息 public static int pagenow= 1 ; //當前頁為第一頁 public show() { setsize( 500 , 430 ); setvisible( true ); setdefaultcloseoperation(exit_on_close); //點x號就是關閉 setresizable( false ); //不可改變窗口大小 setlocationrelativeto( null ); //默認居中顯示 setlayout( null ); //采用坐標布局 studao dao = new studao(); //前面我們已經把增刪改查分頁寫到studao里面,現在就直接拿出來用 list<student> list =dao.findpage(pagesize, pagenow); student stu = new student(); for ( int i = 0 ; i < list.size(); i++) { stu = list.get(i); } string[] rowname = { "學號" , "姓名" , "年齡" , "性別" }; //從這里開始是二維數組的遍歷使用 object[][] data = new object[list.size()][ 4 ]; for ( int i = 0 ; i < list.size(); i++) { student s = list.get(i); object st[] = { s.getstuid(), s.getname(), s.getage(), s.getsex() }; data[i] = st; } final jtable table = new jtable(data,rowname); jscrollpane jsp= new jscrollpane(table); //這一步不能省去,否則顯示不出列名 jsp.setbounds( 20 , 10 , 400 , 200 ); add(jsp); jbutton jb11= new jbutton( "首頁" ); jb11.setbounds( 40 , 220 , 80 , 30 ); add(jb11); jbutton jb22= new jbutton( "上一頁" ); jb22.setbounds( 130 , 220 , 80 , 30 ); add(jb22); jbutton jb33= new jbutton( "下一頁" ); jb33.setbounds( 220 , 220 , 80 , 30 ); add(jb33); jbutton jb44= new jbutton( "尾頁" ); jb44.setbounds( 310 , 220 , 80 , 30 ); add(jb44); jbutton jb1 = new jbutton( "查看信息" ); jb1.setbounds( 50 , 270 , 100 , 30 ); add(jb1); jbutton jb2 = new jbutton( "修改信息" ); jb2.setbounds( 280 , 270 , 100 , 30 ); add(jb2); jbutton jb3 = new jbutton( "添加信息" ); jb3.setbounds( 50 , 320 , 100 , 30 ); add(jb3); jbutton jb4 = new jbutton( "刪除信息" ); jb4.setbounds( 280 , 320 , 100 , 30 ); add(jb4); jbutton jb5 = new jbutton( "退出" ); jb5.setbounds( 280 , 360 , 100 , 30 ); add(jb5); jb1.addactionlistener( new actionlistener() { //查看 public void actionperformed(actionevent event) { int row = table.getselectedrow(); //選中第幾行 int index = 0 ; if (row==- 1 ){ joptionpane.showmessagedialog( null , "您沒有選中信息" ); return ; } string id = (string) table.getvalueat(row, index); // 跟check聯系起來 check check= new check(id); check.setvisible( true ); setvisible( false ); } }); jb2.addactionlistener( new actionlistener() { //修改 public void actionperformed(actionevent event) { int row = table.getselectedrow(); int index = 0 ; if (row==- 1 ){ joptionpane.showmessagedialog( null , "您沒有選中信息" ); return ; } string id = (string) table.getvalueat(row, index); // 跟update聯系起來 update up= new update(id); up.setvisible( true ); setvisible( false ); } }); jb3.addactionlistener( new actionlistener() { //添加 public void actionperformed(actionevent event) { add add = new add(); add.setvisible( true ); setvisible( false ); } }); jb4.addactionlistener( new actionlistener() { //刪除 public void actionperformed(actionevent event) { int row = table.getselectedrow(); int index = 0 ; if (row==- 1 ){ joptionpane.showmessagedialog( null , "您沒有選中信息" ); return ; } string num=(string) table.getvalueat(row, index); delete d= new delete(num); d.setvisible( true ); setvisible( false ); } }); jb11.addactionlistener( new actionlistener() { //首頁 public void actionperformed(actionevent event) { pagenow= 1 ; show show= new show(); setvisible( false ); show.setvisible( true ); } }); jb22.addactionlistener( new actionlistener() { //上一頁 public void actionperformed(actionevent event) { if (pagenow != 1 ){ pagenow=pagenow- 1 ; } else { return ; } show show= new show(); setvisible( false ); show.setvisible( true ); } }); jb33.addactionlistener( new actionlistener() { //下一頁 public void actionperformed(actionevent event) { studao dao= new studao(); int count=dao.findcount(); int pagecount=(count- 1 )/pagesize+ 1 ; //pagecount表示最后一頁 if (pagenow != pagecount){ pagenow=pagenow+ 1 ; } else { return ; } show show= new show(); setvisible( false ); show.setvisible( true ); } }); jb44.addactionlistener( new actionlistener() { //尾頁 public void actionperformed(actionevent event) { studao dao= new studao(); int count=dao.findcount(); int pagecount=(count- 1 )/pagesize+ 1 ; pagenow=pagecount; show show= new show(); setvisible( false ); show.setvisible( true ); } }); } public static void main(string args[]) { show s = new show(); } } |
(2)增刪改查:大同小異,因為我們在studao里面已經寫好了,在用的時候就方便多了。
①添加:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
package com.student.add; import java.sql.sqlexception; import java.util.list; import javax.swing.jbutton; import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.joptionpane; import javax.swing.jtextfield; import com.student.dao.studao; import com.student.db.dbmanager; import com.student.show.show; import com.student.vo.student; public class add extends jframe{ public add(){ setsize( 300 , 400 ); setvisible( true ); setdefaultcloseoperation(exit_on_close); setresizable( false ); setlocationrelativeto( null ); setlayout( null ); jlabel j0= new jlabel( "添加信息" ); j0.setbounds( 100 , 20 , 80 , 30 ); add(j0); jlabel j1= new jlabel( "學號:" ); j1.setbounds( 30 , 70 , 50 , 30 ); add(j1); final jtextfield jt1= new jtextfield(); jt1.setbounds( 100 , 70 , 130 , 30 ); add(jt1); jlabel j2= new jlabel( "姓名:" ); j2.setbounds( 30 , 120 , 50 , 30 ); add(j2); final jtextfield jt2= new jtextfield(); jt2.setbounds( 100 , 120 , 130 , 30 ); add(jt2); jlabel j3= new jlabel( "性別:" ); j3.setbounds( 30 , 170 , 50 , 30 ); add(j3); final jtextfield jt3= new jtextfield(); jt3.setbounds( 100 , 170 , 130 , 30 ); add(jt3); jlabel j4= new jlabel( "年齡:" ); j4.setbounds( 30 , 220 , 50 , 30 ); add(j4); final jtextfield jt4= new jtextfield(); jt4.setbounds( 100 , 220 , 130 , 30 ); add(jt4); jbutton jb1= new jbutton( "添加" ); jb1.setbounds( 50 , 280 , 80 , 30 ); add(jb1); jbutton jb2= new jbutton( "返回" ); jb2.setbounds( 150 , 280 , 80 , 30 ); add(jb2); jb1.addactionlistener( new actionlistener(){ public void actionperformed(actionevent event){ string a=jt1.gettext(); //獲取輸入的信息 string b=jt2.gettext(); string c=jt3.gettext(); string d=jt4.gettext(); student stu= new student(a,b,c,d); studao dao= new studao(); list<student> list=dao.check(); //調用studao里面的check()方法 for (student st:list){ //遍歷集合 if (st.getstuid().equals(a)){ joptionpane.showmessagedialog( null , "該賬號存在" ); return ; } } dao.add(stu); joptionpane.showmessagedialog( null , "添加成功" ); show show= new show(); show.setvisible( true ); setvisible( false ); } }); jb2.addactionlistener( new actionlistener(){ public void actionperformed(actionevent event){ show s= new show(); s.setvisible( true ); setvisible( false ); } }); } public static void main(string []args){ add add= new add(); } } |
②修改:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
|
package com.student.update; import java.awt.event.actionevent; import java.awt.event.actionlistener; import java.util.list; import javax.swing.jbutton; import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.joptionpane; import javax.swing.jtextfield; import com.student.dao.studao; import com.student.db.dbmanager; import com.student.show.show; import com.student.vo.student; public class update extends jframe{ public update( final string id){ setsize( 300 , 400 ); setvisible( true ); setdefaultcloseoperation(exit_on_close); setresizable( false ); setlocationrelativeto( null ); setlayout( null ); jlabel j0= new jlabel( "修改信息" ); j0.setbounds( 100 , 20 , 80 , 30 ); add(j0); jlabel j1= new jlabel( "學號:" ); j1.setbounds( 30 , 70 , 50 , 30 ); add(j1); final jlabel jt1= new jlabel(); jt1.setbounds( 100 , 70 , 130 , 30 ); add(jt1); jlabel j2= new jlabel( "姓名:" ); j2.setbounds( 30 , 120 , 50 , 30 ); add(j2); final jtextfield jt2= new jtextfield(); jt2.setbounds( 100 , 120 , 130 , 30 ); add(jt2); jlabel j3= new jlabel( "年齡:" ); j3.setbounds( 30 , 170 , 50 , 30 ); add(j3); final jtextfield jt3= new jtextfield(); jt3.setbounds( 100 , 170 , 130 , 30 ); add(jt3); jlabel j4= new jlabel( "性別:" ); j4.setbounds( 30 , 220 , 50 , 30 ); add(j4); final jtextfield jt4= new jtextfield(); jt4.setbounds( 100 , 220 , 130 , 30 ); add(jt4); jbutton jb1= new jbutton( "修改" ); jb1.setbounds( 50 , 280 , 80 , 30 ); add(jb1); jbutton jb2= new jbutton( "返回" ); jb2.setbounds( 150 , 280 , 80 , 30 ); add(jb2); studao dao= new studao(); list<student> list=dao.check(); student stu= new student(); for ( int i= 0 ;i<list.size();i++){ //遍歷,找到與id相同的學號。 stu=list.get(i); if (stu.getstuid().equals(id)){ //id是參數,跟前面show聯系起來。 break ; } } jt1.settext(stu.getstuid()); jt2.settext(stu.getname()); jt3.settext(stu.getage()); jt4.settext(stu.getsex()); jb1.addactionlistener( new actionlistener(){ public void actionperformed(actionevent event){ studao dao= new studao(); student stu= new student(); stu.setstuid(id); stu.setname(jt2.gettext()); stu.setage(jt3.gettext()); stu.setsex(jt4.gettext()); dao.update(stu); //studao里的update()已經寫好如何修改,這里直接用 joptionpane.showmessagedialog( null , "修改成功" ); show show= new show(); show.setvisible( true ); setvisible( false ); } }); jb2.addactionlistener( new actionlistener(){ public void actionperformed(actionevent event){ show s= new show(); s.setvisible( true ); setvisible( false ); } }); } } |
③查看:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
|
package com.student.check; import java.awt.event.actionevent; import java.awt.event.actionlistener; import java.util.list; import javax.swing.jbutton; import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.jtextfield; import com.student.dao.studao; import com.student.show.show; import com.student.vo.student; public class check extends jframe{ public check(string id) { setsize( 300 , 400 ); setvisible( true ); setdefaultcloseoperation(exit_on_close); setresizable( false ); setlocationrelativeto( null ); setlayout( null ); jlabel j0= new jlabel( "學生信息" ); j0.setbounds( 100 , 20 , 80 , 30 ); add(j0); jlabel j1= new jlabel( "學號:" ); j1.setbounds( 30 , 70 , 50 , 30 ); add(j1); final jlabel jt1= new jlabel(); jt1.setbounds( 100 , 70 , 130 , 30 ); add(jt1); jlabel j2= new jlabel( "姓名:" ); j2.setbounds( 30 , 120 , 50 , 30 ); add(j2); final jlabel jt2= new jlabel(); jt2.setbounds( 100 , 120 , 130 , 30 ); add(jt2); jlabel j3= new jlabel( "年齡:" ); j3.setbounds( 30 , 170 , 50 , 30 ); add(j3); final jlabel jt3= new jlabel(); jt3.setbounds( 100 , 170 , 130 , 30 ); add(jt3); jlabel j4= new jlabel( "性別:" ); j4.setbounds( 30 , 220 , 50 , 30 ); add(j4); final jlabel jt4= new jlabel(); jt4.setbounds( 100 , 220 , 130 , 30 ); add(jt4); jbutton jb1= new jbutton( "確認" ); jb1.setbounds( 50 , 280 , 80 , 30 ); add(jb1); jbutton jb2= new jbutton( "返回" ); jb2.setbounds( 150 , 280 , 80 , 30 ); add(jb2); studao dao= new studao(); list<student> list=dao.check(); student stu= new student(); for ( int i= 0 ;i<list.size();i++){ stu=list.get(i); if (stu.getstuid().equals(id)){ break ; } } jt1.settext(stu.getstuid()); jt2.settext(stu.getname()); jt3.settext(stu.getage()); jt4.settext(stu.getsex()); jb1.addactionlistener( new actionlistener(){ public void actionperformed(actionevent event){ show s= new show(); s.setvisible( true ); setvisible( false ); } }); jb2.addactionlistener( new actionlistener(){ public void actionperformed(actionevent event){ show s= new show(); s.setvisible( true ); setvisible( false ); } }); } } |
④刪除:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
|
package com.student.delete; import java.awt.event.actionevent; import java.awt.event.actionlistener; import java.util.list; import javax.swing.jbutton; import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.joptionpane; import javax.swing.jtextfield; import com.student.dao.studao; import com.student.db.dbmanager; import com.student.show.show; import com.student.vo.student; public class delete extends jframe{ public delete( final string num){ setsize( 300 , 400 ); setvisible( true ); setdefaultcloseoperation(exit_on_close); setresizable( false ); setlocationrelativeto( null ); setlayout( null ); jlabel j0= new jlabel( "您確認要刪除該信息嗎" ); j0.setbounds( 100 , 20 , 200 , 30 ); add(j0); jlabel j1= new jlabel( "學號:" ); j1.setbounds( 30 , 70 , 50 , 30 ); add(j1); final jlabel jt1= new jlabel(); jt1.setbounds( 100 , 70 , 130 , 30 ); add(jt1); jlabel j2= new jlabel( "姓名:" ); j2.setbounds( 30 , 120 , 50 , 30 ); add(j2); final jlabel jt2= new jlabel(); jt2.setbounds( 100 , 120 , 130 , 30 ); add(jt2); jlabel j3= new jlabel( "年齡:" ); j3.setbounds( 30 , 170 , 50 , 30 ); add(j3); final jlabel jt3= new jlabel(); jt3.setbounds( 100 , 170 , 130 , 30 ); add(jt3); jlabel j4= new jlabel( "性別:" ); j4.setbounds( 30 , 220 , 50 , 30 ); add(j4); final jlabel jt4= new jlabel(); jt4.setbounds( 100 , 220 , 130 , 30 ); add(jt4); jbutton jb1= new jbutton( "確認" ); jb1.setbounds( 20 , 280 , 80 , 30 ); add(jb1); jbutton jb2= new jbutton( "返回" ); jb2.setbounds( 180 , 280 , 80 , 30 ); add(jb2); studao dao= new studao(); list<student> list=dao.check(); student stu= new student(); for ( int i= 0 ;i<list.size();i++){ stu=list.get(i); if (stu.getstuid().equals(num)){ break ; } } jt1.settext(stu.getstuid()); jt2.settext(stu.getname()); jt3.settext(stu.getage()); jt4.settext(stu.getsex()); jb1.addactionlistener( new actionlistener(){ public void actionperformed(actionevent event){ studao dao= new studao(); student stu= new student(); stu.setstuid(num); dao.delete(stu); joptionpane.showmessagedialog( null , "刪除成功" ); show show= new show(); show.setvisible( true ); setvisible( false ); } }); jb2.addactionlistener( new actionlistener(){ public void actionperformed(actionevent event){ show s= new show(); s.setvisible( true ); setvisible( false ); } }); } } |
最后貼一下登錄頁面,因為是以管理者的身份登錄的不需要判斷,就非常簡單:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
package com.student.login; import java.awt.event.actionevent; import java.awt.event.actionlistener; import javax.swing.jbutton; import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.joptionpane; import javax.swing.jpasswordfield; import javax.swing.jtextfield; import com.student.show.show; public class login extends jframe{ public login(){ setsize( 300 , 250 ); setvisible( true ); setdefaultcloseoperation(exit_on_close); setresizable( false ); setlocationrelativeto( null ); setlayout( null ); jlabel j= new jlabel( "登錄窗口" ); j.setbounds( 100 , 20 , 80 , 30 ); add(j); jlabel j1= new jlabel( "用戶名:" ); j1.setbounds( 50 , 80 , 60 , 30 ); add(j1); final jtextfield jt1= new jtextfield(); jt1.setbounds( 120 , 80 , 120 , 30 ); add(jt1); jlabel j2= new jlabel( "密 碼:" ); j2.setbounds( 50 , 130 , 60 , 30 ); add(j2); final jpasswordfield jp= new jpasswordfield(); jp.setbounds( 120 , 130 , 120 , 30 ); add(jp); jbutton jb1= new jbutton( "登錄" ); jb1.setbounds( 70 , 180 , 60 , 30 ); add(jb1); jbutton jb2= new jbutton( "重置" ); jb2.setbounds( 170 , 180 , 60 , 30 ); add(jb2); jb1.addactionlistener( new actionlistener(){ public void actionperformed(actionevent event){ string id=jt1.gettext(); char ch[]=jp.getpassword(); string pass= new string(ch); if (id.equals(abcdefj){ //設置用戶名為abcdefj if (pass.equals( 123456 )){ //設置密碼為123456 joptionpane.showmessagedialog( null , "登錄成功" ); show s= new show(); //成功后跳到show s.setvisible( true ); setvisible( false ); } else { joptionpane.showmessagedialog( null , "密碼錯誤" ); jt1.settext( "" ); return ; } } else { joptionpane.showmessagedialog( null , "您輸入的賬號有誤" ); jt1.settext( "" ); jp.settext( "" ); return ; } } }); } public static void main(string []args){ login lo= new login(); } } |
寫在最后:
剛開始學的時候感覺很繞,尤其是jdbc那,后來發現,是因為前面java基礎掌握的不行,我又回去好好復習了java基礎,才發現jdbc是死的,固定的寫法,背過就行了。所以再做這個學生管理系統,就感覺不復雜了。先有一個大的思路,然后順著思路往下走,逐步實現每個功能。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/qq_41717599/article/details/79976755