基于java的图书借阅管理系统,用java实现一个简单的图书管理系统
本文实例为大家分享了爪哇岛实现简单图书借阅系统的具体代码,供大家参考,具体内容如下
直接看代码:
包培训培训师导入Java。awt。borderlayout导入Java。awt。事件。动作事件;导入Java。awt。事件。动作监听器;导入Java。SQL。连接;导入Java。SQL。司机经理;导入Java。SQL。结果集;导入Java。SQL。声明;导入javax。挥棒。jbutton导入javax。挥棒。jframe导入javax。挥棒。joptionpane导入javax。挥棒。jpanel导入javax。挥棒。jscrollpane导入javax。挥棒。jtable导入javax。挥棒。边框。标题边框;导入javax。挥棒。桌子。defaulttablemodel公共类Labmsys扩展JFrame { JTable booktable=null静态DefaultTableModel图书型号=nullJTable borrowtable=null静态DefaultTableModel b model=nullJPanel J1=new JPanel();JPanel J2=new JPanel();JPanelJBOY3乐队=new JPanel();JButton按钮1=新的JButton(借书);JButton按钮2=新的JButton(还书);公共Labmsys(){ this。set layout(new BorderLayout());this.add(j1,BorderLayout .西);this.add(j2,BorderLayout .东);this.add(j3,BorderLayout .中心);j1.setBorder(new TitledBorder(图书借书表));j2.setBorder(new TitledBorder(图书库存表));这个。book table=bookIn();这个。借表=borrowIn();JScrollPane JSP 1=new JScrollPane(借表);JScrollPane JSP 2=new JScrollPane(图书表);J1。添加(JSP 1);J2。添加(JSP 2);JBOY3乐队。添加(按钮1);JBOY3乐队。添加(按钮2);//添加借书监听器按钮1。addactionlistener(new action listener(){ @ Override public void action performed(action event e){//TODO自动生成的方法stub int rowNum=book表。getselectedrow();系统。出去。println(rowNum);if(rowNum=0){ String ISBN=book table。在(rowNum,0)处获取值.toString();系统。出去。println(国际标准书号);int count=整数。解析int((String)book表。getvalue at(rowNum,2));string s=joptionpane。显示输入对话框(请输入学号);if(count=0)joptionpane。showmessagedialog(空,图书库存不足);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);
运行结果显示:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持盛行IT。
郑重声明:本文由网友发布,不代表盛行IT的观点,版权归原作者所有,仅为传播更多信息之目的,如有侵权请联系,我们将第一时间修改或删除,多谢。