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);

运行结果显示:

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

(0)

相关推荐

  • Java实现图书借阅系统

    今天这个是一个Java小练习,一个图书借阅系统,需要实现的功能有: 判断用户是否需要进行借书 在用户选择借书时,展示出图书列表 图书列表包含 图书序号.图书名称.借阅价格.作者 用户选择借书数量.并选择对应图书.借阅天数 计算出用户需支付金额 Book.java package com.imooc; /**  * 图书类 包含图书序号 名称 价格  * */ public class Book {     private int id;     private String name;    

  • Java实现图书馆借阅系统

    本文实例为大家分享了Java实现图书馆借阅系统的具体代码,供大家参考,具体内容如下 Main.java package com.src1.booksystem; import com.src1.booksystem.booklist.BookList; import com.src1.booksystem.users.AdminUser; import com.src1.booksystem.users.NormalUser; import com.src1.booksystem.users.U

  • Java实现简易图书借阅系统

    在简单学习Java的基础知识点后,动手做了一个十分简陋的图书馆借阅系统,作为对所学知识的综合应用,有不足的地方希望大家多多评论,会积极进行改正. 1.先附上总的效果 一开始的登录界面 登录界面 注册界面 登录进去后的个人主页 (本来想在上方插入一张图片,但是刚学swing部分,搞不懂图片的插入方式,搞了很久还是没懂,就暂时放下了) 借书页面 输入关键词后搜索的结果 还书界面,点击自动显示未还书籍 查询未还书籍的具体信息 2.贴上源代码 1).这里简单说一下与数据库的操作,注册用户时在表perso

  • Java实现简单图书借阅系统

    本文实例为大家分享了Java实现图书借阅系统的具体代码,供大家参考,具体内容如下 为图书阅览室开发一个图书借阅系统,最多可存50本图书,实现图书的管理.图书借阅系统具备以下主要功能. u功能 Ø借出排行榜 Ø新增图书 Ø查看图书 Ø删除图书 Ø借出图书 Ø归还图书 Ø退出 package com.daiinfo.seninorjava.ken8.implentment.utils; import java.text.SimpleDateFormat; import java.util.Calen

  • 基于Java SSM框架开发图书借阅系统源代码

    一.技术框架与开发环境 开发环境: IDE:IDEA 2020 数据库:MySQL 8.0 JDK 1.8 Maven 3.6.1 Tomcat 9 lombok 1.18.1 技术框架: 核心框架:Spring 5.1.9 持久层框架:Mybatis 3.5.2 视图层框架:SpringMVC 5.1.9 前端框架:Bootstrap 4 jquery-3.6.0 二.项目源码 有用就点赞博客 Github 国内:Giee 项目结构 三.功能介绍 1.登录 2.注册 AJAX异步刷新显示判断账

  • 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

  • Python连接Mysql实现图书借阅系统

    相信大家在学习python编程时绝对离不开数据库的连接,那么我们就用python来连接数据库实现一个简单的图书借阅系统.其实也很简单,就是在我们的程序中加入sql语句即可 数据库的表结构 我们在这里需要三张表,一张用户表,一张图书表和一张借阅表.注意我们的数据库命名为bbs(book borrow system) 1.用户表 2.图书表 bookname:书名author:作者booknum:图书编号bookpress:出版社bookamoun:图书数量 3.借阅表 id:借阅号borrowna

  • java实现简单的图书管理系统

    本文实例为大家分享了java实现简单的图书管理系统的具体代码,供大家参考,具体内容如下 一.项目分布 Book类: 定义了书的一些属性(书名,作者,价格,分类,状态)并且写了属性的get.set方法 Library类: 写了登录函数(1.普通用户,2.管理员,3.退出系统), Person类:定义的人的属性(姓名,性别,年龄)并且写了属性的get.set方法,定义了一些书. operate接口:操作的接口,操作方法:查询/查阅,删除/借阅,增加/还书,显示书籍列表 Root类:继承了Person

  • C语言图书借阅系统源码

    本文实例为大家分享了C语言图书借阅系统的具体代码,供大家参考,具体内容如下 #include "stdafx.h" #include"stdio.h" #include"conio.h" #include"string.h" #include"stdlib.h" int N; char mima[20]="mm"; /**********定义图书结构体类型book*******/ str

  • python实现图书借阅系统

    本文实例为大家分享了python实现图书借阅系统的具体代码,供大家参考,具体内容如下 部分代码: from flask import Flask,render_template from flask import request from DB import createdb from flask import session app = Flask(__name__) app.config['SECRET_KEY'] = '123456' # 首页-->登录页面 @app.route('/')

  • java实现简单的汽车租赁系统

    本文实例为大家分享了java实现简单的汽车租赁系统的具体代码,供大家参考,具体内容如下 欢迎进入xx汽车租赁公司请输入用户名请输入密码(用户名默认是名字缩写,密码是123,将登陆模块封装到方法中去调用方法)请输入您的操作1)查看现在车库中的所有车辆信息2)租赁汽车3)往车库中添加汽车4)修改汽车租赁价格信息 用switch去判断操作 类分析 代码: package com.youjiuye.bms; public class CRMS {     public static void main(

  • C语言数据结构之图书借阅系统

    本文实例为大家分享了C语言实现图书借阅系统的具体代码,供大家参考,具体内容如下 /*****************************   @title: 数据结构实验   @name: <实验2-1> 线性表(顺序表)的应用--我的图书馆   @object:       [实验目的]           应用线性表解决问题.           有若干图书,借出的书需要登记下来.            用两个线性表分别保存现有图书和借书信息,           并实现增加新书,借书

随机推荐