JDBC-图书管理系统

Posted 霏ིྀ宇ིྀ

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JDBC-图书管理系统相关的知识,希望对你有一定的参考价值。

文章目录

JDBC-图书管理系统

数据库设计

BOOK

CREATE TABLE `book`  (
  `book_id` int(11) NOT NULL AUTO_INCREMENT,
  `book_name` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `book_kind` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `book_author` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `book_print` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `book_state` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  PRIMARY KEY USING BTREE (`book_id`)
) ENGINE = InnoDB AUTO_INCREMENT = 10 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;

Boorrow

CREATE TABLE `borrow`  (
  `borrow_id` int(11) NOT NULL AUTO_INCREMENT,
  `borrow_bookid` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  `borrow_readerid` int(11) NOT NULL,
  `borrow_gettime` date NOT NULL,
  `borrow_returntime` date NOT NULL,
  `ya_money` int(11) NULL DEFAULT NULL,
  PRIMARY KEY USING BTREE (`borrow_id`)
) ENGINE = InnoDB AUTO_INCREMENT = 12 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;

reader

DROP TABLE IF EXISTS `reader`;
CREATE TABLE `reader`  (
  `reader_id` int(11) NOT NULL AUTO_INCREMENT,
  `reader_book_id` int(11) NOT NULL,
  `reader_name` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  `reader_sex` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  `reader_tel` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  `reader_department` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  `reader_class` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  PRIMARY KEY USING BTREE (`reader_id`)
) ENGINE = InnoDB AUTO_INCREMENT = 7 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;

user

CREATE TABLE `user`  (
  `user_id` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  `user_keyword` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  `user_role` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  PRIMARY KEY USING BTREE (`user_id`)
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;

数据库系统登录界面

管理员界面

1:用户登录

UserDao

  public List<UserInfo> login(String username, String pwd)
        //创建一个集合,用来存放从数据库查询到的数据封装的对象
        ArrayList<UserInfo> list = new ArrayList<>();
        String sql = "select * from user where user_id=? and user_keyword=?";
        try 
            ps=con.prepareStatement(sql);
            //给?赋值
            ps.setString(1, username);
            ps.setString(2, pwd);
            //执行sql  得到一个resultSet
            rs = ps.executeQuery();
            //处理rs
            while (rs.next())
                UserInfo user = new UserInfo(); //创建一个user对象用来接收数据库查的信息
                String user_id = rs.getString("user_id");
                String user_keyword = rs.getString("user_keyword");
                String user_role = rs.getString("user_role");
                user.setUser_id(user_id);
                user.setUser_ketword(user_keyword);
                user.setUser_role(user_role); //截止到这里,我们把数据库查询出来的数据,封装到了user中
                list.add(user);    //把封装并赋值完成的对象存入list集合中
            
         catch (SQLException e) 
            e.printStackTrace();
        

        return list;
    

2:修改密码

UserDao

 public int updateKeyWord(String username, String pwd)
        //创建一个集合,用来存放从数据库查询到的数据封装的对象

        String sql = "UPDATE user set   user_keyword=?  where user_id=? ";
        try 
            ps=con.prepareStatement(sql);
            //给?赋值
            ps.setString(1, pwd);
            ps.setString(2, username);
            //执行sql  得到一个resultSet
             num = ps.executeUpdate();
            //处理rs

         catch (SQLException e) 
            e.printStackTrace();
        

        return  num;

    

3:新增用户功能

UserDao

  public int insertMethod(UserInfo userInfo)
        //ArrayList<UserInfo> list = new ArrayList<>();
        String sql = "insert into  user values(?,?,?) ";
        try 
            ps=con.prepareStatement(sql);
            //给?赋值
            ps.setString(1, userInfo.getUser_id());
            ps.setString(2, userInfo.getUser_ketword());

            ps.setString(3, userInfo.getUser_role());
            //执行sql  得到一个resultSet
             num = ps.executeUpdate();
            //处理rs

         catch (SQLException e) 
            e.printStackTrace();
        

        return  num;
    

4:删除用户功能

UserDao

 public int delectMethod(UserInfo user1)  
       // ArrayList<UserInfo> list = new ArrayList<>();
        String sql = "delete FROM user where user_id = ?";
        try 
            ps = con.prepareStatement(sql);
            ps.setObject(1, user1.getUser_id());
             num = ps.executeUpdate();

         catch (Exception e) 
            e.printStackTrace();
        
        return num;

    

5:查询用户(根据用户名模糊查询)

UserDao

 public List<UserInfo> selectUser(String content) 
        ArrayList<UserInfo> list = new ArrayList<>();

        String sql = "select * from user where user_id like ? ";
        try 

            ps = con.prepareStatement(sql);
            ps.setObject(1, "%"+content+"%");
            rs = ps.executeQuery();
            while (rs.next())
                UserInfo user = new UserInfo();
                String user_id = rs.getString("user_id");
                String user_keyword = rs.getString("user_keyword");
                String user_role = rs.getString("user_role");
                user.setUser_id(user_id);
                user.setUser_ketword(user_keyword);
                user.setUser_role(user_role); 
                list.add(user);   
            

         catch (Exception e) 
            e.printStackTrace();
        


        return list;

    

UserControl

package com.ts.controller;

import com.ts.Service.UserService;
import com.ts.pojo.UserInfo;

import java.util.Iterator;
import java.util.List;
import java.util.Scanner;

/**
 * @description:com.ts.controller_Word.java
 * @author: 霏宇
 * @time: 2022/6/27,17:22
 */
public class UserControl 
    Scanner sc = new Scanner(System.in);
    // 创建user逻辑层对象
    UserService service = new UserService();
      BookControl control2= new BookControl();
    ReadControl control3=new ReadControl();
    // 数据库登录界面
    public void login() 
        System.out.println("------数据库系统登录界面------");
        System.out.println("请输入您的登录名");
        UserInfo user = new UserInfo();
        String user_id = sc.next();  //获取控制台输入的内容 用作user_id
        user.setUser_id(user_id); //给user对象设置user_id
        System.out.println("请输入您的密码");
        String user_keyWord = sc.next();
        user.setUser_ketword(user_keyWord);
        System.out.println(user.toString());
        //调用service层方法 获取权限
        String quanxian = service.login(user.getUser_id(), user.getUser_ketword());
        if(quanxian.equals("admin"))
            indexGuan(user);
        else if(quanxian.equals("普通用户"))
            indexYon(user);
        else 
            System.out.println("输入的登录名与密码不匹配");
            login();
        
    
    //管理员界面
    public void indexGuan(UserInfo user) 
        System.out.println("---------管理员界面----------");
        System.out.println("输入数字,选择相应的功能");
        System.out.println("1:用户登录");
        System.out.println("2:修改密码");
        System.out.println("3:新增用户功能");
        System.out.println("4:删除用户功能");
        System.out.println("5:查询用户(根据用户名模糊查询)");
        System.out.println("6:新增图书");
        System.out.println("7:删除图书");
        System.out.println("8:修改图书");
        System.out.println("9:退出数据库系统界面");
        int choose = sc.nextInt();
        if (choose == 1) 
            login();
        
        if (choose == 2) 
            updateKeyWord(user);
        
        if (choose == 3) 
            insertMethod(user);
        
        if (choose == 4) 
            delectMethod(user);
        
        if (choose == 5) 
            selectUser(user);

        
        if (choose == 6) 
            control2.insertMethod();
            indexGuan(user);
        

        if (choose == 7) 
            control2.delectMethod();
            indexGuan(user);
        
        if (choose == 8) 
            control2.updateBookInfo();
            indexGuan(user);
        
        if (choose == 9) 
            System.out.println("------退出数据库系统界面---------");
        
    
    /**
     * @param user
     *            用户界面
     */
    public void indexYon(UserInfo user) 
        System.out.println("---------用户界面----------");
        System.out.println("输入数字,选择相应的功能");
        System.out.println("1:新增读者信息,同时新增一个读者用户信息");
        System.out.println("2:删除读者信息,同时删除读者的用户信息");
        System.out.println("3:修改读者信息,同时修改读者的用户信息");
        System.out.println("4:查询最近1周将要到期图书名,读者姓名,还书时间,借阅时间");
        System.out.println("5:查询已过还书时间的图书名,读者姓名,还书时间,借阅时间");
        System.out.println("6:用户借书功能");
        System.out.println("7:用户还书功能");
        System.out.println("8:退出登录");
        int choose = sc.nextInt();

        if (choose == 1) 
            control3.addMethod();
            indexYon(user);
        
        if (choose == 2) 
            control3.delectMethod();
            indexYon(user);
        
        if (choose == 3) 
            control3.updateReaderInfoMethod();
            indexYon(user);
        
        if (choose == 4) 
            control3.userSelectByWeek();
            indexYon(user);
        
        if (choose == 5) 
            control3.selectReaderInfoOutReturnTime();
            indexYon(user);
        
        if (choose == 6) 
            control3.borrowBook(user);
            indexYon(user);
        
        if (choose == 7) 
            control3.returnBorrowBook(user);
            indexYon(user);
        
        if (choose == 8) 
            System.out.println("------退出数据库系统界面---------");
        
    
    public void updateKeyWord(UserInfo user) 
		System.out.println("--------修改密码界面--------- ");
		System.out.println("请输入您要修改的密码值:");
		String adminJS = user.getUser_id();
		String key2 = sc.next();
		int num2 = service.updateKeyWord(adminJS, key2);
		if (num2 == 0) 
            System.out.println("该用户修改失败");
			updateKeyWord(user);

		 else 
			System.out.println("该用户密码修改成功");
			indexGuan(user);
		

	
	public void insertMethod(UserInfo user) 
		System.out.println("-------------管理员新增用户界面--------------");
		UserInfo user1 = new UserInfo();
		System.out.println("请输入新增用户id:");
		String user1Id = sc.next();
		System.out.println("请输入新增用户密码");
        String user1Key = sc.next();
		System.out.println("请输入新增用户类型");
		String user1Role = sc.next();

		user1.setUser_id(user1Id);
		user1.setUser_ketwor

以上是关于JDBC-图书管理系统的主要内容,如果未能解决你的问题,请参考以下文章

基于JavaSE + JDBC的图书管理系统

Servlet+JDBC设计实现图书系统管理功能实现

如何制作班级模板

使用JDBC+javafx写一个简单功能齐全的图书管理系统

Java swing项目-图书管理系统(swing+mysql+jdbc) 总结

图书管理系统( JSP + JDBC + Servlet )实现-01: 流程分析和数据库建表阶段