本文實例為大家分享了java學生信息管理系統源碼的具體代碼,供大家參考,具體內容如下
1、studetmanage類(主界面)
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
|
package com.sms3; import java.awt.*; import javax.swing.*; import java.awt.event.*; public class studentmanage extends jframe implements actionlistener { /** * @param args */ public static void main(string[] args) { // todo auto-generated method stub new studentmanage(); } //========面板控件 private jlabel querylab = null ; private jtextfield querytxt = null ; private jbutton querybtn = null ; private jbutton allbtn = null ; private jtable resulttb = null ; private jscrollpane jsp = null ; private jbutton addbtn = null ; private jbutton deletebtn = null ; private jbutton updatebtn = null ; private jpanel top = null ; private jpanel bottom = null ; //======== private stumodel sm = null ; //構造函數 public studentmanage() { /***************************初始化面板控件***********************/ //========查詢欄 querylab = new jlabel( "請輸入姓名:" ); querytxt = new jtextfield( 10 ); querybtn = new jbutton( "查詢" ); allbtn = new jbutton( "全部" ); //......添加查詢欄監聽 querybtn.addactionlistener( this ); querybtn.setactioncommand( "query" ); allbtn.addactionlistener( this ); allbtn.setactioncommand( "all" ); //========增刪改欄 addbtn = new jbutton( "添加" ); deletebtn = new jbutton( "刪除" ); updatebtn = new jbutton( "修改" ); //......添加增刪改欄監聽 addbtn.addactionlistener( this ); addbtn.setactioncommand( "add" ); deletebtn.addactionlistener( this ); deletebtn.setactioncommand( "delete" ); updatebtn.addactionlistener( this ); updatebtn.setactioncommand( "update" ); //========創建窗口整體布局 //......頂層查詢欄 top = new jpanel(); top.add(querylab); top.add(querytxt); top.add(querybtn); top.add(allbtn); //......底層增刪改欄 bottom = new jpanel(); bottom.add(addbtn); bottom.add(deletebtn); bottom.add(updatebtn); //......中間層顯示欄 sm = new stumodel(); string sql = "select * from stu" ; sm.querystu(sql, null ); resulttb = new jtable(sm); jsp = new jscrollpane(resulttb); //......構建整體布局 this .add(top,borderlayout.north); this .add(jsp,borderlayout.center); this .add(bottom,borderlayout.south); //========設置窗口屬性 this .setsize( 400 , 300 ); this .setdefaultcloseoperation(jframe.exit_on_close); this .setvisible( true ); this .setresizable( false ); } //監聽 @override public void actionperformed(actionevent e) { // todo auto-generated method stub if (e.getactioncommand().equals( "query" )) { /*********************查詢***********************/ //========獲取輸入學生的姓名 string name = querytxt.gettext().trim(); if (name.length() != 0 ) { //========姓名輸入有效時,執行查詢 //......定義參數 string sql = "select * from stu where stuname=?" ; string []paras = {name}; //......更新模型 jtableupdate(sql, paras); } else { //========姓名為空時,設置提醒 joptionpane.showmessagedialog( this , "姓名輸入不能為空" ); } } else if (e.getactioncommand().equals( "add" )) { /*********************添加***********************/ new stuadddialog( this , "添加學生信息" , true ); string sql = "select * from stu" ; jtableupdate(sql, null ); } else if (e.getactioncommand().equals( "all" )) { /*********************全部顯示***********************/ string sql = "select * from stu" ; jtableupdate(sql, null ); } else if (e.getactioncommand().equals( "delete" )) { /*********************刪除***********************/ //========獲取選擇行號 int rownum = this .resulttb.getselectedrow(); if (rownum == - 1 ) { joptionpane.showmessagedialog( this , "請選擇一行" ); return ; } //========獲取學生id號 string stuid = (string)sm.getvalueat(rownum, 0 ); //========刪除學生 string sql = "delete from stu where stuid=?" ; string []paras = {stuid}; stumodel tmp = new stumodel(); tmp.cudstu(sql, paras); //========更新模型 sql = "select * from stu" ; jtableupdate(sql, null ); } else if (e.getactioncommand().equals( "update" )) { /*********************修改***********************/ //========獲取選擇行號 int rownum = this .resulttb.getselectedrow(); if (rownum == - 1 ) { joptionpane.showmessagedialog( this , "請選擇一行" ); return ; } new stuupdatedialog( this , "修改學生信息" , true , sm, rownum); string sql = "select * from stu" ; jtableupdate(sql, null ); } } //========更新jtable內數據 public void jtableupdate(string sql, string[] paras) { //......創建模型 sm = new stumodel(); sm.querystu(sql, paras); //......更新顯示 resulttb.setmodel(sm); } } |
2、stumodel類(學生數據庫模型)
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 com.sms3; import java.sql.resultset; import java.util.vector; import javax.swing.table.abstracttablemodel; public class stumodel extends abstracttablemodel{ private vector columnnames; private vector rowdates; // public stumodel() { string sql = "select * from stu" ; string []paras = {}; } //========增刪改學生 public boolean cudstu(string sql, string []paras) { return new sqlhelper().cudexecute(sql, paras); } //========查詢學生 public void querystu(string sql, string []paras) { sqlhelper sqlhelper = null ; //========初始化jtable信息 columnnames = new vector(); rowdates = new vector(); columnnames.add( "學號" ); columnnames.add( "名字" ); columnnames.add( "性別" ); columnnames.add( "年齡" ); columnnames.add( "籍貫" ); columnnames.add( "系別" ); try { sqlhelper = new sqlhelper(); resultset rs = sqlhelper.queryexecute(sql, paras); while (rs.next()) { vector row = new vector(); row.add(rs.getstring( 1 )); row.add(rs.getstring( 2 )); row.add(rs.getstring( 3 )); row.add(rs.getstring( 4 )); row.add(rs.getstring( 5 )); row.add(rs.getstring( 6 )); rowdates.add(row); } } catch (exception e) { // todo: handle exception } finally { sqlhelper.close(); } } @override public int getcolumncount() { // todo auto-generated method stub return this .columnnames.size(); } @override public int getrowcount() { // todo auto-generated method stub return this .rowdates.size(); } @override public object getvalueat( int row, int col) { // todo auto-generated method stub if (!rowdates.isempty()) return ((vector) this .rowdates.get(row)).get(col); else return null ; } @override public string getcolumnname( int column) { // todo auto-generated method stub return (string) this .columnnames.get(column); } } |
3、stuadddialog類(添加學生信息子界面)
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
|
package com.sms3; import java.awt.borderlayout; import java.awt.dialog; import java.awt.frame; import java.awt.gridlayout; import java.awt.event.actionevent; import java.awt.event.actionlistener; import javax.swing.*; public class stuadddialog extends jdialog implements actionlistener{ //=========面板控件 //......左側標題欄 private jlabel idlab,namelab,sexlab,agelab,jglab,deptlab; //......右側信息選擇填寫欄 private jtextfield idtxt,nametxt,sextxt,agetxt,jgtxt,depttxt; //......添加和取消按鈕 private jbutton addbtn,cancelbtn; //......布局控件 private jpanel left,center,bottom; //構造函數 public stuadddialog(frame owner, string title, boolean modal) { //========重寫父類方法 super (owner, title, modal); //========左側標簽欄 idlab = new jlabel( "學號: " ); namelab = new jlabel( "姓名: " ); sexlab = new jlabel( "性別: " ); agelab = new jlabel( "年齡: " ); jglab = new jlabel( "籍貫: " ); deptlab = new jlabel( "系別: " ); //========右側信息填寫欄 idtxt = new jtextfield(); nametxt = new jtextfield(); sextxt = new jtextfield(); agetxt = new jtextfield(); jgtxt = new jtextfield(); depttxt = new jtextfield(); //========添加和取消按鈕 addbtn = new jbutton( "添加" ); cancelbtn = new jbutton( "取消" ); //......添加監聽 addbtn.addactionlistener( this ); addbtn.setactioncommand( "add" ); cancelbtn.addactionlistener( this ); cancelbtn.setactioncommand( "cancel" ); //========創建布局 //......創建左邊欄 left = new jpanel(); left.setlayout( new gridlayout( 6 , 1 )); left.add(idlab); left.add(namelab); left.add(sexlab); left.add(agelab); left.add(jglab); left.add(deptlab); //......創建右邊欄 center = new jpanel(); center.setlayout( new gridlayout( 6 , 1 )); center.add(idtxt); center.add(nametxt); center.add(sextxt); center.add(agetxt); center.add(jgtxt); center.add(depttxt); //========底層添加和取消按鈕 bottom = new jpanel(); bottom.add(addbtn); bottom.add(cancelbtn); //========整體布局 this .add(left,borderlayout.west); this .add(center,borderlayout.center); this .add(bottom,borderlayout.south); //========設置窗口屬性 this .setsize( 300 , 250 ); this .setresizable( false ); this .setvisible( true ); } @override public void actionperformed(actionevent e) { // todo auto-generated method stub if (e.getactioncommand().equals( "add" )) { /***********************添加學生信息**************************/ stumodel tmp = new stumodel(); string sql = "insert into stu values(?,?,?,?,?,?)" ; string []paras = {idtxt.gettext(),nametxt.gettext(),sextxt.gettext(), agetxt.gettext(),jgtxt.gettext(),depttxt.gettext()}; if (!tmp.cudstu(sql, paras)) joptionpane.showmessagedialog( this , "添加學生信息失敗" ); //========關閉窗口 this .dispose(); } else if (e.getactioncommand().equals( "cancel" )) { //========關閉窗口 this .dispose(); } } } |
4、stuupdatedialog類(修改學生信息子界面)
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.sms3; import java.awt.borderlayout; import java.awt.frame; import java.awt.gridlayout; import java.awt.event.actionevent; import java.awt.event.actionlistener; import javax.swing.jbutton; import javax.swing.jdialog; import javax.swing.jlabel; import javax.swing.joptionpane; import javax.swing.jpanel; import javax.swing.jtextfield; import javax.swing.table.abstracttablemodel; public class stuupdatedialog extends jdialog implements actionlistener{ //=========面板控件 //......左側標題欄 private jlabel idlab,namelab,sexlab,agelab,jglab,deptlab; //......右側信息選擇填寫欄 private jtextfield idtxt,nametxt,sextxt,agetxt,jgtxt,depttxt; //......添加和取消按鈕 private jbutton addbtn,cancelbtn; //......布局控件 private jpanel left,center,bottom; //構造函數 public stuupdatedialog(frame owner, string title, boolean modal, stumodel sm, int rownum) { //========重寫父類方法 super (owner, title, modal); //========左側標簽欄 idlab = new jlabel( "學號: " ); namelab = new jlabel( "姓名: " ); sexlab = new jlabel( "性別: " ); agelab = new jlabel( "年齡: " ); jglab = new jlabel( "籍貫: " ); deptlab = new jlabel( "系別: " ); //========右側信息填寫欄 idtxt = new jtextfield(); idtxt.settext((string)sm.getvalueat(rownum, 0 )); idtxt.seteditable( false ); nametxt = new jtextfield(); nametxt.settext((string)sm.getvalueat(rownum, 1 )); sextxt = new jtextfield(); sextxt.settext((string)sm.getvalueat(rownum, 2 )); agetxt = new jtextfield(); agetxt.settext((string)sm.getvalueat(rownum, 3 )); jgtxt = new jtextfield(); jgtxt.settext((string)sm.getvalueat(rownum, 4 )); depttxt = new jtextfield(); depttxt.settext((string)sm.getvalueat(rownum, 5 )); //========添加和取消按鈕 addbtn = new jbutton( "修改" ); cancelbtn = new jbutton( "取消" ); //......添加監聽 addbtn.addactionlistener( this ); addbtn.setactioncommand( "update" ); cancelbtn.addactionlistener( this ); cancelbtn.setactioncommand( "cancel" ); //========創建布局 //......創建左邊欄 left = new jpanel(); left.setlayout( new gridlayout( 6 , 1 )); left.add(idlab); left.add(namelab); left.add(sexlab); left.add(agelab); left.add(jglab); left.add(deptlab); //......創建右邊欄 center = new jpanel(); center.setlayout( new gridlayout( 6 , 1 )); center.add(idtxt); center.add(nametxt); center.add(sextxt); center.add(agetxt); center.add(jgtxt); center.add(depttxt); //========底層添加和取消按鈕 bottom = new jpanel(); bottom.add(addbtn); bottom.add(cancelbtn); //========整體布局 this .add(left,borderlayout.west); this .add(center,borderlayout.center); this .add(bottom,borderlayout.south); //========設置窗口屬性 this .setsize( 300 , 250 ); this .setresizable( false ); this .setvisible( true ); } @override public void actionperformed(actionevent e) { // todo auto-generated method stub if (e.getactioncommand().equals( "update" )) { /***********************修改學生信息**************************/ stumodel tmp = new stumodel(); string sql = "update stu set stuname=?,stusex=?,stuage=?,stujg=?,studept=? where stuid=?" ; string []paras = {nametxt.gettext(),sextxt.gettext(),agetxt.gettext(), jgtxt.gettext(),depttxt.gettext(),idtxt.gettext()}; if (!tmp.cudstu(sql, paras)) joptionpane.showmessagedialog( this , "修改學生信息失敗" ); //========關閉窗口 this .dispose(); } else if (e.getactioncommand().equals( "cancel" )) { //========關閉窗口 this .dispose(); } } } |
5、sqlhelper類(最底層數據庫類)
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
|
package com.sms3; import java.sql.*; public class sqlhelper { //========數據庫 private connection ct = null ; private preparedstatement ps = null ; private resultset rs = null ; private string driver = "com.microsoft.sqlserver.jdbc.sqlserverdriver" ; private string url = "jdbc:sqlserver://127.0.0.1:1433;database=studentman" ; private string user = "sa" ; private string passwd = "****" ; //========查詢 public resultset queryexecute(string sql, string []paras) { try { //========1、加載驅動 class .forname(driver); //========2、連接 ct = drivermanager.getconnection(url, user, passwd); //========3、創建preparedstatement ps = ct.preparestatement(sql); //========4、給問號賦值 if (paras != null ) { for ( int i = 0 ; i < paras.length; i++) { ps.setstring(i + 1 , paras[i]); } } //========5、執行 rs = ps.executequery(); } catch (exception e) { // todo: handle exception e.printstacktrace(); } finally { //this.close(); } //========返回值 return rs; } //========增刪改 public boolean cudexecute(string sql, string []paras) { boolean b = true ; try { //========1、加載驅動 class .forname(driver); //========2、連接 ct = drivermanager.getconnection(url, user, passwd); //========3、創建preparedstatement ps = ct.preparestatement(sql); //========4、給問號賦值 for ( int i = 0 ; i < paras.length; i++) { ps.setstring(i + 1 , paras[i]); } //========5、執行 if (ps.executeupdate() != 1 ) b = false ; } catch (exception e) { // todo: handle exception b = false ; e.printstacktrace(); } finally { this .close(); } //========返回值 return b; } //========關閉資源 public void close() { try { if (rs!= null ) rs.close(); if (ps!= null ) ps.close(); if (ct!= null ) ct.close(); } catch (exception e2) { // todo: handle exception e2.printstacktrace(); } } } |
主界面
添加學生信息界面
修改學生信息界面
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:http://blog.csdn.net/xsc_c/article/details/12526963