如何使用java实现简单的图书借阅系统
发表于:2025-11-08 作者:千家信息网编辑
千家信息网最后更新 2025年11月08日,这篇文章将为大家详细讲解有关如何使用java实现简单的图书借阅系统,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。具体内容如下直接看代码:package ttt;im
千家信息网最后更新 2025年11月08日如何使用java实现简单的图书借阅系统
这篇文章将为大家详细讲解有关如何使用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.ResultSet;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.setLayout(new BorderLayout()); this.add(j1,BorderLayout.WEST); this.add(j2,BorderLayout.EAST); this.add(j3,BorderLayout.CENTER); j1.setBorder(new TitledBorder("图书借书表")); j2.setBorder(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.getSelectedRow(); System.out.println(rowNum); if(rowNum>=0) { String isbn=booktable.getValueAt(rowNum, 0).toString(); System.out.println(isbn); int count=Integer.parseInt((String) booktable.getValueAt(rowNum, 2)); String s=JOptionPane.showInputDialog("请输入学号"); if(count<=0) JOptionPane.showMessageDialog(null,"图书库存不足"); else if (null==s) JOptionPane.showMessageDialog(null,"请输入学号"); else{ if(borrowBook(isbn,s)) { System.out.println("yes"); JOptionPane.showMessageDialog(null,"successful!"); } else JOptionPane.showMessageDialog(null,"Wrong!"); } }else JOptionPane.showMessageDialog(null,"Choose book!"); } }); //添加还书监听器 button2.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub int rowNum=borrowtable.getSelectedRow(); 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,"学号不正确"); else{ if(returnBook(isbn)) { JOptionPane.showMessageDialog(null,"successful!"); } else JOptionPane.showMessageDialog(null,"Wrong!"); } }else JOptionPane.showMessageDialog(null,"Choose book!"); } }); } public boolean borrowBook(String isbn, String s) { int xh = Integer.parseInt(s); int isbn2 =Integer.parseInt(isbn); System.out.println("学号:"+xh); boolean b=false; String driver="com.mysql.jdbc.Driver"; String url="jdbc:mysql://localhost:3306/test"; String user="root"; String password="123456"; Connection conn=null; Statement stmt=null; try { Class.forName(driver); conn = DriverManager.getConnection(url, user, password); stmt = conn.createStatement(); System.out.println("isbn:"+isbn); conn.setAutoCommit(false); String sql1 = "update book set count=count-1 where isbn='"+isbn2+"'"; String sql2 = "insert into borrow(xh,isbn) values('"+xh+"','"+isbn2+"')"; stmt.executeUpdate(sql1); stmt.executeUpdate(sql2); System.out.println("isbn2:"+isbn2); //stmt = (PreparedStatement) conn.prepareStatement("update book set count=count-1 where isbn=?"); conn.commit(); stmt.close(); conn.close(); b=true; } catch (Exception e) { try{ conn.rollback(); }catch(Exception e1){e1.printStackTrace();} } return b; } private boolean returnBook(String isbn) { int isbn2 =Integer.parseInt(isbn); boolean b=false; String driver="com.mysql.jdbc.Driver"; String url="jdbc:mysql://localhost:3306/test"; String user="root"; String password="123456"; Connection conn=null; Statement stmt=null; try { Class.forName(driver); conn = DriverManager.getConnection(url, user, password); stmt = conn.createStatement(); conn.setAutoCommit(false); stmt.executeUpdate("delete from borrow where isbn='"+isbn2+"'"); stmt.executeUpdate("update book set count=count+1 where isbn='"+isbn2+"'"); System.out.println("还书isbn2:"+isbn2); conn.commit(); stmt.close(); conn.close(); 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 user="root"; String password="123456"; Connection conn=null; Statement stmt=null; ResultSet rs=null; try { Class.forName(driver); conn = DriverManager.getConnection(url, user, password); stmt = conn.createStatement(); rs=stmt.executeQuery("select * 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.close(); stmt.close(); conn.close();} 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 user="root"; String password="123456"; Connection conn=null; Statement stmt=null; ResultSet rs=null; try { Class.forName(driver); conn = DriverManager.getConnection(url, user, password); stmt = conn.createStatement(); rs=stmt.executeQuery("select * 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.close(); stmt.close(); conn.close();} 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.setTitle("图书借阅管理系统"); n.setSize(1000,500); n.setLocationRelativeTo(null); n.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); n.setVisible(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 CHARSET=utf8;---- Dumping data for table `book`--LOCK TABLES `book` WRITE;/*!40000 ALTER TABLE `book` DISABLE KEYS */;INSERT 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 CHARSET=utf8;---- Dumping data for table `borrow`--LOCK TABLES `borrow` WRITE;/*!40000 ALTER TABLE `borrow` DISABLE KEYS */;INSERT INTO `borrow` VALUES (1006,123456,662545),(1007,456789,663520),(1009,789456,662530),(1010,741852,662530);
运行结果显示:

关于"如何使用java实现简单的图书借阅系统"这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。
图书
学号
系统
借阅
篇文章
内容
更多
输入
不错
实用
代码
库存
数据
数据库
文章
监听器
知识
管理系统
结果
参考
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
广州互联网平安科技有限公司
武装突袭2服务器怎么设置
数据库2008无法连接
数据库ma
阿里巴巴网络技术有
海康威视ip广播服务器
mc空岛服务器如何给人权限
学软件开发可以自己开店吗
军人如何提高网络安全意识
中国商品诚信数据库是真的吗
奉贤区上门软件开发电话多少
移动网络安全工作
ibm刀片式服务器价格
java 数据库验证密码
服务器上的文件
王牌战争如何进服务器
网络安全我先行的征文
php取服务器当前时间
甘肃科技产业互联网
搭建个人服务器要申报吗
全球最新数据库技术论文
网络安全最好的语言
软件开发难题及策略
新买的服务器怎么连接电脑
四川盛恒鑫网络技术
甘肃大数据卫星授时服务器
服务器日志系统的设计与实现
哪里有企业内网网络安全
引路人网络安全团队
网络安全关机