<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
本文範例為大家分享了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);
執行結果顯示:
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援it145.com。
相關文章
<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
综合看Anker超能充系列的性价比很高,并且与不仅和iPhone12/苹果<em>Mac</em>Book很配,而且适合多设备充电需求的日常使用或差旅场景,不管是安卓还是Switch同样也能用得上它,希望这次分享能给准备购入充电器的小伙伴们有所
2021-06-01 09:31:42
除了L4WUDU与吴亦凡已经多次共事,成为了明面上的厂牌成员,吴亦凡还曾带领20XXCLUB全队参加2020年的一场音乐节,这也是20XXCLUB首次全员合照,王嗣尧Turbo、陈彦希Regi、<em>Mac</em> Ova Seas、林渝植等人全部出场。然而让
2021-06-01 09:31:34
目前应用IPFS的机构:1 谷歌<em>浏览器</em>支持IPFS分布式协议 2 万维网 (历史档案博物馆)数据库 3 火狐<em>浏览器</em>支持 IPFS分布式协议 4 EOS 等数字货币数据存储 5 美国国会图书馆,历史资料永久保存在 IPFS 6 加
2021-06-01 09:31:24
开拓者的车机是兼容苹果和<em>安卓</em>,虽然我不怎么用,但确实兼顾了我家人的很多需求:副驾的门板还配有解锁开关,有的时候老婆开车,下车的时候偶尔会忘记解锁,我在副驾驶可以自己开门:第二排设计很好,不仅配置了一个很大的
2021-06-01 09:30:48
不仅是<em>安卓</em>手机,苹果手机的降价力度也是前所未有了,iPhone12也“跳水价”了,发布价是6799元,如今已经跌至5308元,降价幅度超过1400元,最新定价确认了。iPhone12是苹果首款5G手机,同时也是全球首款5nm芯片的智能机,它
2021-06-01 09:30:45