当前位置: 首页 > news >正文

图书管理系统(JDBC)

AdminUser是管理员类

NormalUser是用户类

AddOperation是增加图书类

BorrowOperation是借书类

DelOperation是删除图书类

ExitOperation是退出类

FindOperation是查找图书类

IOPeration是接口

ReturnOperation是还书类

ShowOperation是显示所有图书类

注意:我的数据库有个cnm数据库,如果你没有的话,需要先创建一个cnm数据库

sql建表语句:

drop table if exists book;
create table book
(	id			   	 bigint  auto_increment,name              varchar(255), author            varchar(255),price             int,type              varchar(255),isBorrowed        varchar(255),primary key (id)
);insert book(name,author,price,type,isBorrowed) values('西游记','吴承恩',35,'小说','未借阅');
insert book(name,author,price,type,isBorrowed) values('活着','余华',40,'文学','未借阅');
commit;
select * from book;

Book类

package book;public class Book {private String name;//书名private String author;//作者private int price;//价格private String type;//类型private boolean isBorrowed;//是否被借出,初始值是false,在构造方法中不用写public String getName() {return name;}public void setName(String name) {this.name = name;}public String getAuthor() {return author;}public void setAuthor(String author) {this.author = author;}public int getPrice() {return price;}public void setPrice(int price) {this.price = price;}public String getType() {return type;}public void setType(String type) {this.type = type;}public boolean isBorrowed() {return isBorrowed;}public void setBorrowed(boolean borrowed) {isBorrowed = borrowed;}public Book(String name, String author, int price, String type) {this.name = name;this.author = author;this.price = price;this.type = type;}@Overridepublic String toString() {return "Book{" +"name:" + name +",  author:" + author +",  price:" + price +",  type=:" + type +",  isBorrowed:" +(isBorrowed==true?"  已借出":"  未借出") +'}';}
}

BookList类

package book;public class BookList {private static final int DEFAULT_SIZE=10;//该常量定义一个书架有多少本书private  Book[] books=new  Book[DEFAULT_SIZE];private int usedSize;//记录下当前book数组中有几本书public int getUsedSize() {return usedSize;}public void setUsedSize(int usedSize) {this.usedSize = usedSize;}}

User类

package user;import book.BookList;
import opera.IOPeration;public abstract class User {//抽象类protected String name;//名字.这边的protect代表的是名字的权限。如果是private,它只能在同一个包的同一类使用。就不能让AdminUser类继承了。写public的话//权限太大了,不是很好。protected IOPeration[] ioPerations;public User(String name) {//构造方法this.name = name;}public abstract int menu();//抽象方法,打印菜单,因为有了choice返回值int类型,所以void改成intpublic void doWork(int choice, BookList bookList){//通过选择的操作,去选择执行数组下的哪个操作this.ioPerations[choice].work(bookList);}
}

AdminUser类

package user;import opera.*;import java.util.Scanner;public class AdminUser extends User{public AdminUser(String name) {super(name);this.ioPerations=new IOPeration[]{new ExitOperation(),new FindOperation(),new AddOperation(),new DelOperation(),new ShowOperation()};System.out.println("欢迎管理员:"+name);}@Overridepublic int menu() {//因为返回值choice是int类型的System.out.println("____________________________________");System.out.println("1.查找图书");System.out.println("2.新增图书");System.out.println("3.删除图书");System.out.println("4.显示图书");System.out.println("0.退出系统");System.out.println("请选择你需要的功能:");Scanner scanner=new Scanner(System.in);int choice=scanner.nextInt();return choice;}}

NormalUser类

package user;import opera.*;import java.util.Scanner;public class NormalUser extends User {public NormalUser(String name) {super(name);this.ioPerations = new IOPeration[]{//引用,这边用super也可以,因为这里没有同名的,不需要做区分。用this最好new ExitOperation(),new FindOperation(),new BorrowOperation(),new ReturnOperation()};System.out.println("欢迎用户:"+name);}@Overridepublic int menu() {System.out.println("_________________");System.out.println("1.查找图书!");System.out.println("2.借阅图书!");System.out.println("3.归还图书!");System.out.println("0.退出系统!");Scanner scanner = new Scanner(System.in);int choice = scanner.nextInt();return choice;}}

IOPeration接口

package opera;import book.BookList;public interface IOPeration {//创建接口void work(BookList bookList);//抽象方法//功能主要是针对图书的,也就是针对书架。
}

AddOperation类

package opera;import book.Book;
import book.BookList;import java.sql.*;
import java.util.Scanner;public class AddOperation implements IOPeration {public void work(BookList bookList) {System.out.println("新增图书!");Scanner scanner = new Scanner(System.in);System.out.println("请输入新增图书名字:");String name = scanner.nextLine();System.out.println("请输入新增图书作者:");String author = scanner.nextLine();System.out.println("请输入价格");int price = scanner.nextInt();//吸收回车符号scanner.nextLine();System.out.println("请输入图书类型:");String type = scanner.nextLine();//添加到数据库//定义3个变量Connection conn = null;Statement stmt = null;ResultSet rs = null;try {//1.注册驱动Class.forName("com.mysql.cj.jdbc.Driver");//2.获取连接conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/cnm", "root", "123456");//3.获取数据库操作对象(执行sql语句的对象)stmt = conn.createStatement();//4.执行sql语句String sql = "select * from book where name='"+name+"'";rs = stmt.executeQuery(sql);if(!rs.next()) {String s = "insert into book avalues('"+name+"','"+author+"','"+price+"','"+type+"','未借阅')";int x= stmt.executeUpdate(s);if(x>0)System.out.println("新增图书成功!");elseSystem.out.println("新增图书失败");}else{System.out.println("已经有这本书了");}}catch (Exception e) {e.printStackTrace();} finally {//6.释放资源if (rs != null) {try {rs.close();} catch (SQLException e) {e.printStackTrace();}}if (stmt != null) {try {stmt.close();} catch (SQLException e) {e.printStackTrace();}}if (conn != null) {try {conn.close();} catch (SQLException e) {e.printStackTrace();}}}}}

DelOperation类

package opera;import book.Book;
import book.BookList;import java.sql.*;
import java.util.Scanner;public class DelOperation implements IOPeration{@Overridepublic void work(BookList bookList) {System.out.println("删除图书!");System.out.println("请输入要删除图书的名称");Scanner scanner=new Scanner(System.in);String name=scanner.nextLine();//添加到数据库//定义3个变量Connection conn = null;Statement stmt = null;ResultSet rs = null;try {//1.注册驱动Class.forName("com.mysql.cj.jdbc.Driver");//2.获取连接conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/cnm", "root", "123456");//3.获取数据库操作对象(执行sql语句的对象)stmt = conn.createStatement();//4.执行sql语句String sql = "select * from book where name='"+name+"'";rs = stmt.executeQuery(sql);if(rs.next()) {String s = "delete from book where name='"+name+"'";int x = stmt.executeUpdate(s);if(x>0)System.out.println("删除成功!");elseSystem.out.println("删除失败!");}else{System.out.println("没有这本书");}}catch (Exception e) {e.printStackTrace();} finally {//6.释放资源if (rs != null) {try {rs.close();} catch (SQLException e) {e.printStackTrace();}}if (stmt != null) {try {stmt.close();} catch (SQLException e) {e.printStackTrace();}}if (conn != null) {try {conn.close();} catch (SQLException e) {e.printStackTrace();}}}}}

FindOperation类

package opera;import book.Book;
import book.BookList;import java.sql.*;
import java.util.Scanner;public class FindOperation implements IOPeration{//继承@Overridepublic void work(BookList bookList) {//重写IOPeration类中的work方法System.out.println("查找图书!");System.out.println("请输入要查找的图书名字");Scanner scanner=new Scanner(System.in);String name=scanner.nextLine();//添加到数据库//定义3个变量Connection conn = null;Statement stmt = null;ResultSet rs = null;try {//1.注册驱动Class.forName("com.mysql.cj.jdbc.Driver");//2.获取连接conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/cnm", "root", "123456");//3.获取数据库操作对象(执行sql语句的对象)stmt = conn.createStatement();//4.执行sql语句String sql = "select * from book where name='"+name+"'";rs = stmt.executeQuery(sql);if(rs.next()) {while (true) {String s1 = rs.getString("name");String s2 = rs.getString("author");int s3 = rs.getInt("price");String s4 = rs.getString("type");String s5 = rs.getString("isBorrowed");System.out.print("name: " + s1);System.out.print(",author:" + s2);System.out.print(",price:" + s3);System.out.print(",type:" + s4);System.out.print(",isBorrowed:" + s5);System.out.println();if(!rs.next())break;}}else{System.out.println("没有这本书");}}catch (Exception e) {e.printStackTrace();} finally {//6.释放资源if (rs != null) {try {rs.close();} catch (SQLException e) {e.printStackTrace();}}if (stmt != null) {try {stmt.close();} catch (SQLException e) {e.printStackTrace();}}if (conn != null) {try {conn.close();} catch (SQLException e) {e.printStackTrace();}}}}}

ShowOperation类

package opera;import book.BookList;import java.sql.*;public class ShowOperation implements IOPeration{@Overridepublic void work(BookList bookList) {//添加到数据库//定义3个变量Connection conn = null;Statement stmt = null;ResultSet rs = null;try {//1.注册驱动Class.forName("com.mysql.cj.jdbc.Driver");//2.获取连接conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/cnm", "root", "123456");//3.获取数据库操作对象(执行sql语句的对象)stmt = conn.createStatement();//4.执行sql语句String sql = "select * from book ";rs = stmt.executeQuery(sql);if(rs.next()) {while (true) {String s1 = rs.getString("name");String s2 = rs.getString("author");int s3 = rs.getInt("price");String s4 = rs.getString("type");String s5 = rs.getString("isBorrowed");System.out.print("name: " + s1);System.out.print(",author:" + s2);System.out.print(",price:" + s3);System.out.print(",type:" + s4);System.out.print(",isBorrowed:" + s5);System.out.println();if(!rs.next())break;}}else{System.out.println("没有这本书");}}catch (Exception e) {e.printStackTrace();} finally {//6.释放资源if (rs != null) {try {rs.close();} catch (SQLException e) {e.printStackTrace();}}if (stmt != null) {try {stmt.close();} catch (SQLException e) {e.printStackTrace();}}if (conn != null) {try {conn.close();} catch (SQLException e) {e.printStackTrace();}}}}
}

BorrowOperation类

package opera;import book.Book;
import book.BookList;import java.sql.*;
import java.util.Scanner;public class BorrowOperation implements IOPeration{@Overridepublic void work(BookList bookList) {System.out.println("借阅图书!");System.out.println("请输入要借阅的图书名字:");Scanner scanner=new Scanner(System.in);String name=scanner.nextLine();//添加到数据库//定义3个变量Connection conn = null;Statement stmt = null;ResultSet rs = null;try {//1.注册驱动Class.forName("com.mysql.cj.jdbc.Driver");//2.获取连接conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/cnm", "root", "123456");//3.获取数据库操作对象(执行sql语句的对象)stmt = conn.createStatement();//4.执行sql语句String sql = "select * from book where name='"+name+"'";rs = stmt.executeQuery(sql);if(rs.next()) {String s1 = "select isBorrowed from book where name='"+name+"'";rs = stmt.executeQuery(s1);if(rs.next()){String str = rs.getString("isBorrowed");if(str.equals("未借阅")) {String s2 = "update book set isBorrowed='已借阅' where name='" + name + "'";int x = stmt.executeUpdate(s2);if (x > 0)System.out.println("借阅成功!");elseSystem.out.println("借阅失败");}}}else{System.out.println("没有这本书");}}catch (Exception e) {e.printStackTrace();} finally {//6.释放资源if (rs != null) {try {rs.close();} catch (SQLException e) {e.printStackTrace();}}if (stmt != null) {try {stmt.close();} catch (SQLException e) {e.printStackTrace();}}if (conn != null) {try {conn.close();} catch (SQLException e) {e.printStackTrace();}}}}
}

ReturnOperation类

package opera;import book.Book;
import book.BookList;import java.sql.*;
import java.util.Scanner;public class ReturnOperation implements IOPeration{@Overridepublic void work(BookList bookList) {System.out.println("归还图书!");System.out.println("请输入要归还的图书名字:");Scanner scanner=new Scanner(System.in);String name=scanner.nextLine();//添加到数据库//定义3个变量Connection conn = null;Statement stmt = null;ResultSet rs = null;try {//1.注册驱动Class.forName("com.mysql.cj.jdbc.Driver");//2.获取连接conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/cnm", "root", "123456");//3.获取数据库操作对象(执行sql语句的对象)stmt = conn.createStatement();//4.执行sql语句String sql = "select * from book where name='"+name+"'";rs = stmt.executeQuery(sql);if(rs.next()) {String s1 = "select isBorrowed from book where name='"+name+"'";rs = stmt.executeQuery(s1);if(rs.next()){String str = rs.getString("isBorrowed");if(str.equals("已借阅")) {String s2 = "update book set isBorrowed='未借阅' where name='" + name + "'";int x = stmt.executeUpdate(s2);if (x > 0)System.out.println("归还成功!");elseSystem.out.println("归还失败");}}}else{System.out.println("没有这本书");}}catch (Exception e) {e.printStackTrace();} finally {//6.释放资源if (rs != null) {try {rs.close();} catch (SQLException e) {e.printStackTrace();}}if (stmt != null) {try {stmt.close();} catch (SQLException e) {e.printStackTrace();}}if (conn != null) {try {conn.close();} catch (SQLException e) {e.printStackTrace();}}}}
}

ExitOperation类

package opera;import book.BookList;public class ExitOperation implements IOPeration{@Overridepublic void work(BookList bookList) {System.out.println("退出系统!");System.exit(0);//退出系统。0代表正常退出}
}

Main类

import book.BookList;
import user.AdminUser;
import user.NormalUser;
import user.User;import java.util.Scanner;public class Main {//登录public static User login() {System.out.println("请输入你的姓名:");Scanner scanner = new Scanner(System.in);String name = scanner.nextLine();System.out.println("请选择你的身份:1->管理员 0->普通用户");int choice = scanner.nextInt();if (choice == 1) {//说明是管理员//由于有返回值,所以我们的方法返回值就不能写void了。但是我们也无法确定返回值是什么,可能是管理员,可能是用户。所以,用向上转型,写User.return new AdminUser(name);//返回实例化一个管理员对象} else {return new NormalUser(name);//返回实例化一个用户对象}}public static void main(String[] args) {User user = login();//执行上面的login方法BookList bookList = new BookList();while (true) {int choice = user.menu();//实现打印菜单user.doWork(choice, bookList);}}
}

http://www.lryc.cn/news/473739.html

相关文章:

  • 模板初阶及STL简介
  • UE5 不同的编译模式下,module的组织形式
  • 【ms-swift 大模型微调实战】
  • Linux:网络基础
  • mysql 的内连接、左连接、右连接有什么区别?
  • update-alternatives(选择工具)
  • php解密,sg11解密-sg15解密 如何由sourceGuardian11-sourceGuardian15加密(sg11加密~sg15加密)的源码
  • b站小土堆PyTorch视频学习笔记(二)
  • Linux的压缩及其解压命令
  • GXYCTF2019:gakki
  • 顺序表(C 语言)
  • 一:时序数据库-Influx应用
  • Word文档丢失抢救方法:15 个 Word 文档恢复工具
  • 关于自动驾驶等级相关知识
  • Java中跳转结构
  • CNN-Attention分类预测 | Matlab实现多特征分类预测
  • [java][基础]JSP
  • 《测绘学报》
  • 代码随想录之链表刷题总结
  • Python爬虫的“京东大冒险”:揭秘商品类目信息
  • 双目视觉标定——1原理与实践
  • 【设计模式系列】代理模式(八)
  • 微服务架构设计的初次尝试——基于以太坊智能合约 + NestJS 微服务的游戏社区与任务市场系统:架构设计
  • “北斗+实景三维”,助力全域社会治理
  • #渗透测试#SRC漏洞挖掘# 信息收集-常见端口及谷歌语法
  • 如何使用java雪花算法在分布式环境中生成唯一ID?
  • 【php常用公共函数】php获取指定时间段相差几小时,几分钟,几秒
  • 图文深入介绍Oracle DB link(一)
  • Uniswap/v2-core使用及其交易流程
  • clickhouse运维篇(二):多机器手动部署ck集群