栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

基于java+swring+数据库实现的图书管理系统

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

基于java+swring+数据库实现的图书管理系统

说明
一、角色:管理员、普通用户
二、功能划分
(1)管理员:图书查询(查询全部、按类别查询、按书名查询、按书号查询)、图书管理(新增、删除、修改)、用户查询(查询全部、按用户名查询、按学号查询)、用户管理(新增、删除)、账户管理(修改密码、退出登录)
(2)普通用户:图书查询(查询全部、按类别查询、按书名查询、按书号查询)、图书借还(借书、还书)、账户管理(修改密码、借书记录、退出登录)
三、说明:含有数据库、源码、视频、截图、数据库驱动、部分说明
四、该系统功能完善,可实现借书、还书的过程,代码简洁明了~
源码链接点击下载
创作不易,提供部分核心代码
运行截图









部分代码
UserDao

package Dao;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.Vector;

public class UserDao {
    public UserDao() {
    }

    // 登录
    public static void login(String user,String password) {
        Connection con = DB.getConnection();
        PreparedStatement preSql;
        ResultSet rs;
        String sqlStr = "select * from usertable where `name` = ? and password =?";
        try {
            preSql = con.prepareStatement(sqlStr);
            preSql.setString(1, user);
            preSql.setString(2, password);
            rs = preSql.executeQuery();
            boolean flag = false;
            while (rs.next()) {
                flag = true;
                String admin = rs.getString(5);
                System.out.println(admin);
            }
            if (!flag) {
                JOptionPane.showMessageDialog(null, "用户不存在", "警告", JOptionPane.WARNING_MESSAGE);
            }
            con.close();
        } catch (SQLException e) {
        }
    }


    //添加用户
    public static void adduser(String user,String id,String name,String password) {
        try {
            Connection con = DB.getConnection();
            //新增语句
            String sql = "INSERT into usertable(user,studentid,name,password) VALUES(?,?,?,?)";
            PreparedStatement ps = con.prepareStatement(sql);
            ps.setString(1, user);
            ps.setString(2, id);
            ps.setString(3, name);
            ps.setString(4, password);
            ps.executeUpdate();
            DB.close(null,ps,con);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    // 显示所有用户
    public static void alluser(DefaultTableModel model) {
        Connection con = DB.getConnection();
        PreparedStatement preSql;
        ResultSet rs;
        String sqlStr = "select * from usertable";
        try {
            preSql = con.prepareStatement(sqlStr);
            rs = preSql.executeQuery();
            while (rs.next()) {
                String user = rs.getString(1);
                String id = rs.getString(2);
                String name = rs.getString(3);
                String password = rs.getString(4);
                model.addRow(new Vector<>(Arrays.asList(user, id, name, password)));
            }
            con.close();
        } catch (SQLException e) {
        }}

    // 用户名按查找用户
    public static void finduser(DefaultTableModel model, String user) {
        Connection con = DB.getConnection();
        PreparedStatement preSql;
        ResultSet rs;
        String sqlStr = "select * from usertable where user = ?";
        try {
            preSql = con.prepareStatement(sqlStr);
            preSql.setString(1, user);
            rs = preSql.executeQuery();
            boolean flag = false;
            while (rs.next()) {
                flag = true;
                String us = rs.getString(1);
                String id = rs.getString(2);
                String name = rs.getString(3);
                String password = rs.getString(4);
                model.addRow(new Vector<>(Arrays.asList(us, id, name, password)));
            }
            if (!flag) {
                JOptionPane.showMessageDialog(null, "用户不存在", "警告", JOptionPane.WARNING_MESSAGE);
            }
            con.close();
        } catch (SQLException e) {
        }
    }
    // 学号按查找用户
    public static void finduserid(DefaultTableModel model, String user) {
        Connection con = DB.getConnection();
        PreparedStatement preSql;
        ResultSet rs;
        String sqlStr = "select * from usertable where studentid = ?";
        try {
            preSql = con.prepareStatement(sqlStr);
            preSql.setString(1, user);
            rs = preSql.executeQuery();
            boolean flag = false;
            while (rs.next()) {
                flag = true;
                String us = rs.getString(1);
                String id = rs.getString(2);
                String name = rs.getString(3);
                String password = rs.getString(4);
                model.addRow(new Vector<>(Arrays.asList(us, id, name, password)));
            }
            if (!flag) {
                JOptionPane.showMessageDialog(null, "用户不存在", "警告", JOptionPane.WARNING_MESSAGE);
            }
            con.close();
        } catch (SQLException e) {
        }
    }
    //删除用户
    public static void deleteuser(String user) {
        Connection con = DB.getConnection();
        PreparedStatement preSql;

        String sqlStr = "delete from usertable where user = ?";
        try {
            preSql = con.prepareStatement(sqlStr);
            preSql.setString(1, user);
            preSql.executeUpdate();
            con.close();
        } catch (SQLException e) {
        }}
}

Main

package Test;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import Dao.LandDao;
import View.AdminMainFace;
import View.MainFace;
public class Main {
    private JLabel jLabel = new JLabel("图书管理系统");
    private JLabel jLabel2 = new JLabel("用户名: ");
    private JLabel jLabel3 = new JLabel("密  码: ");
    private Font font = new Font("宋体", Font.BOLD, 40);
    private Font font2 = new Font("宋体", Font.BOLD, 25);
    private Font font3 = new Font("宋体", Font.BOLD, 20);
    private JTextField field = new JTextField(22);
    private JPasswordField field2 = new JPasswordField(22);
    private JButton button = new JButton("登陆");
    private JButton button2 = new JButton("重置");
    private JPanel jPanel = new JPanel();
    private JPanel jPanel2 = new JPanel();
    private JPanel jPanel3 = new JPanel();
    private JPanel jPanel4 = new JPanel();
    private JPanel jPanel5 = new JPanel();
    private JFrame frame = new JFrame("登陆");
    private Dimension dimension = new Dimension(30, 30);
    private Dimension dimension2 = new Dimension(100, 50);
    public String user;
    private String password;
    public Main() {
        frame.setTitle("登陆");
        frame.setSize(550, 500);
        frame.setLocationRelativeTo(null);
        frame.setLayout(null);
        Toolkit t = Toolkit.getDefaultToolkit();
        addassembly();
        transparent();
        addEvent();
        frame.setResizable(false);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);}
    private void addassembly() {
        jLabel.setFont(font);
        jLabel2.setFont(font2);
        jLabel3.setFont(font2);
        button.setFont(font3);
        button2.setFont(font3);
        field.setFont(font3);
        field2.setFont(font3);
        field.setPreferredSize(dimension);
        field2.setPreferredSize(dimension);
        button.setPreferredSize(dimension2);
        button2.setPreferredSize(dimension2);
        jPanel.add(jLabel);
        jPanel2.add(jLabel2);
        jPanel2.add(field);
        jPanel3.add(jLabel3);
        jPanel3.add(field2);
        jPanel4.add(button);
        jPanel5.add(button2);
        jPanel.setBounds(0, 60, 550, 80);
        jPanel2.setBounds(0, 160, 550, 80);
        jPanel3.setBounds(0, 250, 550, 60);
        jPanel4.setBounds(100, 325, 150, 80);
        jPanel5.setBounds(275, 325, 150, 80);
        frame.add(jPanel);
        frame.add(jPanel2);
        frame.add(jPanel3);
        frame.add(jPanel4);
        frame.add(jPanel5);}
    private void transparent() {
        jLabel.setOpaque(false);
        jLabel2.setOpaque(false);
        jLabel3.setOpaque(false);
        field.setOpaque(false);
        field2.setOpaque(false);
        jPanel.setOpaque(false);
        jPanel2.setOpaque(false);
        jPanel3.setOpaque(false);
        jPanel4.setOpaque(false);
        jPanel5.setOpaque(false);}
    private void addEvent() {
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                user = field.getText().trim();
                password = field2.getText().trim();
                if(LandDao.test(user, password)) {
                    JOptionPane.showMessageDialog(null, "登陆成功");
                    frame.dispose();
                    if (LandDao.sureadmin(user)){
                        new AdminMainFace(user);
                    }else {
                        new MainFace(user);
                    }
                }else {
                    empty();
                }}
        });
        button2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                empty();
            }
        });
    }
    private void empty() {
        field.setText("");
        field2.setText("");
    }
    public static void main(String[] args) {
        new Main();
    }}

AdminFace

package View;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;

public class AdminFace {
    public JPanel jPanel2 = new JPanel();
    private JLabel jLabel = new JLabel("用户管理");
    private JButton button2 = new JButton("添加用户");
    private JButton button5 = new JButton("删除用户");
    private Font font = new Font("宋体", Font.BOLD, 60);
    private Font font1 = new Font("宋体", Font.BOLD, 25);
    private String user;
    private JFrame frame;
    public DefaultTableModel model = new DefaultTableModel();
    public AdminFace() {
        jPanel2.setLayout(null);
        jLabel.setFont(font);
        jLabel.setBounds(460, 50, 800, 70);
        button2.setFont(font1);

        button5.setFont(font1);
        button2.setBounds(460, 200, 250, 50);
        button5.setBounds(460, 270, 250, 50);
        button2.setBackground(Color.cyan);
        button5.setBackground(Color.cyan);
        add();
        button2.setOpaque(false);
        button5.setOpaque(false);
        jPanel2.add(button2);
        jPanel2.add(button5);
        jPanel2.add(jLabel);
    }
    public void setUser(String user) {
        this.user = user;
    }
    public void setFrame(JFrame frame) {
        this.frame = frame;
    }

    public void setModel(DefaultTableModel model) {
        this.model = model;
    }
    private void add() {
        button2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                 new AddUserFace();
            }
        });

        button5.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                DeleteUserFace deleteUserFace = new DeleteUserFace();
                deleteUserFace.setModel(model);
            }
        });
    }}

AdminMainFace

package View;

import Dao.LandDao;

import javax.swing.*;
import java.awt.*;

public class AdminMainFace extends JFrame{
    public  JTabbedPane jTabbedPane = new JTabbedPane();
    private JPanel jPanel = new JPanel();
    private JLabel jLabel = new JLabel("欢迎登陆图书管理系统");
    private Font font = new Font("宋体", Font.BOLD, 60);
    private Font font2 = new Font("宋体", Font.BOLD, 20);
    private Container con = getContentPane();
    public AdminMainFace(String user) {
        Toolkit t = Toolkit.getDefaultToolkit();
        jPanel.setLayout(null);
        jLabel.setFont(font);
        jLabel.setBounds(330, 50, 1000,200);
        jLabel.setForeground(Color.BLACK);
        jPanel.add(jLabel);
        jTabbedPane.setFont(font2);
        jTabbedPane.add("主 界 面", jPanel);

        BookSearchFace search = new BookSearchFace();
        jTabbedPane.add("图书查询",search.jLayeredPane);

        BookAdminFace bookAdmin = new BookAdminFace();
        bookAdmin.setModel(search.model);
        jTabbedPane.add("图书管理",bookAdmin.jPanel2);

        UserSearchFace searchFace = new UserSearchFace();
        jTabbedPane.add("用户查询",searchFace.jLayeredPane);

        AdminFace admin = new AdminFace();
        jTabbedPane.add("用户管理", admin.jPanel2);

        AdminUserFace management = new AdminUserFace();
        management.setUser(user);
        management.setFrame(this);
        jTabbedPane.add("账户管理", management.jPanel2);


        con.add(jTabbedPane);
        setResizable(false);
        setTitle("图书管理系统");
        setSize(1200, 800);
        setLocationRelativeTo(null);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }}

UserFace

package View;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import Test.Main;

public class UserFace {
    public JPanel jPanel2 = new JPanel();
    private JLabel jLabel = new JLabel("账户管理");
    private JButton button2 = new JButton("修改密码");
    private JButton button3 = new JButton("借书记录");
    private JButton button4 = new JButton("退出登陆");
    private Font font = new Font("宋体", Font.BOLD, 60);
    private Font font1 = new Font("宋体", Font.BOLD, 25);
    private String user;
    private JFrame frame;
    public UserFace() {
        jPanel2.setLayout(null);
        jLabel.setFont(font);
        jLabel.setBounds(460, 50, 800, 70);
        button2.setFont(font1);
        button3.setFont(font1);
        button4.setFont(font1);
        button2.setBounds(460, 200, 250, 50);
        button3.setBounds(460, 300, 250, 50);
        button4.setBounds(460, 400, 250, 50);
        button2.setBackground(Color.cyan);
        button3.setBackground(Color.cyan);
        button4.setBackground(Color.cyan);
        button2.setOpaque(false);
        button3.setOpaque(false);
        button4.setOpaque(false);
        add();
        jPanel2.add(button2);
        jPanel2.add(button3);
        jPanel2.add(button4);
        jPanel2.add(jLabel);}
    public void setUser(String user) {
        this.user = user;}
    public void setFrame(JFrame frame) {
        this.frame = frame;}
    private void add() {
        button2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                UpdatePswFace password = new UpdatePswFace(user);
                password.setFrame(frame);
            }});
        button3.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                new UBorrowBookFace(user);
            }});
        button4.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                frame.dispose();
                new Main();}
        });
    }}

BorrowDao

package Dao;
import java.sql.*;
import java.sql.Date;
import java.util.*;
import javax.swing.table.DefaultTableModel;
public class BorrowDao {
    public BorrowDao() {
    }
    //借阅图书
    public static void Borrow(String user, int bookid, String bookname) {
        Connection con = DB.getConnection();
        PreparedStatement preSql;
        String sqlStr = "insert into borrowrecords(user,bookid,bookname,borrowtime,returntime,status) values (?,?,?,now(),null,?)";
        try {
            preSql = con.prepareStatement(sqlStr);
            preSql.setString(1, user);
            preSql.setInt(2, bookid);
            preSql.setString(3, bookname);
            preSql.setString(4, "未还");
            int ok = preSql.executeUpdate();
            con.close();
            ChangeBorrowState(bookid);
        } catch (SQLException e) {
        }}
    //借阅图书修改图书状态
    private static void ChangeBorrowState(int bookid) {
        Connection con = DB.getConnection();
        PreparedStatement preSql;
        String sqlStr = "update booktable set state=? where bookid = ?";
        try {
            preSql = con.prepareStatement(sqlStr);
            preSql.setString(1, "外借");
            preSql.setInt(2, bookid);
            int ok = preSql.executeUpdate();
            con.close();
        } catch (SQLException e) {
        }}
    //还书
    public static void Return(String user, int bookid) {
        Connection con = DB.getConnection();
        PreparedStatement preSql;
        String sqlStr = "select now()";
        ResultSet rs;
        Date date=null;
        try {
            preSql = con.prepareStatement(sqlStr);
            rs = preSql.executeQuery();
            while(rs.next()) {
                date = rs.getDate(1);
            }
            sqlStr = "update borrowrecords set returntime =  ? where bookid = ? and user = ? and status = ?";
            preSql = con.prepareStatement(sqlStr);
            preSql.setDate(1, date);
            preSql.setInt(2, bookid);
            preSql.setString(3, user);
            preSql.setString(4, "未还");
            int ok = preSql.executeUpdate();
            sqlStr = "update borrowrecords set status = ? where bookid = ? and user = ? and status = ?";
            preSql = con.prepareStatement(sqlStr);
            preSql.setString(1, "已还");
            preSql.setInt(2, bookid);
            preSql.setString(3, user);
            preSql.setString(4, "未还");
            ok = preSql.executeUpdate();
            con.close();
            ChangeReturnState(bookid);
        } catch (SQLException e) {
        }}
    //还书图书修改图书状态
    private static void ChangeReturnState(int bookid) {
        Connection con = DB.getConnection();
        PreparedStatement preSql;
        String sqlStr = "update booktable set state=? where bookid = ?";
        try {
            preSql = con.prepareStatement(sqlStr);
            preSql.setString(1, "在馆");
            preSql.setInt(2, bookid);
            int ok = preSql.executeUpdate();
            con.close();
        } catch (SQLException e) {
        }}
    //比对还书
    public static boolean comparison(String user,int bookid) {
        Connection con = DB.getConnection();
        PreparedStatement preSql;
        ResultSet rs;
        String sqlStr = "select * from borrowrecords where bookid = ? and status = ?";
        try {
            preSql = con.prepareStatement(sqlStr);
            preSql.setInt(1, bookid);
            preSql.setString(2, "未还");
            rs = preSql.executeQuery();
            while (rs.next()) {
                String user2 = rs.getString(2);
                if(user2.equals(user)) {
                    return true;
                }else {
                    return false;
                }}
            con.close();
            return false;
        } catch (SQLException e) {
            return false;
        }}
    public static void allborrow(DefaultTableModel model) {
        Connection con = DB.getConnection();
        PreparedStatement preSql;
        ResultSet rs;
        String sqlStr = "select * from borrowrecords";
        try {
            preSql = con.prepareStatement(sqlStr);
            rs = preSql.executeQuery();
            while (rs.next()) {
                String user = rs.getString(2);
                int bookid = rs.getInt(3);
                String bookname = rs.getString(4);
                Date date = rs.getDate(5);
                Date date2 = rs.getDate(6);
                String status = rs.getString(7);
                model.addRow(new Vector<>(Arrays.asList(user,bookid,bookname,date,date2,status)));}
            con.close();
        } catch (SQLException e) {
        }}
    //用户名借书记录
    public static void userborrow(DefaultTableModel model,String user) {
        Connection con = DB.getConnection();
        PreparedStatement preSql;
        ResultSet rs;
        String sqlStr = "select * from borrowrecords where user = ?";
        try {
            preSql = con.prepareStatement(sqlStr);
            preSql.setString(1, user);
            rs = preSql.executeQuery();
            while (rs.next()) {
                int bookid = rs.getInt(3);
                String bookname = rs.getString(4);
                Date date = rs.getDate(5);
                Date date2 = rs.getDate(6);
                String status = rs.getString(7);
                model.addRow(new Vector<>(Arrays.asList(user,bookid,bookname,date,date2,status)));}
            con.close();
        } catch (SQLException e) {
        }}
    //书号借书记录
    public static void bookidborrow(DefaultTableModel model,int bookid) {
        Connection con = DB.getConnection();
        PreparedStatement preSql;
        ResultSet rs;
        String sqlStr = "select * from borrowrecords where bookid = ?";
        try {
            preSql = con.prepareStatement(sqlStr);
            preSql.setInt(1, bookid);
            rs = preSql.executeQuery();
            while (rs.next()) {
                String user = rs.getString(2);
                String bookname = rs.getString(4);
                Date date = rs.getDate(5);
                Date date2 = rs.getDate(6);
                String status = rs.getString(7);
                model.addRow(new Vector<>(Arrays.asList(user,bookid,bookname,date,date2,status)));}
            con.close();
        } catch (SQLException e) {}
    }}

AdminUserFace

package View;

import Test.Main;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class AdminUserFace {
    public JPanel jPanel2 = new JPanel();
    private JLabel jLabel = new JLabel("账户管理");
    private JButton button2 = new JButton("修改密码");
    private JButton button3 = new JButton("退出登陆");
    private Font font = new Font("宋体", Font.BOLD, 60);
    private Font font1 = new Font("宋体", Font.BOLD, 25);
    private String user;
    private JFrame frame;
    public AdminUserFace() {
        jPanel2.setLayout(null);
        jLabel.setFont(font);
        jLabel.setBounds(460, 50, 800, 70);
        button2.setFont(font1);
        button3.setFont(font1);
        button2.setBounds(460, 200, 250, 50);
        button3.setBounds(460, 300, 250, 50);
        button2.setBackground(Color.cyan);
        button3.setBackground(Color.cyan);
        button2.setOpaque(false);
        button3.setOpaque(false);
        add();
        jPanel2.add(button2);
        jPanel2.add(button3);
        jPanel2.add(jLabel);}
    public void setUser(String user) {
        this.user = user;}
    public void setFrame(JFrame frame) {
        this.frame = frame;}
    private void add() {
        button2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                UpdatePswFace password = new UpdatePswFace(user);
                password.setFrame(frame);
            }});
        button3.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                frame.dispose();
                new Main();}
        });
    }}

创作不易,源码链接点击下载

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/918917.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号