本文实例为大家分享了java gui学生图书管理的具体代码,供大家参考,具体内容如下
– mysql数据库建表:
1.book表
2.bs借书记录表
3.std学生表
4.dl登录用户表
– 列表内容
1.databd.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
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
|
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.*;
import javax.swing.border.emptyborder;
public class databd extends jframe implements actionlistener{
static framedemo a=null;
string getuser="";
string getpd="";
string sql = "select * from dl where id='";
string sql1 = "select password from dl where password='";
jbutton btnnewbutton = new jbutton("登录");
jbutton btnnewbutton_1 = new jbutton("注册");
jtextfield show=new jtextfield("\\t 请选择按钮",10);
private jpanel contentpane;
private jtextfield userfield;
private jpasswordfield pwdfield;
private statement statement = null; //查询账号
private statement statement2 = null; //注册账户
private static databd frame; //
resultset rst2=null;
public static void main(string[] args) {
eventqueue.invokelater(new runnable() {
public void run() {
try {
frame = new databd();
frame.setvisible(true);
} catch (exception e) {
e.printstacktrace();
}
}
});
}
public databd() {
string driver = "com.mysql.jdbc.driver";
string url = "jdbc:mysql://localhost:8088/library?useunicode=true&characterencoding=utf-8&usessl=false";
string user = "root";
string password = "11111";
try{
class.forname(driver);
connection conn = drivermanager.getconnection(url, user, password);
connection conn2 = drivermanager.getconnection(url, user, password);
statement = conn.createstatement();
statement2 = conn2.createstatement(resultset.type_scroll_sensitive,resultset.concur_updatable);
rst2=statement2.executequery("select * from dl");
show.seteditable(false);
setdefaultcloseoperation(jframe.exit_on_close);
setsize(250, 200);
setlocationrelativeto(null);
contentpane = new jpanel();
contentpane.setborder(new emptyborder(5, 5, 5, 5));
contentpane.setlayout(null);
add(contentpane);
jlabel lblusername = new jlabel("账号");
lblusername.setbounds(12, 13, 54, 15);
contentpane.add(lblusername);
jlabel lblpassword = new jlabel("密码");
lblpassword.setbounds(12, 38, 54, 15);
contentpane.add(lblpassword);
userfield = new jtextfield();
userfield.setbounds(76, 10, 144, 21);
contentpane.add(userfield);
userfield.setcolumns(10);
pwdfield = new jpasswordfield();
pwdfield.setechochar('*'); //密码回显字符
pwdfield.setbounds(76, 35, 144, 21);
contentpane.add(pwdfield);
pwdfield.setcolumns(10);
add(show,borderlayout.south);
btnnewbutton.addactionlistener(this); //登录
btnnewbutton.setbounds(10, 92, 93, 23);
contentpane.add(btnnewbutton);
btnnewbutton_1.addactionlistener(this); //注册
btnnewbutton_1.setbounds(127, 92, 93, 23);
contentpane.add(btnnewbutton_1);
show.addactionlistener(this);
}catch(classnotfoundexception e){system.out.print("不能找到驱动器");
}catch(exception e){system.out.print("出现错误");e.printstacktrace();}
}
public void actionperformed(actionevent e) {
getuser=userfield.gettext().trim()+"'";
string pd=string.valueof(pwdfield.getpassword());
try{
resultset rs=statement.executequery(sql+getuser);
if(e.getsource()==btnnewbutton){ //登录
if(rs.next()){
if(rs.getstring(2).equals(pd)){
frame.setvisible(false);
joptionpane.showmessagedialog(null, "登录成功");
uimanager.setlookandfeel(uimanager.getsystemlookandfeelclassname());
a=framedemo.a; //生成静态对象
}else show.settext("\\t 密码错误");
}
else
show.settext("\\t 账号不存在");
}
else if(e.getsource()==btnnewbutton_1){ //注册
if(userfield.gettext().equals("")){
show.settext("\\t 注册账号不能为空");}
else {
if((string.valueof(pwdfield.getpassword())).equals(""))
show.settext("\\t 注册密码不能为空");
else {register();}
}
}
}catch(exception c){c.printstacktrace();}
}
private void register(){
string name=userfield.gettext(),
passwd=string.valueof(pwdfield.getpassword());
try{
rst2.movetoinsertrow();
rst2.updatestring(1, name);
rst2.updatestring(2, passwd);
rst2.insertrow();
rst2.movetocurrentrow();
show.settext("\\t 注册成功");
}catch(exception e){show.settext("\\t 注册失败");}
}
}
|
2.framedemo //主框架,处理事件交给addinformation类
?
|
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
|




