服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|JavaScript|易语言|

服务器之家 - 编程语言 - JAVA教程 - java GUI实现学生图书管理简单实例

java GUI实现学生图书管理简单实例

2021-03-19 10:50wqdsb JAVA教程

这篇文章主要为大家详细介绍了java GUI实现学生图书管理简单示例,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了java gui学生图书管理的具体代码,供大家参考,具体内容如下

- mysql数据库建表:

1.book表

java GUI实现学生图书管理简单实例

2.bs借书记录表

java GUI实现学生图书管理简单实例

3.std学生表

java GUI实现学生图书管理简单实例

4.dl登录用户表

.java GUI实现学生图书管理简单实例

- 列表内容

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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
package database;
import javax.swing.*;
import javax.swing.border.titledborder;
 
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.util.calendar;
import java.util.gregoriancalendar;
public class framedemo extends jframe implements actionlistener{
  public static framedemo a=new framedemo();
  jpanel panel=new jpanel(),
      panel1=new jpanel(),
      panel2=new jpanel(),
      panel3=new jpanel(),
      panel4=new jpanel(),
      panel5=new jpanel();
  string[] adds={"请选择操作","添加馆藏记录","添加借书记录","添加学生记录"};
  string[] deletes={"请选择操作","删除馆藏记录","删除借书记录","删除学生记录"};
  string[] changes={"请选择操作","修改库存记录","修改应还日期"};
  string[] querys={"请选择操作","查询书籍","查询书号","查询学生借书记录","查询学生信息"};
  string[] shows={"请选择操作","显示所有馆藏记录","显示借书记录","显示学生记录"};
  string[] record=new string[4];
  jtextarea ta=new jtextarea("-->欢迎使用系统\n",11,60);
  jscrollpane pane=new jscrollpane(ta);
  connection conn=null;
  statement stmt=null;
  statement stmt1=null;
  statement stmt2=null;
  resultset rst=null;
  resultset rst1=null;
  resultset rst2=null;
  static boolean key;
  private jframe big=new jframe("请选择操作");
  private jcombobox add=new jcombobox(adds);
  private jcombobox delete=new jcombobox(deletes);
  private jcombobox change=new jcombobox(changes);
  private jcombobox query=new jcombobox(querys);
  private jcombobox show=new jcombobox(shows);
  public framedemo(){
    super("图书馆管理系统");
    try{
      class.forname("com.mysql.jdbc.driver");
      string url="jdbc:mysql://localhost:8088/library?useunicode=true&characterencoding=utf-8&usessl=false";
      string user="root";
      string password="11111";
      conn=drivermanager.getconnection(url, user, password);    
      panel4.setlayout(new gridlayout(3,1,30,10));
      panel4.setborder(new titledborder("选择管理:"));
      panel1.add(new jlabel("添加记录:"));
      panel1.add(add);
      panel1.add(new jlabel("删除记录:"));
      panel1.add(delete);
      panel2.add(new jlabel("修改记录:"));
      panel2.add(change);
      panel2.add(new jlabel("查询记录:"));
      panel2.add(query);
      panel3.add(new jlabel("显示记录:"));
      panel3.add(show);
      panel4.add(panel1);panel4.add(panel2);panel4.add(panel3);   
      panel5.add(pane);
      panel.add(panel5,borderlayout.south);
      panel.add(panel4,borderlayout.center);
      add(panel);
      //pack();
      stmt=conn.createstatement(resultset.type_scroll_sensitive,resultset.concur_updatable);//可滚可更     
      stmt1=conn.createstatement(resultset.type_scroll_sensitive,resultset.concur_updatable);    
      stmt2=conn.createstatement(resultset.type_scroll_sensitive,resultset.concur_updatable);
 
      add.addactionlistener(this);
      delete.addactionlistener(this);
      change.addactionlistener(this);
      query.addactionlistener(this);
      show.addactionlistener(this);
 
      setsize(470,440);
      //setresizable(false);
      setlocationrelativeto(null);
      setvisible(true);
      setdefaultcloseoperation(jframe.exit_on_close);
      }catch(exception e){e.printstacktrace();}
 
  }
  public void actionperformed(actionevent ae){
    try{
      //if(key){   //数据库非空
       if(add.getselectedindex()==1){        
        ta.append("-->添加馆藏记录\n");
        addinformation a1=new addinformation("图书编号", "书名", "库存",1);      
       }
       else if(add.getselectedindex()==2){
        ta.append("-->添加借书记录\n");
        addinformation a1=new addinformation("图书编号", "学号",2);
       }
       else if(add.getselectedindex()==3){
         ta.append("-->添加学生记录\n");
         addinformation a1=new addinformation("学生学号", "学生姓名",3);
       }
       add.setselecteditem("请选择操作");
 
       if(delete.getselectedindex()==1){
        ta.append("-->删除馆藏记录\n");
        addinformation a1=new addinformation("图书编号",4);
       }
       else if(delete.getselectedindex()==2){
         ta.append("-->删除借书记录\n");
         addinformation a1=new addinformation("图书编号","学生学号",5);
       }
       else if(delete.getselectedindex()==3){
        ta.append("-->删除学生记录\n");
        addinformation a1=new addinformation("学生学号",6);
       }
       delete.setselecteditem("请选择操作");
 
       if(change.getselectedindex()==1){
        ta.append("-->修改库存记录\n");
        addinformation a1=new addinformation("图书编号","库存数目",7);
       }
       else if(change.getselectedindex()==2){
        ta.append("-->修改应还日期\n");
        addinformation a1=new addinformation("图书编号","学生学号","应还日期",8); 
       }
       change.setselecteditem("请选择操作");
 
       if(query.getselectedindex()==1){
        ta.append("-->按书名查询\n");
        addinformation a1=new addinformation("书名",9); 
       }
       else if(query.getselectedindex()==2){
        ta.append("-->按书籍编号查询\n");
        addinformation a1=new addinformation("书籍编号",10);
       }
       else if(query.getselectedindex()==3){
        ta.append("-->查询学生借书记录\n");
        addinformation a1=new addinformation("学生学号",11);
       }
       else if(query.getselectedindex()==4){
        ta.append("-->查询学生信息\n");
        addinformation a1=new addinformation("学生学号",12); 
       }
       query.setselecteditem("请选择操作");
 
       if(show.getselectedindex()==1){
         ta.append("-->显示所有馆藏记录\n");
         try{
          rst=stmt.executequery("select * from book");
          rst.first();
          ta.append("------------------------------------\n");
          int id=rst.getint(1);
          string name=rst.getstring(2);
          int num=rst.getint(3);
          ta.append("\t书籍编号\t书籍名称\t在馆库存\n");
          ta.append("\t"+id+"\t"+name+"\t"+num+"\n");
          while(rst.next()){
              id=rst.getint(1);
              name=rst.getstring(2);
              num=rst.getint(3);
              ta.append("\t"+id+"\t"+name+"\t"+num+"\n");
            }//执行后游标指向最后一行的后一个位置
            rst.first();
            ta.append("------------------------------------\n");
          }catch(exception e){ta.append("-->显示错误");}
       }
       else if(show.getselectedindex()==2){
         ta.append("-->显示借书记录\n");       
         rst1=stmt1.executequery("select * from bs");
         calendar calendar =new gregoriancalendar();
         try{
          rst1.first();
          ta.append("----------------------------------------------------\n");
          int bookid=rst1.getint(2);
          int stdid=rst1.getint(3);
          date x=rst1.getdate(4);    
           calendar.settime(x);
           calendar.add(calendar.date, 30);
           java.util.date utildate = (java.util.date)calendar.gettime();
           utildate = (java.util.date)calendar.gettime();
           date newdate =new date(utildate.gettime()); 
          ta.append("\t书籍编号\t学生学号\t借书日期\t\t应还日期\n");
          ta.append("\t"+bookid+"\t"+stdid+"\t"+x+"\t"+newdate+"\n");      
          while(rst1.next()){
              bookid=rst1.getint(2);
              stdid=rst1.getint(3);
              x=rst1.getdate(4);
              calendar.settime(x);
              calendar.add(calendar.date, 30);
              utildate = (java.util.date)calendar.gettime();
              utildate = (java.util.date)calendar.gettime();
              newdate =new date(utildate.gettime());
              ta.append("\t"+bookid+"\t"+stdid+"\t"+x+"\t"+newdate+"\n");
            }//执行后游标指向最后一行的后一个位置
            rst1.first();
            ta.append("----------------------------------------------------\n");
          }catch(exception e){ta.append("-->显示错误");e.printstacktrace();}
       }
       else if(show.getselectedindex()==3){
         ta.append("-->显示学生记录\n");
         try{
          rst2=stmt2.executequery("select * from std");
          rst2.first();
          ta.append("------------------------------------\n");
          int id=rst2.getint(1);
          string name=rst2.getstring(2);
          ta.append("\t学生学号\t学生姓名\n");
          ta.append("\t"+id+"\t"+name+"\t"+"\n");
          while(rst2.next()){
              id=rst2.getint(1);
              name=rst2.getstring(2);
              ta.append("\t"+id+"\t"+name+"\t"+"\n");
            }//执行后游标指向最后一行的后一个位置
          rst2.first();
          ta.append("------------------------------------\n");
          }catch(exception e){ta.append("-->显示错误");}
       }
       show.setselecteditem("请选择操作");
       ta.setcaretposition(ta.gettext().length()); //滚动条保持在最后一条   
     }catch(exception e){e.printstacktrace();}
  }
  public void showmessage(){
    try{
      int a=rst.getrow();
      ta.append(a+"\n");
      rst.first();
      ta.append("---------------------------\n");
      string id=rst.getstring(1),
          name=rst.getstring(2),
          brand=rst.getstring(3),
          price=rst.getstring(4),
          stock=rst.getstring(5);    
      ta.append(id+" "+name+" "+brand+" "+price+" "+stock+"\n");
      while(rst.next()){
        id=rst.getstring(1);
        name=rst.getstring(2);
        brand=rst.getstring(3);
        price=rst.getstring(4);
        stock=rst.getstring(5);
        ta.append(id+" "+name+" "+brand+" "+price+" "+stock+"\n");
      }//执行后游标指向最后一行的后一个位置
      rst.absolute(a);
      ta.append("---------------------------\n");
    }catch(exception e){ta.append("-->显示错误");}
  }
// public static void main(string[] args){  
//       try{
//uimanager.setlookandfeel(uimanager.getsystemlookandfeelclassname());    
//       }catch(exception e){system.out.print("1111111111");}
//       a=new framedemo();
//     }
 
  }

3.addinformation.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
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
package database;
import javax.swing.*;
import java.util.calendar;
import java.util.gregoriancalendar;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
public class addinformation extends jframe implements actionlistener{
  jbutton sure=new jbutton("确定"),
      quxiao=new jbutton("取消");
  jtextfield jtf1=new jtextfield(10),
      jtf2=new jtextfield(10),
      jtf3=new jtextfield(10);
  connection conn=null;
  static{
    try{
    class.forname("com.mysql.jdbc.driver");
    }catch(exception e){e.printstacktrace();}   
  }
  int x=0;
  public connection getconnection() throws sqlexception{
    if(conn==null){
      conn=drivermanager.getconnection("jdbc:mysql://localhost:8088/library?useunicode=true&characterencoding=utf-8&usessl=false","root","11111");
    }
    return conn;
  }
  addinformation(string s1,string s2,string s3,int m){
    jpanel p=new jpanel();
    p.setlayout(new flowlayout());
    p.add(new jlabel(s1));
    p.add(jtf1);
    p.add(new jlabel(s2));
    p.add(jtf2);
    p.add(new jlabel(s3));
    p.add(jtf3); 
    p.add(sure);
    p.add(quxiao);
    add(p);
    if(m==8){
      joptionpane.showmessagedialog(null, "图书日期应为 xxxx-xx-xx 格式!","警告",joptionpane.warning_message);
    }
    setsize(500, 100);
    setvisible(true);
    setdefaultcloseoperation(jframe.dispose_on_close);      
    setlocationrelativeto(null);
    sure.addactionlistener(this);
    quxiao.addactionlistener(this);
    x=m;
  }
  addinformation(string s1,string s2,int m){  
    jpanel p=new jpanel();
    p.setlayout(new flowlayout());
    p.add(new jlabel(s1));
    p.add(jtf1);
    p.add(new jlabel(s2));
    p.add(jtf2); 
    p.add(sure);
    p.add(quxiao);
    add(p);
    setsize(500, 100);
    setvisible(true);
    setdefaultcloseoperation(jframe.dispose_on_close);      
    setlocationrelativeto(null);
    sure.addactionlistener(this);
    quxiao.addactionlistener(this);
    x=m;
  }
  addinformation(string s1,int m){
    jpanel p=new jpanel();
    p.setlayout(new flowlayout());
    p.add(new jlabel(s1));
    p.add(jtf1);
    if(m==4){
    joptionpane.showmessagedialog(null, "若存在借阅记录则不能删除书籍信息!","警告",joptionpane.warning_message);
    }
    if(m==6){
    joptionpane.showmessagedialog(null, "若学生存在借阅记录则不能删除该学生信息!","警告",joptionpane.warning_message);
    }
    p.add(sure);
    p.add(quxiao);
    add(p);
    setsize(500, 80);
    setvisible(true);
    setdefaultcloseoperation(jframe.dispose_on_close);      
    setlocationrelativeto(null);
    sure.addactionlistener(this);
    quxiao.addactionlistener(this);
    x=m;
  }
 
  public void actionperformed(actionevent ae){
    if(ae.getsource()==sure){
      if(x==1){  //添加 馆藏记录
        string sql="insert into book values(?,?,?)";
        try
        connection conn=getconnection();
        preparedstatement stmt=conn.preparestatement(sql)){
        int id=integer.parseint(jtf1.gettext());
        string name=jtf2.gettext();
        int num=integer.parseint(jtf3.gettext());
        stmt.setint(1, id);
        stmt.setstring(2, name);
        stmt.setint(3, num);
        stmt.executeupdate();
        framedemo.a.ta.append("-->添加成功\n");     
        }catch(exception x){framedemo.a.ta.append("-->插入出错\n");x.printstacktrace();}
        addinformation.this.dispose();
      }
      if(x==2){  //添加借书记录
          string sql="insert into bs(bookid,stdid,date) values(?,?,?)";
          string sql1="select id,num from book where id=?";
          string sql2="select id from std where id=?";
          try{
          connection conn1=getconnection();
          preparedstatement stmt1=conn1.preparestatement(sql1,resultset.type_scroll_sensitive,resultset.concur_updatable);
          stmt1.setint(1,integer.parseint(jtf1.gettext()));         
          resultset rst1=stmt1.executequery();//找有没有这本书和库存是否为0
          if(rst1.next()&&(rst1.getint(2)>0)){
            int n=rst1.getint(2);n-=1;
            int m=rst1.getint(1);
            rst1.updateint(1, m);
            rst1.updateint(2, n);
            rst1.updaterow();
            connection conn2=getconnection();
            preparedstatement stmt2=conn2.preparestatement(sql2);
            stmt2.setint(1,integer.parseint(jtf2.gettext()));
            resultset rst2=stmt2.executequery();//有人
            if(rst2.next()){
              connection conn=getconnection();
              preparedstatement stmt=conn.preparestatement(sql);
              int bookid=integer.parseint(jtf1.gettext());
              int stdid=integer.parseint(jtf2.gettext());
              stmt.setint(1, bookid);
              stmt.setint(2, stdid);
              stmt.setdate(3, new date(system.currenttimemillis()));
              stmt.executeupdate();
              framedemo.a.ta.append("-->添加成功\n");
            }
            else{framedemo.a.ta.append("-->无该学生记录,不能添加借阅记录\n");}
          }else{framedemo.a.ta.append("-->无此书或已全部借出\n");}             
          }catch(exception x){framedemo.a.ta.append("-->插入出错\n");x.printstacktrace();}
          addinformation.this.dispose();      
       }
      if(x==3){  //添加学生记录
        string sql="insert into std values(?,?)";
        try
        connection conn=getconnection();
        preparedstatement stmt=conn.preparestatement(sql)){
        int id=integer.parseint(jtf1.gettext());
        string name=jtf2.gettext();
        stmt.setint(1, id);
        stmt.setstring(2, name);
        stmt.executeupdate();
        framedemo.a.ta.append("-->添加成功\n");   
        }catch(exception x){framedemo.a.ta.append("-->插入出错\n");x.printstacktrace();}
        addinformation.this.dispose();      
      }
      if(x==4){  //删除图书记录
        string sql="delete from book where id=?";
        string sql1="select * from bs where bookid=?";
        try{
        connection conn1=getconnection();
        preparedstatement stmt1=conn1.preparestatement(sql1);
        stmt1.setint(1,integer.parseint(jtf1.gettext()));
        resultset rst1=stmt1.executequery();
        if(rst1.next()){  //借阅记录中有书
          joptionpane.showmessagedialog(null, "借阅记录中有该书的借阅记录!","警告",joptionpane.warning_message);
        }
        else{    //借阅记录无书,可以删掉
          connection conn=getconnection();
          preparedstatement stmt=conn.preparestatement(sql);
          int id=integer.parseint(jtf1.gettext());
          stmt.setint(1, id);
          int t=stmt.executeupdate();
          if(t==0){
          framedemo.a.ta.append("-->删除出错\n");
          joptionpane.showmessagedialog(null, "不存在该图书信息!","警告",joptionpane.warning_message);
          }
          else framedemo.a.ta.append("-->删除记录成功\n");
         }         
        }catch(exception x){framedemo.a.ta.append("-->删除失败\n");x.printstacktrace();}
        addinformation.this.dispose();
      }
      if(x==5){  //删除借书记录
        string sql="delete from bs where bookid=? and stdid=?";
        string sql1="select id,num from book where id=?";
        try{     
        connection conn=getconnection();
        preparedstatement stmt=conn.preparestatement(sql);
        int bookid=integer.parseint(jtf1.gettext());
        int stdid=integer.parseint(jtf2.gettext());
        stmt.setint(1, bookid);
        stmt.setint(2, stdid);
        int t=stmt.executeupdate();
        if(t!=0){
          connection conn1=getconnection();
          preparedstatement stmt1=conn1.preparestatement(sql1,resultset.type_scroll_sensitive,resultset.concur_updatable);
          stmt1.setint(1,integer.parseint(jtf1.gettext())); 
          resultset rst1=stmt1.executequery();rst1.next();
          int m=rst1.getint(1);
          int n=rst1.getint(2);n+=1;
          rst1.updateint(1, m);
          rst1.updateint(2, n);
          rst1.updaterow();
          framedemo.a.ta.append("-->删除借阅记录成功\n");
        }
        else{
          framedemo.a.ta.append("-->删除出错\n");
          joptionpane.showmessagedialog(null, "不存在这条借阅记录!","警告",joptionpane.warning_message);
        }          
        }catch(exception x){framedemo.a.ta.append("-->删除出错\n");x.printstacktrace();}
        addinformation.this.dispose();
      }
      if(x==6){ //删除学生记录
        string sql="delete from std where id=?";
        string sql1="select * from bs where stdid=?";
        try {
          connection conn1=getconnection();
          preparedstatement stmt1=conn1.preparestatement(sql1);
          stmt1.setint(1,integer.parseint(jtf1.gettext()));
          resultset rst1=stmt1.executequery();
          if(rst1.next()){  //借阅记录中有该人
            joptionpane.showmessagedialog(null, "借阅记录中有该人的借阅记录!","警告",joptionpane.warning_message);
          }
          else{
            connection conn=getconnection();
            preparedstatement stmt=conn.preparestatement(sql);
            int stdid=integer.parseint(jtf1.gettext());
            stmt.setint(1, stdid);
            int t=stmt.executeupdate();
            if(t==0){
              framedemo.a.ta.append("-->删除出错\n");
              joptionpane.showmessagedialog(null, "不存在该学生记录!","警告",joptionpane.warning_message);
              }
            else framedemo.a.ta.append("-->删除记录成功\n");
            }            
        }catch(exception x){framedemo.a.ta.append("-->删除出错\n");x.printstacktrace();}
        addinformation.this.dispose();
      }
      if(x==7){  //修改图书库存记录
        string sql="update book set num=? where id=?";
        try
        connection conn=getconnection();
        preparedstatement stmt=conn.preparestatement(sql)){
        int num=integer.parseint(jtf2.gettext());
        int bookid=integer.parseint(jtf1.gettext());
        stmt.setint(1, num);
        stmt.setint(2, bookid);
        int t=stmt.executeupdate();
        if(t==0){
          framedemo.a.ta.append("-->修改出错\n");
          joptionpane.showmessagedialog(null, "不存在该图书记录!","警告",joptionpane.warning_message);
        }
        else framedemo.a.ta.append("-->修改记录成功\n");   
        }catch(exception x){framedemo.a.ta.append("-->修改出错\n");x.printstacktrace();}
        addinformation.this.dispose();
      }
      if(x==8){  //修改应还日期
        string sql="update bs set date=? where bookid=? and stdid=?";
        try
        connection conn=getconnection();
        preparedstatement stmt=conn.preparestatement(sql)){
        date date = date.valueof(jtf3.gettext());
        calendar calendar =new gregoriancalendar();
        calendar.settime(date);
        calendar.add(calendar.date, -30);
        java.util.date utildate = (java.util.date)calendar.gettime();
        utildate = (java.util.date)calendar.gettime();
        date newdate =new date(utildate.gettime());
        int stdid=integer.parseint(jtf2.gettext());
        int bookid=integer.parseint(jtf1.gettext());
        stmt.setdate(1, newdate);       
        stmt.setint(2, bookid);
        stmt.setint(3, stdid);
        int t=stmt.executeupdate();
        if(t==0){
          framedemo.a.ta.append("-->修改出错\n");
          joptionpane.showmessagedialog(null, "不存在该借书记录!","警告",joptionpane.warning_message);
        }
        else framedemo.a.ta.append("-->修改记录成功\n");   
        }catch(exception x){framedemo.a.ta.append("-->修改出错\n");x.printstacktrace();}
        addinformation.this.dispose();
      }
      if(x==9){  //按书名查询
        string sql="select * from book where name=?";
        resultset rst=null;
        try
        connection conn=getconnection();
        preparedstatement stmt=conn.preparestatement(sql)){
        string name=jtf1.gettext();
        stmt.setstring(1, name);       
        rst=stmt.executequery();
        if(rst.next()){
          framedemo.a.ta.append("---------------------------------------\n");
          framedemo.a.ta.append("\t图书编号\t书名\t在馆数\n");
          framedemo.a.ta.append("\t"+rst.getint(1)+"\t"+rst.getstring(2)+"\t"+rst.getint(3)+"\n");
          framedemo.a.ta.append("---------------------------------------\n");
        }
        else{
          framedemo.a.ta.append("-->查询出错\n");
          joptionpane.showmessagedialog(null, "不存在该书!","警告",joptionpane.warning_message); 
        }
        }catch(exception x){framedemo.a.ta.append("-->查询出错\n");x.printstacktrace();}
        addinformation.this.dispose();
      
      if(x==10){  //按书籍编号查询
        string sql="select * from book where id=?";
        resultset rst=null;
        try
        connection conn=getconnection();
        preparedstatement stmt=conn.preparestatement(sql)){
        stmt.setint(1, integer.parseint(jtf1.gettext()));       
        rst=stmt.executequery();
        if(rst.next()){
          framedemo.a.ta.append("---------------------------------------\n");
          framedemo.a.ta.append("\t图书编号\t书名\t在馆数\n");
          framedemo.a.ta.append("\t"+rst.getint(1)+"\t"+rst.getstring(2)+"\t"+rst.getint(3)+"\n");
          framedemo.a.ta.append("---------------------------------------\n");
        }
        else{
          framedemo.a.ta.append("-->查询出错\n");
          joptionpane.showmessagedialog(null, "不存在该书!","警告",joptionpane.warning_message); 
        }
        }catch(exception x){framedemo.a.ta.append("-->查询出错\n");x.printstacktrace();}
        addinformation.this.dispose();
      }
      if(x==11){  //按学生借书记录查询
        string sql="select bookid,stdid,date from bs where stdid=?";
        resultset rst=null;
        try
        connection conn=getconnection();
        preparedstatement stmt=conn.preparestatement(sql)){
        stmt.setint(1, integer.parseint(jtf1.gettext()));       
        rst=stmt.executequery();
        calendar calendar =new gregoriancalendar();
        if(rst.next()){
          framedemo.a.ta.append("----------------------------------------------------\n");
          framedemo.a.ta.append("\t图书编号\t学生学号\t借书时间\t\t应还时间\n");
          date x=rst.getdate(3);       
           calendar.settime(x);
           calendar.add(calendar.date, 30);
           java.util.date utildate = (java.util.date)calendar.gettime();
           utildate = (java.util.date)calendar.gettime();
           date newdate =new date(utildate.gettime());
           framedemo.a.ta.append("\t"+rst.getint(1)+"\t"+rst.getint(2)+"\t"+x+"\t"+newdate+"\n");
          while(rst.next()){      
           x=rst.getdate(3);
           calendar.settime(x);
           calendar.add(calendar.date, 30);
           utildate = (java.util.date)calendar.gettime();
           utildate = (java.util.date)calendar.gettime();
           newdate =new date(utildate.gettime());
           framedemo.a.ta.append("\t"+rst.getint(1)+"\t"+rst.getint(2)+"\t"+x+"\t"+newdate+"\n");
           }
          framedemo.a.ta.append("----------------------------------------------------\n");
          }
          else{
          framedemo.a.ta.append("-->查询出错\n");
          joptionpane.showmessagedialog(null, "不存在该借书记录!","警告",joptionpane.warning_message); 
         }
        }catch(exception x){framedemo.a.ta.append("-->查询出错\n");x.printstacktrace();}
        addinformation.this.dispose();
      }
      if(x==12){  //查询学生信息
        string sql="select * from std where id=?";
        resultset rst=null;
        try
        connection conn=getconnection();
        preparedstatement stmt=conn.preparestatement(sql)){
        stmt.setint(1, integer.parseint(jtf1.gettext()));       
        rst=stmt.executequery();
        if(rst.next()){
          framedemo.a.ta.append("---------------------------------------\n");
          framedemo.a.ta.append("\t学生学号\t学生姓名\t\n");     
          framedemo.a.ta.append("\t"+rst.getint(1)+"\t"+rst.getstring(2)+"\n");
          framedemo.a.ta.append("---------------------------------------\n");
          }
          else{
          framedemo.a.ta.append("-->查询出错\n");
          joptionpane.showmessagedialog(null, "不存在该学生!","警告",joptionpane.warning_message);  
         }
        }catch(exception x){framedemo.a.ta.append("-->查询出错\n");x.printstacktrace();}
        addinformation.this.dispose();
      }
    }
    if(ae.getsource()==quxiao){
      addinformation.this.dispose();
      framedemo.a.ta.append("-->操作取消\n");
    }
 
  }
}

java GUI实现学生图书管理简单实例 java GUI实现学生图书管理简单实例

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:http://blog.csdn.net/qq_28929579/article/details/53562552

延伸 · 阅读

精彩推荐