本文為大家分享了java基于mysql實(shí)現(xiàn)學(xué)生管理系統(tǒng),供大家參考,具體內(nèi)容如下
因?yàn)閷?shí)驗(yàn)室要交作業(yè)然后就做了一個(gè)學(xué)生管理系統(tǒng),貼個(gè)代碼紀(jì)念一下,做的太急界面什么的也比較差。
還有一些小細(xì)節(jié)沒有完善不過還是能實(shí)現(xiàn)主要的功能的。
window是主界面
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 first; import java.sql.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class window { public static void main(string[] args){ jframe jframe = new jframe( "學(xué)生管理系統(tǒng)" ) ; //window dimension d = new dimension( 400 , 300 ); point p = new point ( 250 , 350 ); jframe.setsize(d); jframe.setlocation(p); jframe.setdefaultcloseoperation(jframe.exit_on_close); jframe.setvisible( true ); jbutton button1 = new jbutton( "添加" ); jbutton button2 = new jbutton( "修改" ); jbutton button3 = new jbutton( "查詢" ); jbutton button4 = new jbutton( "刪除" ); jbutton button5 = new jbutton( "瀏覽" ); flowlayout flow = new flowlayout(flowlayout.left, 10 , 10 ); jpanel panel = new jpanel(flow); panel.add(button1); panel.add(button2); panel.add(button3); panel.add(button4); panel.add(button5); jframe.add(panel); button1.addactionlistener( new actionlistener(){ public void actionperformed(actionevent e){ add add = new add(); } }); button2.addactionlistener( new actionlistener(){ public void actionperformed(actionevent e){ change change = new change(); } }); button3.addactionlistener( new actionlistener(){ public void actionperformed(actionevent e){ ask ask = new ask(); } }); button4.addactionlistener( new actionlistener(){ public void actionperformed(actionevent e){ delete delete = new delete(); } }); button5.addactionlistener( new actionlistener(){ public void actionperformed(actionevent e){ look look = new look(); } }); } } |
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
|
package first; import java.sql.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; import com.mysql.jdbc.driver; import first.window; public class add extends jframe { private static final long serialversionuid = -1928970409928880648l; jlabel jlnumber = new jlabel( "學(xué)號:" ); jlabel jlname = new jlabel( "姓名:" ); jlabel jlsex = new jlabel( "性別:" ); jlabel jlbirthday = new jlabel( "出生日期:" ); jlabel jldepartment = new jlabel( "學(xué)院:" ); jtextfield jtnumber = new jtextfield( "" , 20 ); jtextfield jtname = new jtextfield( "" , 20 ); jtextfield jtsex = new jtextfield( "" , 20 ); jtextfield jtbirthday = new jtextfield( "" , 20 ); jtextfield jtdepartment = new jtextfield( "" , 20 ); jbutton buttonadd = new jbutton( "添加" ); jbutton buttonreturn = new jbutton( "返回" ); public add() { jpanel jpnumber = new jpanel(); jpanel jpname = new jpanel(); jpanel jpsex = new jpanel(); jpanel jpbirthday = new jpanel(); jpanel jpdepartment = new jpanel(); jpanel jpforbutton = new jpanel( new gridlayout( 1 , 1 )); jpnumber.add(jlnumber); jpnumber.add(jtnumber); jpname.add(jlname); jpname.add(jtname); jpsex.add(jlsex); jpsex.add(jtsex); jpbirthday.add(jlbirthday); jpbirthday.add(jtbirthday); jpdepartment.add(jldepartment); jpdepartment.add(jtdepartment); jpforbutton.add(buttonadd); jpforbutton.add(buttonreturn); buttonadd.addactionlistener( new actionlistener(){ public void actionperformed(actionevent e){ //add connection conn = null ; statement stat = null ; preparedstatement ps= null ; string sql = "insert into student(number,name,sex,birthday,department) " + "values(?,?,?,?,?)" ; try { class .forname( "driver" ); system.out.println( "jbdc 加載成功!" ); } catch (exception a){ system.out.println( "jbdc 狗帶!" ); a.printstacktrace(); } try { conn=drivermanager.getconnection( "jdbc:mysql://localhost:3306/javastu" , "root" , "123" ); ps=conn.preparestatement(sql); ps.setstring( 1 ,jtnumber.gettext()); ps.setstring( 2 ,jtname.gettext()); ps.setstring( 3 ,jtsex.gettext()); ps.setstring( 4 ,jtbirthday.gettext()); ps.setstring( 5 ,jtdepartment.gettext()); ps.executeupdate(); //system.out.println("mysql 連接成功!"); //stat = conn.createstatement(); //stat.executeupdate(sql); //system.out.println("插入數(shù)據(jù)成功!"); } catch (sqlexception b){ b.printstacktrace(); } finally { try { conn.close(); system.out.println( "mysql 關(guān)閉成功" ); } catch (sqlexception c){ system.out.println( "mysql 關(guān)閉失敗 " ); c.printstacktrace(); } } }} ); buttonreturn.addactionlistener( new actionlistener(){ public void actionperformed(actionevent e){ window window = new window(); } }); this .settitle( "添加學(xué)生信息" ); this .setlayout( new gridlayout( 9 , 1 )); this .add(jpnumber); this .add(jpname); this .add(jpsex); this .add(jpbirthday); this .add(jpdepartment); this .add(jpforbutton); this .setlocation( 400 , 300 ); this .setsize( 350 , 300 ); this .setvisible( true ); } } |
ask是查詢
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
|
package first; import java.sql.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; import first.window; public class ask extends jframe { private static final long serialversionuid = -1928970409928880648l; jlabel jlnumber = new jlabel( "學(xué)號:" ); jlabel jlname = new jlabel( "姓名:" ); jlabel jlsex = new jlabel( "性別:" ); jlabel jlbirthday = new jlabel( "出生日期:" ); jlabel jldepartment = new jlabel( "學(xué)院:" ); jtextfield jtnumber = new jtextfield( "" , 20 ); jlabel jname = new jlabel(); jlabel jsex = new jlabel(); jlabel jbirthday = new jlabel(); jlabel jdepartment = new jlabel(); jbutton buttonask = new jbutton( "查詢" ); jbutton buttonreturn = new jbutton( "返回" ); public ask() { jpanel jpnumber = new jpanel(); jpanel jpname = new jpanel(); jpanel jpsex = new jpanel(); jpanel jpbirthday = new jpanel(); jpanel jpdepartment = new jpanel(); jpanel jpforbutton = new jpanel( new gridlayout( 1 , 1 )); jpnumber.add(jlnumber); jpnumber.add(jtnumber); jpname.add(jlname); jpname.add(jname); jpsex.add(jlsex); jpsex.add(jsex); jpbirthday.add(jlbirthday); jpbirthday.add(jbirthday); jpdepartment.add(jldepartment); jpdepartment.add(jdepartment); jpforbutton.add(buttonask); jpforbutton.add(buttonreturn); buttonask.addactionlistener( new actionlistener(){ public void actionperformed(actionevent e){ connection conn = null ; resultset res = null ; statement stat = null ; string sql = "select number,name,sex,birthday,department from student;" ; try { class .forname( "com.mysql.jdbc.driver" ); } catch (exception d){ system.out.println( "jdbc fall" ); d.printstacktrace(); } try { conn=drivermanager.getconnection( "jdbc:mysql://localhost:3306/javastu" , "root" , "123" ); stat=conn.createstatement(); res=stat.executequery(sql); while (res.next()) { if (res.getstring( 1 ).equals(jtnumber.gettext())) { jname.settext(res.getstring( 2 )); jsex.settext(res.getstring( 3 )); jbirthday.settext(res.getstring( 4 )); jdepartment.settext(res.getstring( 5 )); break ; } } } catch (sqlexception e1) { // todo auto-generated catch block e1.printstacktrace(); } finally { try { conn.close(); } catch (sqlexception ar){ ar.printstacktrace(); } }}} ); buttonreturn.addactionlistener( new actionlistener(){ public void actionperformed(actionevent e){ window window = new window(); } }); this .settitle( "查詢學(xué)生信息" ); this .setlayout( new gridlayout( 9 , 1 )); this .add(jpnumber); this .add(jpname); this .add(jpsex); this .add(jpbirthday); this .add(jpdepartment); this .add(jpforbutton); this .setlocation( 400 , 300 ); this .setsize( 350 , 300 ); this .setvisible( true ); } } |
change是修改
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
|
package first; import java.sql.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; import first.window; public class change extends jframe { private static final long serialversionuid = -1928970409928880648l; jlabel jlnumber = new jlabel( "學(xué)號:" ); jlabel jlname = new jlabel( "姓名:" ); jlabel jlsex = new jlabel( "性別:" ); jlabel jlbirthday = new jlabel( "出生日期:" ); jlabel jldepartment = new jlabel( "學(xué)院:" ); jtextfield jtnumber = new jtextfield( "" , 20 ); jtextfield jtname = new jtextfield( "" , 20 ); jtextfield jtsex = new jtextfield( "" , 20 ); jtextfield jtbirthday = new jtextfield( "" , 20 ); jtextfield jtdepartment = new jtextfield( "" , 20 ); jbutton buttonchange = new jbutton( "修改" ); jbutton buttonreturn = new jbutton( "返回" ); public change() { jpanel jpnumber = new jpanel(); jpanel jpname = new jpanel(); jpanel jpsex = new jpanel(); jpanel jpbirthday = new jpanel(); jpanel jpdepartment = new jpanel(); jpanel jpforbutton = new jpanel( new gridlayout( 1 , 1 )); jpnumber.add(jlnumber); jpnumber.add(jtnumber); jpname.add(jlname); jpname.add(jtname); jpsex.add(jlsex); jpsex.add(jtsex); jpbirthday.add(jlbirthday); jpbirthday.add(jtbirthday); jpdepartment.add(jldepartment); jpdepartment.add(jtdepartment); jpforbutton.add(buttonchange); jpforbutton.add(buttonreturn); buttonchange.addactionlistener( new actionlistener(){ public void actionperformed(actionevent e){ string number = jtnumber.gettext(); string name = jtname.gettext(); string sex = jtsex.gettext(); string birthday = jtbirthday.gettext(); string department = jtdepartment.gettext(); connection conn = null ; resultset res = null ; statement stat = null ; string sql = "select number,name,sex,birthday,department from student;" ; try { class .forname( "com.mysql.jdbc.driver" ); } catch (exception d){ system.out.println( "jdbc fall" ); d.printstacktrace(); } try { conn=drivermanager.getconnection( "jdbc:mysql://localhost:3306/javastu" , "root" , "123" ); stat=conn.createstatement(); res=stat.executequery(sql); while (res.next()) { //change if (res.getstring( 1 ).equals(jtnumber.gettext())) { try { class .forname( "com.mysql.jdbc.driver" ); } catch (exception d){ system.out.println( "jdbc fall" ); d.printstacktrace(); } string sql2= "update student set name='" +name+ "' where number='" +jtnumber.gettext()+ "'" ; string sql3= "update student set sex='" +sex+ "' where number='" +jtnumber.gettext()+ "'" ; string sql4= "update student set birthday='" +birthday+ "' where number='" +jtnumber.gettext()+ "'" ; string sql5= "update student set department='" +department+ "' where number='" +jtnumber.gettext()+ "'" ; try { conn=drivermanager.getconnection( "jdbc:mysql://localhost:3306/javastu" , "root" , "123" ); stat=conn.createstatement(); stat.executeupdate(sql2); stat.executeupdate(sql3); stat.executeupdate(sql4); stat.executeupdate(sql5); } catch (sqlexception g) { // todo auto-generated catch block g.printstacktrace(); } try { stat.close(); conn.close(); } catch (sqlexception ar){ ar.printstacktrace(); } break ; } //change end } } catch (sqlexception e1) { // todo auto-generated catch block e1.printstacktrace(); } finally { try { conn.close(); } catch (sqlexception ar){ ar.printstacktrace(); } } } }); buttonreturn.addactionlistener( new actionlistener(){ public void actionperformed(actionevent e){ window window = new window(); } }); this .settitle( "修改學(xué)生信息" ); this .setlayout( new gridlayout( 9 , 1 )); this .add(jpnumber); this .add(jpname); this .add(jpsex); this .add(jpbirthday); this .add(jpdepartment); this .add(jpforbutton); this .setlocation( 400 , 300 ); this .setsize( 350 , 300 ); this .setvisible( true ); } } |
delete是刪除
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
|
package first; import java.sql.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; import first.window; public class delete extends jframe { private static final long serialversionuid = -1928970409928880648l; jlabel jlnumber = new jlabel( "學(xué)號:" ); jtextfield jtnumber = new jtextfield( "" , 20 ); jbutton buttondelete = new jbutton( "刪除" ); jbutton buttonreturn = new jbutton( "返回" ); public delete() { jpanel jpnumber = new jpanel(); jpanel jpforbutton = new jpanel( new gridlayout( 1 , 1 )); jpnumber.add(jlnumber); jpnumber.add(jtnumber); jpforbutton.add(buttondelete); jpforbutton.add(buttonreturn); buttondelete.addactionlistener( new actionlistener(){ public void actionperformed(actionevent e){ string number = jtnumber.gettext(); connection conn = null ; resultset res = null ; statement stat = null ; string sql = "delete from student where number='" +number+ "'" ; try { class .forname( "com.mysql.jdbc.driver" ); } catch (exception a){ a.printstacktrace(); } try { conn=drivermanager.getconnection( "jdbc:mysql://localhost:3306/javastu" , "root" , "123" ); stat = conn.createstatement(); stat.executeupdate(sql); } catch (sqlexception h){ h.printstacktrace(); } finally { try { conn.close(); system.out.println( "close success!" ); } catch (sqlexception j){ system.out.println( "close go die!" ); j.printstacktrace(); } } } }); buttonreturn.addactionlistener( new actionlistener(){ public void actionperformed(actionevent e){ window window = new window(); } }); this .settitle( "刪除學(xué)生信息" ); this .setlayout( new gridlayout( 9 , 1 )); this .add(jpnumber); this .add(jpforbutton); this .setlocation( 400 , 300 ); this .setsize( 350 , 300 ); this .setvisible( true ); } } |
look是瀏覽
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
|
package first; import java.sql.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.util.*; import first.window; public class look extends jframe { private static final long serialversionuid = -1928970409928880648l; connection conn = null ; preparedstatement ps = null ; resultset res = null ; //jbutton buttonlook = new jbutton("瀏覽"); //jbutton buttonreturn = new jbutton("返回"); jtable jtable; jscrollpane jscrollpane = new jscrollpane(); vector columnnames = null ; vector rowdata = null ; public look() { jpanel jpforbutton = new jpanel( new gridlayout( 1 , 1 )); columnnames = new vector(); columnnames.add( "學(xué)號" ); columnnames.add( "姓名" ); columnnames.add( "性別" ); columnnames.add( "出生日期" ); columnnames.add( "學(xué)院" ); rowdata = new vector(); //jpforbutton.add(buttonlook); //jpforbutton.add(buttonreturn); try { class .forname( "com.mysql.jdbc.driver" ); conn = drivermanager.getconnection( "jdbc:mysql://localhost:3306/javastu" , "root" , "123" ); ps = conn.preparestatement( "select * from student" ); res = ps.executequery(); while (res.next()) { vector hang = new vector(); hang.add(res.getstring( 1 )); hang.add(res.getstring( 2 )); hang.add(res.getstring( 3 )); hang.add(res.getstring( 4 )); hang.add(res.getstring( 5 )); rowdata.add(hang); } system.out.println( "load ok!" ); } catch (exception q){ q.printstacktrace(); system.out.println( "go die" ); } finally { try { res.close(); ps.close(); conn.close(); system.out.println( "close ok" ); } catch (sqlexception o){ o.printstacktrace(); system.out.println( "go die 2" ); } } jtable = new jtable(rowdata,columnnames); jscrollpane = new jscrollpane(jtable); this .add(jscrollpane); this .settitle( "瀏覽學(xué)生信息" ); this .setlayout( new gridlayout( 2 , 5 )); this .add(jpforbutton); this .setlocation( 300 , 300 ); this .setsize( 500 , 300 ); this .setvisible( true ); this .setresizable( false ); } } |
一些運(yùn)行的界面:
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:http://blog.csdn.net/qq_34472846/article/details/51097965