问题描述
编写一个简单的图书管理子系统。图书馆中需要存储书名,编号,作者、出版社,图书类型、存放位置、同名图书的存放位置等信息。需要存储对应的学生或教师的基本信息,可以用学号和工号来借阅图书。
基本要求
程序应提供的基本管理功能有:
1)添加:即增加同名书的记录到图书馆。
2)修改:修改图书除编号以外的信息。
3)删除:删除已经过时的图书。
4)查询:可根据书名查找图书的相关信息,若找到显示其基本信息。
6)借阅:可完成教师或学生借阅图书,一次可以借多本书,并可存储借阅信息。
7)借阅信息查询:通过学号或工号可以查询借阅情况
7)归还:可完成教师或学生归还图书,一次可以还多本书,并可存储归还信息
8)借阅信息查询:通过学号或工号可以查询归还信息情况
9)排序:可以根据条目的某个项对所有条目进行排序,如姓名,或出版社等

连接MySQL数据库的类

package com.bit.Database;import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;/*** @className Mysql* @Description TODO* @Author zhangqianqian* @Date 2020/1/8 15:00* @Version 1.0**/
public class Mysql {private final static String URL = "jdbc:mysql://localhost/library";private final static String USER = "root";private final static String PASSWORD = "12345";public static DataSource getDataSource() {MysqlDataSource dataSource = new MysqlDataSource();dataSource.setURL(URL);dataSource.setUser(USER);dataSource.setPassword(PASSWORD);return dataSource;}public static int judge(int no) {//判断是否是用户Connection connection = null;PreparedStatement pstmt = null;ResultSet rs = null;try {connection = getDataSource().getConnection();String sql = "select *from user where no=?";pstmt = connection.prepareStatement(sql);pstmt.setInt(1, no);rs = pstmt.executeQuery();} catch (SQLException e) {e.printStackTrace();} finally {try {if (pstmt != null) {pstmt.close();}if (connection != null) {connection.close();}} catch (SQLException e) {e.printStackTrace();}}if(rs!=null)return 1;return 0;}}

接口,所有功能类实现的接口

public interface IOperation {Scanner scanner =new Scanner(System.in);public void work();
}

添加图书类

public class AddBook extends Mysql implements IOperation{//添加图书public void work() {System.out.println("添加书籍");System.out.println("请输入图书的名字:");String name=scanner.next();System.out.println("请输入图书的编号");int bno=scanner.nextInt();System.out.println("请输入图书的作者");String author=scanner.next();System.out.println("请输入图书的出版社");String publish=scanner.next();System.out.println("请输入图书的类型");String type=scanner.next();System.out.println("请输入图书的位置");String location=scanner.next();System.out.println("请输入同名图书的位置");String namelocation=scanner.next();Book book=new Book(name,bno,author,publish,type,location,namelocation);Connection connection = null;PreparedStatement pstmt=null;try{connection = getDataSource().getConnection();String sql = "insert into book values(?,?,?,?,?,?,?,?)";pstmt = connection.prepareStatement(sql);pstmt.setString(1, book.getName());pstmt.setInt(2, book.getBno());pstmt.setString(3, book.getAuthor());pstmt.setString(4, book.getPublish());pstmt.setString(5, book.getType());pstmt.setString(6, book.getLocation());pstmt.setString(7, book.getNameLocation());pstmt.setBoolean(8, book.isBorrowed());pstmt.execute();} catch(SQLException e) {e.printStackTrace();} finally {try {if (pstmt != null) {pstmt.close();}if (connection != null) {connection.close();}} catch (SQLException e) {e.printStackTrace();}}System.out.println("添加成功");}
}

借阅图书信息类

public class BorrowMessage extends Mysql implements IOperation {@Overridepublic void work() {System.out.println("借阅书籍信息查询");System.out.println("请输入要查询借阅人的编号");int id=scanner.nextInt();Connection connection = null;PreparedStatement pstmt=null;ResultSet rs=null;Message m=new Message();try {connection=getDataSource().getConnection();String sql="select *from borrow,book where borrow.bno=book.bno and no=?";pstmt=connection.prepareStatement(sql);pstmt.setInt(1, id);rs=pstmt.executeQuery();while(rs.next()){m.setNo(rs.getInt("no"));m.setName(rs.getString("name"));m.setBno(rs.getInt("bno"));m.setBorrowTime(rs.getString("borrowTime"));m.setReturnTime(rs.getString("returnTime"));System.out.println(m);}} catch (SQLException e) {e.printStackTrace();} finally {try {if (pstmt != null) {pstmt.close();}if (connection != null) {connection.close();}} catch (SQLException e) {e.printStackTrace();}}}
}

借阅图书类

public class BrrowBook extends Mysql implements IOperation {//借阅图书@Overridepublic void work() {System.out.println("借阅书籍");System.out.println("请输入要借阅书籍的编号");int bno=scanner.nextInt();System.out.println("请输入您的id");int id=scanner.nextInt();System.out.println("请输入您的借阅时间");String borrowTime=scanner.next();System.out.println("请输入您的还书时间");String returnTime=scanner.next();Connection connection = null;PreparedStatement pstmt=null;ResultSet rs=null;Borrow book=new Borrow(id,bno,borrowTime,returnTime);try {connection=getDataSource().getConnection();String sql1="select isBorrowed from book where bno=?";pstmt=connection.prepareStatement(sql1);pstmt.setInt(1,bno);rs=pstmt.executeQuery();boolean flag=false;if(rs.next()){flag=rs.getBoolean("isBorrowed");}if(!flag){String sql2 = "update Book set isBorrowed=true where bno=?";pstmt = connection.prepareStatement(sql2);pstmt.setInt(1, bno);pstmt.execute();String sql3 = "insert into  borrow values(?,?,?,?)";pstmt = connection.prepareStatement(sql3);pstmt.setInt(1, book.getBno());pstmt.setInt(2, book.getNo());pstmt.setString(3, book.getBorrowTime());pstmt.setString(4, book.getReturnTime());pstmt.execute();System.out.println("借阅成功!");}else {System.out.println("该书已被借出");}} catch (SQLException e) {e.printStackTrace();} finally {try {if (pstmt != null){pstmt.close();}if (connection != null){connection.close();}} catch (SQLException e) {e.printStackTrace();}}}
}

删除图书类

public class DelBook extends Mysql implements IOperation {//删除图书@Overridepublic void work() {System.out.println("删除书籍");System.out.println("请输入要删除图书的名字:");String name=scanner.next();Connection connection = null;PreparedStatement pstmt=null;try{connection = getDataSource().getConnection();String sql = "delete from book where name=?";pstmt = connection.prepareStatement(sql);pstmt.setString(1, name);pstmt.execute();} catch(SQLException e) {e.printStackTrace();} finally {try {if (pstmt != null) {pstmt.close();}if (connection != null) {connection.close();}} catch (SQLException e) {e.printStackTrace();}}System.out.println("删除成功");    }
}

显示所有图书类

public class DisplayBook extends Mysql implements IOperation{//显示所有图书@Overridepublic void work() {Connection connection = null;PreparedStatement pstmt=null;ResultSet rs=null;Book book=new Book();try {connection=getDataSource().getConnection();String sql="select *from book";pstmt=connection.prepareStatement(sql);rs=pstmt.executeQuery();while(rs.next()){book.setName(rs.getString("name"));book.setBno(rs.getInt("bno"));book.setAuthor(rs.getString("author"));book.setPublish(rs.getString("publish"));book.setType(rs.getString("type"));book.setLocation(rs.getString("location"));book.setNameLocation(rs.getString("nameLocation"));book.setBorrowed(rs.getBoolean("isBorrowed"));System.out.println(book);}} catch (SQLException e) {e.printStackTrace();} finally {try {if (pstmt != null) {pstmt.close();}if (connection != null) {connection.close();}} catch (SQLException e) {e.printStackTrace();}}}
}

退出程序

public class Exit implements IOperation{public void work() {System.out.println("byebye!");System.exit(0);}
}

查找书籍类

public class FindBook extends Mysql implements IOperation{//查找图书@Overridepublic void work() {System.out.println("查找书籍");System.out.println("请输入要查找图书的编号:");int bno=scanner.nextInt();Book book=new Book();Connection connection = null;PreparedStatement pstmt=null;ResultSet rs=null;try{connection = getDataSource().getConnection();String sql = "select *from book where bno=?";pstmt = connection.prepareStatement(sql);pstmt.setInt(1, bno);rs=pstmt.executeQuery();while(rs.next()){book.setName(rs.getString("name"));book.setBno(rs.getInt("bno"));book.setAuthor(rs.getString("author"));book.setPublish(rs.getString("publish"));book.setType(rs.getString("type"));book.setLocation(rs.getString("location"));book.setNameLocation(rs.getString("nameLocation"));book.setBorrowed(rs.getBoolean("isBorrowed"));}} catch(SQLException e) {e.printStackTrace();} finally {try {if (pstmt != null) {pstmt.close();}if (connection != null) {connection.close();}} catch (SQLException e) {e.printStackTrace();}}if(book!=null){System.out.println(book);System.out.println("查找成功");}elseSystem.out.println("没有此书籍");}
}

还书类

public class ReturnBook extends Mysql implements IOperation {@Overridepublic void work() {System.out.println("归还书籍");System.out.println("请输入要归还书籍的编号");int bno=scanner.nextInt();Connection connection = null;PreparedStatement pstmt=null;try {connection=getDataSource().getConnection();String sql="update Book set isBorrowed=false where bno=?";pstmt=connection.prepareStatement(sql);pstmt.setInt(1,bno);pstmt.execute();} catch (SQLException e) {e.printStackTrace();} finally {try {if (pstmt != null){pstmt.close();}if (connection != null){connection.close();}} catch (SQLException e) {e.printStackTrace();}}System.out.println("归还成功!");}
}

排序类

public class SortBook extends Mysql implements IOperation {@Overridepublic void work() {Connection connection = null;PreparedStatement pstmt=null;Book book=new Book();ResultSet rs=null;int count=0;try {connection=getDataSource().getConnection();String sql="select * from book order by  bno desc";pstmt=connection.prepareStatement(sql);rs=pstmt.executeQuery();while(rs.next()){book.setName(rs.getString("name"));book.setBno(rs.getInt("bno"));book.setAuthor(rs.getString("author"));book.setPublish(rs.getString("publish"));book.setType(rs.getString("type"));book.setLocation(rs.getString("location"));book.setNameLocation(rs.getString("nameLocation"));book.setBorrowed(rs.getBoolean("isBorrowed"));System.out.println(book);}} catch (SQLException e) {e.printStackTrace();} finally {try {if (pstmt != null){pstmt.close();}if (connection != null){connection.close();}} catch (SQLException e) {e.printStackTrace();}}System.out.println("排序成功!");}
}

修改图书信息类

public class UpdataBook extends Mysql implements IOperation {@Overridepublic void work() {System.out.println("修改书籍");System.out.println("请输入要修改书籍的编号");int bno=scanner.nextInt();Connection connection = null;PreparedStatement pstmt=null;try {connection=getDataSource().getConnection();String sql="update Book set name=?,author=?,publish=?,type=?,location=? ,nameLocation=? where bno=?";pstmt=connection.prepareStatement(sql);System.out.println("请输入要修改的书籍的名称:");String name=scanner.next();pstmt.setString(1,name);System.out.println("请输入要修改的书籍的作者:");String author=scanner.next();pstmt.setString(2,author);System.out.println("请输入要修改的书籍的出版社:");String publish=scanner.next();pstmt.setString(3,publish);System.out.println("请输入要修改的书籍的类型:");String type=scanner.next();pstmt.setString(4,type);System.out.println("请输入要修改的书籍的位置:");String location=scanner.next();pstmt.setString(5,location);System.out.println("请输入要修改的书籍的同名图书位置:");String nameLocation=scanner.next();pstmt.setString(6,nameLocation);pstmt.setInt(7,bno);pstmt.execute();} catch (SQLException e) {e.printStackTrace();} finally {try {if (pstmt != null){pstmt.close();}if (connection != null){connection.close();}} catch (SQLException e) {e.printStackTrace();}}System.out.println("修改成功!");}
}

完整代码见:https://github.com/zqq234/test/commit/80a64e7a24eef2a19af33596b272e834fe8a5472

图书馆管理系统(连接数据库)相关推荐

  1. 基于C#+Oracle的模拟图书馆管理系统

    上学期的大作业,以此为记. 基于C#+Oracle的模拟图书馆管理系统 目录 基于C#+Oracle的模拟图书馆管理系统 需要实现的功能 数据库设计 前端设计 需要实现的功能 1.学生登录/注册 2. ...

  2. python图书馆管理系统实验报告_基于python图书馆管理系统设计实例详解

    写完这个项目后,导师说这个你完全可以当作毕业项目使用了,写的很全,很多的都设计考虑周全,但我的脚步绝不止于现在,我想要的是星辰大海!与君共勉! 这个项目不是我的作业, 只是无意中被拉进来了,然后就承担 ...

  3. 图书馆管理系统中遇见的问题与问题的解决方法思路

    图书馆管理结束已经过去半个月了,现在的我是时候到了那个总结自己在图书馆管理系统中遇见的各种问题的时候了. 首先,我来说一下我的图书馆管理系统的样子(结构),我的图书馆管理系统是有7个页面的,分别是 用 ...

  4. php图书馆管理系统的设计与实现毕业设计-附源码

    摘 要 大数据时代下,数据呈爆炸式地增长.为了迎合信息化时代的潮流和信息化安全的要求,利用互联网服务于其他行业,促进生产,已经是成为一种势不可挡的趋势.在图书馆的要求下,开发一款整体式结构的图书馆管理 ...

  5. Java实现简单图书馆管理系统

    编写图书馆管理系统 源码https://github.com/Hu1Wence/Learn_Java/tree/master/BookManage 思路 要编写这个程序我们首相要清楚这个程序中有哪些对 ...

  6. 图书馆管理系统——vs2017\sqlserver\c#

    基于vs2017的图书馆管理系统 图书馆管理系统 基于vs2017的图书馆管理系统 1. 用到的工具:vs2017 .sqlserver 2. 介绍数据库表: 2.1 首先打开数据库:sqlsrerv ...

  7. ThinkPHP+基于ThinkPHP的图书馆管理系统 毕业设计-附源码311833

    图书馆管理系统的设计与实现 摘 要 大数据时代下,数据呈爆炸式地增长.为了迎合信息化时代的潮流和信息化安全的要求,利用互联网服务于其他行业,促进生产,已经是成为一种势不可挡的趋势.在图书馆的要求下,开 ...

  8. 基于SSM实现的图书馆管理系统

    项目类别: BS-XX-075 运行环境: 开发工具:IDEA / ECLIPSE 数据库:MYSQL5.7 应用服务器:TOMCAT8.5.31 JDK: 1.8 开发技术:Spring+Sprin ...

  9. 20190318-使用类做一个简单的图书馆管理系统

    要求:使用类的形式做一个图书馆管理系统,实现借书,入库,还书,查书等功能. 设计思路: 第一步:先写一个书的类,来存储图书馆中最重要的组成部分书的信息管理,包括书名,书作者,书的所属类别,书的价格等 ...

  10. 大一java图书馆管理系统课程设计

    大一java图书馆管理系统课程设计 代码仅供参考!!!仅供参考!!! 效果图如下 管理员用户名:123  密码123 密码错误的话进入项目resources文件夹有个LMS数据库,使用数据库客户端打开 ...

最新文章

  1. 给大家推荐8个SpringBoot精选项目
  2. JavaScript实现permutate Without Repetitions无重复排列算法(附完整源码)
  3. [你必须知道的.NET]第十九回:对象创建始末(下)
  4. mysql 11.2.16_Navicat for MySQL 11.2
  5. Fuel 9.0安装Openstack由于NTP检查没通过导致失败--解决办法
  6. VS+Qt应用开发-设置鼠标光标
  7. 图像处理之LOMO特效
  8. Android Button按钮周围添加图片
  9. AI还原乾隆后妃样貌,延禧攻略众生相。
  10. 八. geotrellis使用 矢量数据栅格化
  11. 编写训练一年级学生10以内减法的程序
  12. Appium 按压元素进行滑动
  13. 网易云发送验证码短信,发送通知短信,java版
  14. 音频怎么转文字?学会这3招,轻松拉满你的工作效率
  15. H3Cmsr830 l2tp二层隧道协议配置详解(host-R版)
  16. 面试java项目中解决了什么问题,附源代码
  17. arcgis android 指南针,BaiduMap SDK-地图显示指南针(左上角)
  18. 敏捷开发中如何写好用户故事?
  19. 1253:Dungeon Master
  20. [物联网篇 ] 15 -博通AP6255模块中WL_HOST_WAKE功能

热门文章

  1. JMeter之BeanShell的变量使用方法
  2. c语言杭电oj1090答案,杭电OJ水题答案.doc
  3. ie11加载项启用不了 java,IE11网页加载项和控件不能运行怎么办
  4. 五位数电话号码以及中国各市区号
  5. WDM驱动inf模板
  6. 集成电路经典资料分享
  7. 倒立摆系统的多种控制器设计
  8. 按月分隔时间段,java实现,DateRange
  9. guzzle 封装api_Wuzzlist API与Guzzle的速成课程
  10. python唯美壁纸_Python爬虫教程-爬取5K分辨率超清唯美壁纸源码