本文实例为大家分享了java实现简单图书借阅系统的具体代码,供大家参考,具体内容如下
直接看代码:
package ttt;import java.awt.borderlayout;import java.awt.event.actionevent;import java.awt.event.actionlistener;import java.sql.connection;import java.sql.drivermanager;import java.sql.resultt;import java.sql.statement;import javax.swing.jbutton;import javax.swing.jframe;import javax.swing.joptionpane;import javax.swing.jpanel;import javax.swing.jscrollpane;import javax.swing.jtable;import javax.swing.border.titledborder;import javax.swing.table.defaulttablemodel;public class labmsys extends jframe{ jtable booktable=null; static defaulttablemodel bookmodel=null; jtable borrowtable=null; static defaulttablemodel bmodel=null; jpanel j1=new jpanel(); jpanel j2=new jpanel(); jpanel j3=new jpanel(); jbutton button1=new jbutton("借书"); jbutton button2=new jbutton("还书"); public labmsys() { this.tlayout(new borderlayout()); this.add(j1,borderlayout.west); this.add(j2,borderlayout.east); this.add(j3,borderlayout.center); j1.tborder(new titledborder("图书借书表")); j2.tborder(new titledborder("图书库存表")); this.booktable=bookin(); this.borrowtable=borrowin(); jscrollpane jsp1=new jscrollpane(borrowtable); jscrollpane jsp2=new jscrollpane(booktable); j1.add(jsp1); j2.add(jsp2); j3.add(button1); j3.add(button2); //添加借书监听器 button1.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent e) { // todo auto-generated method stub int rownum=booktable.getlectedrow(); system.out.println(rownum); if(rownum>=0) { string isbn=booktable.getvalueat(rownum, 0).tostring(); system.out.println(isbn); int count=integer.parint((string) booktable.getvalueat(rownum, 2)); string s=joptionpane.showinputdialog("请输入学号"); if(count<=0) joptionpane.showmessagedialog(null,"图书库存不足"); el if (null==s) joptionpane.showmessagedialog(null,"请输入学号"); el{ if(borrowbook(isbn,s)) { system.out.println("yes"); joptionpane.showmessagedialog(null,"successful!"); } el joptionpane.showmessagedialog(null,"wrong!"); } }el joptionpane.showmessagedialog(null,"choo book!"); } }); //添加还书监听器 button2.addactionlistener(new actionlistener() { @override public void actionperf石河子大学怎么样ormed(actionevent e) { // todo auto-generated method stub int rownum=borrowtable.getlectedrow(); system.out.println(rownum); if(rownum>=0) { string isbn=borrowtable.getvalueat(rownum, 2).tostring(); string tablexh = borrowtable.getvalueat(rownum, 1).tostring(); system.out.println(isbn); string xh=joptionpane.showinputdialog("请输入学号"); if (!xh.equals(tablexh)) joptionpane.showmessagedialog(null,"学号不正确"); el{ if(returnbook(isbn)) { joptionpane.showmessagedialog(null,"successful!"); } 高中毕业证编号 el joptionpane.showmessagedialog(null,"wrong!"); } }el joptionpane.showmessagedialog(null,"choo book!"); } }); } public boolean borrowbook(string isbn, string s) { int xh = integer.parint(s); int isbn2 =integer.parint(isbn); system.out.println("学号:"+xh); boolean b=fal; string driver="com.mysql.jdbc.driver"; string url="jdbc:mysql://localhost:3306/test"; string ur="root"; string password="123456"; connection conn=null; statement stmt=null; try { class.forname(driver); conn = drivermanager.getconnection(url, ur, password); stmt = conn.createstatement(); system.out.println("isbn:"+isbn); conn.tautocommit(fal); string sql1 = "update book t count=count-1 where isbn='"+isbn2+"'"; string sql2 = "inrt into borrow(xh,isbn) values('"+xh+"','"+isbn2+"')"; stmt.executeupdate(sql1); stmt.executeupdate(sql2); system.out.println("isbn2:"+isbn2); //stmt = (preparedstatement) conn.preparestatement("update book t count=count-1 where isbn=?"); conn.commit(); stmt.clo(); conn.clo(); b=true; } catch (exception e) { try{ conn.rollback(); }catch(exception e1){e1.printstacktrace();} } return b; } private boolean returnbook(string isbn) { int isbn2 =integer.parint(isbn); boolean b=fal; string driver="com.mysql.jdbc.driver"; string url="jdbc:mysql://localhost:3306/test"; string ur="root"; string password="123456"; connection conn=null; statement stmt=null; try { class.forname(driver); conn = drivermanager.getconnection(url, ur, password); stmt = conn.createstatement(); conn.tautocommit(fal); stmt.executeupdate("delete from borrow where isbn='"+isbn2+"'"); stmt.executeupdate("update book t count=count+1 where isbn='"+isbn2+"'"); system.out.println("还书isbn2:"+isbn2); conn.commit(); stmt.clo(); conn.clo(); b=true; } catch (exception e) { try{ conn.rollback(); }catch(exception e1){e1.printstacktrace();}} return b; } private jtable borrowin() { // todo auto-generated method stub string []head={"id","xh","isbn"}; string [][]data=null; bookmodel=new defaulttablemodel(data,head); jtable t=new jtable(bookmodel); string driver="com.mysql.jdbc.driver"; string url="jdbc:mysql://localhost:3306/test"; string ur="root"; string password="123456"; connection conn=null; statement stmt=null; resultt rs=null; try { class.forname(driver); conn = drivermanager.getconnection(url, ur, password); stmt = conn.createstatement(); rs=stmt.executequery("lect * from borrow"); string[] row=new string[3]; while(rs.next()){ row[0]=rs.getstring(1); row[1]=rs.getstring(2); row[2]=rs.getstring(3); bookmodel.addrow(row); bookmodel.firetabledatachanged(); } rs.clo(); stmt.clo(); conn.clo();} catch (exception e) { // todo auto-generated catch block system.out.println(e); } 乌坦战争 return t; } public jtable bookin() { // todo auto-generated method stub string []head={"isbn","name","count"}; string [][]data=null; bmodel=new defaulttablemodel(data,head); jtable t=new jtable(bmodel); string driver="com.mysql.jdbc.driver"; string url="jdbc:mysql://localhost:3306/test"; string ur="root"; string password="123456"; connection conn=null; statement stmt=null; resultt rs=null; try { class.forname(driver); conn = drivermanager.getconnection(url, ur, password); stmt = conn.createstatement(); rs=stmt.executequery("lect * from book"); string[] row=new string[3]; while(rs.next()){ row[0]=rs.getstring(1); row[1]=rs.getstring(2); row[2]=rs.getstring(3); bmodel.addrow(row); bmodel.firetabledatachanged(); } rs.clo(); stmt.clo(); conn.clo();} catch (exception e) { // todo auto-generated catch block system.out.println(e); } return t; } public static void main(string[] args) { // todo auto-generated method stub labmsys n=new labmsys(); n.ttitle("图书借阅管理系统"); n.tsize(1000,500); n.tlocationrelativeto(null); n.tdefaultclooperation(jframe.exit_on_clos骆驼祥子读后感400字e); n.tvisible(true); }}
数据库:
---- table structure for table `book`--drop table if exists `book`;create table `book` ( `isbn` int(11学习的乐趣) not null, `name` varchar(50) not null, `count` int(11) default null, primary key (`isbn`)) engine=innodb default chart=utf8;---- dumping data for table `book`--lock tables `book` write;/*!40000 alter table `book` disable keys */;inrt into `book` values (662530,'c#',9),(662545,'python',12),(663520,'c++',6),(663548,'java',8);/*!40000 alter table `book` enable keys */;unlock tables;---- table structure for table `borrow`--drop table if exists `borrow`;create table `borrow` ( `id` int(11) not null auto_increment, `xh` int(11) not null, `isbn` int(11) not null, primary key (`id`)) engine=innodb default chart=utf8;---- dumping data for table `borrow`--lock tables `borrow` write;/*!40000 alter table `borrow` disable keys */;inrt into `borrow` values (1006,123456,662545),(1007,456789,663520),(1009,789456,662530),(1010,741852,662530);
运行结果显示:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持www.887551.com。
本文发布于:2023-04-06 03:29:47,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/12222f5e1767c9c7f59d507debd54a1d.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:java实现简单的图书借阅系统.doc
本文 PDF 下载地址:java实现简单的图书借阅系统.pdf
留言与评论(共有 0 条评论) |