0


图书管理系统 ——mysql数据库

java图形化 图书管理系统 使用mysql数据库

1、管理员操作:能够实现图书的增、删、改、查操作
2、普通注册用户操作:
(1)借阅图书(借阅成功后,对应图书在馆数量相应减1)
(2)归还图书(归还成功后,对应图书在馆数量加1)
(3)查阅自己借阅的图书
(4)续借图书(借阅图书以3个月为限,可以在期间内续借1个月)
(5)如果预借图书在馆数量为零,或者图书不存在,则无法借阅
(6)可以为所借阅的图书进行评论
3自行设计数据库表

此项目已经更新,修改了所有反馈的bug,增加了新功能,增加可玩性,调整了代码结构。欢迎访问另一篇博客

https://blog.csdn.net/m0_52889702/article/details/127230912

结果展示:

普通用户
在这里插入图片描述
管理员:
在这里插入图片描述
源码包地址:
https://gitee.com/wang-yongyan188/experimental-code.git

库表设计:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

代码:引入jdbc-mysql的jar包 添加到库

在这里插入图片描述

创建实体类:

book:

publicclassBook{String bname;Integer bid;Integer allnum;Integer borrownum;String type;publicBook(){}publicBook(String bname,Integer bid,Integer allnum,Integer borrownum,String type){this.bname = bname;this.bid = bid;this.allnum = allnum;this.borrownum = borrownum;this.type = type;}publicStringgetType(){return type;}publicvoidsetType(String type){this.type = type;}publicStringgetBname(){return bname;}publicvoidsetBname(String bname){this.bname = bname;}publicIntegergetBid(){return bid;}publicvoidsetBid(Integer bid){this.bid = bid;}publicIntegergetAllnum(){return allnum;}publicvoidsetAllnum(Integer allnum){this.allnum = allnum;}publicIntegergetBorrownum(){return borrownum;}publicvoidsetBorrownum(Integer borrownum){this.borrownum = borrownum;}@OverridepublicStringtoString(){return"Book{"+"bname='"+ bname +'\''+", bid="+ bid +", allnum="+ allnum +", borrownum="+ borrownum +'}';}}

User:

publicclassUser{Integer id;String passwd;String uname;Integer ismanger;publicUser(Integer id,String passwd,String uname,Integer ismanger){this.id = id;this.passwd = passwd;this.uname = uname;this.ismanger = ismanger;}publicUser(){}publicIntegergetId(){return id;}publicvoidsetId(Integer id){this.id = id;}publicStringgetPasswd(){return passwd;}publicIntegergetIsmanger(){return ismanger;}publicvoidsetIsmanger(Integer ismanger){this.ismanger = ismanger;}publicvoidsetPasswd(String passwd){this.passwd = passwd;}publicStringgetUname(){return uname;}publicvoidsetUname(String uname){this.uname = uname;}@OverridepublicStringtoString(){return"User{"+"id="+ id +", passwd='"+ passwd +'\''+", uname='"+ uname +'\''+", ismanger="+ ismanger +'}';}}

Rent:

publicclassRent{Integer rid;String btime;String days;Integer uid;Integer bid;publicRent(){}publicRent(Integer rid,String btime,String days,Integer uid,Integer bid){this.rid = rid;this.btime = btime;this.days = days;this.uid = uid;this.bid = bid;}publicIntegergetRid(){return rid;}publicvoidsetRid(Integer rid){this.rid = rid;}publicStringgetBtime(){return btime;}publicvoidsetBtime(String btime){this.btime = btime;}publicStringgetDays(){return days;}publicvoidsetDays(String days){this.days = days;}publicIntegergetUid(){return uid;}publicvoidsetUid(Integer uid){this.uid = uid;}publicIntegergetBid(){return bid;}publicvoidsetBid(Integer bid){this.bid = bid;}@OverridepublicStringtoString(){return"Rent{"+"rid="+ rid +", btime='"+ btime +'\''+", days='"+ days +'\''+", uid="+ uid +", bid="+ bid +'}';}}

与数据库进行交互 封装成的工具类:

importcode6.entity.Book;importcode6.entity.Rent;importcode6.entity.User;importjava.sql.*;importjava.text.ParseException;importjava.text.SimpleDateFormat;importjava.util.*;importjava.util.Date;publicclassSqlUtils{publicstaticConnectiongetCon()throwsClassNotFoundException,SQLException{Class.forName("com.mysql.cj.jdbc.Driver");Connection con =DriverManager.getConnection("jdbc:mysql://localhost:3306/code6","root","1234");return con;}publicstaticintAutoId(){int orderId = UUID.randomUUID().toString().hashCode();
        orderId = orderId <0?-orderId : orderId;return orderId;}publicstaticUsergetUserById(int id)throwsSQLException,ClassNotFoundException{Connection con =getCon();Statement statement =  con.createStatement();String sql ="SELECT * from `user` where uid="+ id;ResultSet resultSet = statement.executeQuery(sql);System.out.println(resultSet.next());User user =newUser();
        user.setId(resultSet.getInt(1));
        user.setPasswd(resultSet.getString(2));
        user.setUname(resultSet.getString(3));
        user.setIsmanger(resultSet.getInt(4));
        statement.close();
          con.close();return user;}publicstaticList<Book>getAll()throwsSQLException,ClassNotFoundException{Connection con =getCon();Statement statement =  con.createStatement();String sql ="SELECT * FROM book";ResultSet resultSet = statement.executeQuery(sql);ArrayList<Book> books =newArrayList<>();Book book =newBook();while(resultSet.next()){
            book.setBname(resultSet.getString(1));
            book.setBid(resultSet.getInt(2));
            book.setAllnum(resultSet.getInt(3));
            book.setBorrownum(resultSet.getInt(4));
            book.setType(resultSet.getString(5));
            books.add(book);}
      statement.close();
        con.close();return books;}publicstaticintaddBook(Book book)throwsSQLException,ClassNotFoundException{Connection con =getCon();String sql ="INSERT book VALUES(?,?,?,?,?);";PreparedStatement prepareStatement = con.prepareStatement(sql);
        prepareStatement.setString(1, book.getBname());
        prepareStatement.setInt(2,AutoId());
        prepareStatement.setInt(3, book.getAllnum());
        prepareStatement.setInt(4, book.getBorrownum());
        prepareStatement.setString(5, book.getType());int i = prepareStatement.executeUpdate();
        prepareStatement.close();
        con.close();return i;}publicstaticintdelBook(int bid)throwsSQLException,ClassNotFoundException{Connection con =getCon();String sql="DELETE from book WHERE bid=?";PreparedStatement prepareStatement = con.prepareStatement(sql);
        prepareStatement.setInt(1,bid);return prepareStatement.executeUpdate();}publicstaticintupBook(Book book)throwsSQLException,ClassNotFoundException{Connection con =getCon();String sql="UPDATE book SET bname=?,allnum=?,borrownum=?,type=? WHERE bid=?";PreparedStatement prepareStatement = con.prepareStatement(sql);
        prepareStatement.setString(1,book.getBname());
        prepareStatement.setInt(2,book.getAllnum());
        prepareStatement.setInt(3,book.getBorrownum());
        prepareStatement.setString(4,book.getType());
        prepareStatement.setInt(5,book.getBid());int i = prepareStatement.executeUpdate();
        prepareStatement.close();
        con.close();return i;}publicstaticintaddRent(int uid,int bid,int days,int month)throwsSQLException,ClassNotFoundException{Connection con =getCon();
        con.setAutoCommit(false);String sql="INSERT INTO rent VALUES(?,?,?,?,?)";PreparedStatement prepareStatement = con.prepareStatement(sql);
        prepareStatement.setInt(1,AutoId());Date date =newDate();SimpleDateFormat format =newSimpleDateFormat("yyyy-MM-dd HH:mm:ss");String sbegin= format.format(date);
        prepareStatement.setString(2,sbegin);Calendar cal =Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.MONTH,month);Date temptime = cal.getTime();SimpleDateFormat format2 =newSimpleDateFormat("yyyy-MM-dd");String endday = format2.format(temptime.getTime()+ days *24*60*60*1000);
        prepareStatement.setString(3,endday);
        prepareStatement.setInt(4,uid);
        prepareStatement.setInt(5,bid);String sql2="UPDATE book set borrownum=borrownum+1 where bid="+bid;int i1=0;int i=0;Statement statement = con.createStatement();try{
                i1= statement.executeUpdate(sql2);
                i= prepareStatement.executeUpdate();
                con.commit();}catch(Exception e){
             con.rollback();}finally{
                statement.close();
                prepareStatement.close();
                con.close();}return(i==1&&i==1)?1:0;}publicstaticBooleanisAddRend(int bid)throwsSQLException,ClassNotFoundException{Connection con =getCon();String sql="SELECT allnum,borrownum from book WHERE bid="+bid;Statement statement = con.createStatement();ResultSet resultSet = statement.executeQuery(sql);
        resultSet.next();int all=resultSet.getInt(1);int rent=resultSet.getInt(2);
        statement.close();
        con.close();return all>rent?true:false;}publicstaticList<Rent>getAllRentByUid(int uid)throwsSQLException,ClassNotFoundException{Connection con =getCon();String sql="SELECT * from rent where uid="+uid;Statement statement = con.createStatement();ResultSet resultSet = statement.executeQuery(sql);ArrayList<Rent> rents =newArrayList<>();while(resultSet.next()){Rent rent =newRent();
            rent.setRid(resultSet.getInt(1));
            rent.setBtime(resultSet.getString(2));
            rent.setDays(resultSet.getString(3));
            rent.setUid(resultSet.getInt(4));
            rent.setBid(resultSet.getInt(5));
            rents.add(rent);}return rents;}publicstaticBookgetBookByBid(int bid)throwsSQLException,ClassNotFoundException{Connection con =getCon();String sql="SELECT * from book where bid="+bid;Statement statement = con.createStatement();ResultSet resultSet = statement.executeQuery(sql);Book book =newBook();while(resultSet.next()){
            book.setBname(resultSet.getString(1));
            book.setBid(resultSet.getInt(2));
            book.setAllnum(resultSet.getInt(3));
            book.setBorrownum(resultSet.getInt(4));
            book.setType(resultSet.getString(5));}return book;}publicstaticList<Map>getCentosByUid(int uid)throwsSQLException,ClassNotFoundException{Connection con =getCon();Statement statement = con.createStatement();String sql="SELECT bname,b.bid,type,btime,days from book b ,rent r WHERE b.bid=r.bid and r.uid="+uid;ResultSet resultSet = statement.executeQuery(sql);ArrayList<Map> maps =newArrayList<>();while(resultSet.next()){HashMap<String,String> map =newHashMap<>();
            map.put("bname",resultSet.getString(1));
            map.put("bid",String.valueOf(resultSet.getInt(2)));
            map.put("type",resultSet.getString(3));
            map.put("btime",resultSet.getString(4));
            map.put("days",resultSet.getString(5));
            maps.add(map);}return maps;}publicstaticBooleanIsRent(int uid,int bid)throwsSQLException,ClassNotFoundException{Connection con =getCon();String sql="SELECT bid from rent WHERE uid="+uid;Statement statement = con.createStatement();ArrayList<Object> list =newArrayList<>();ResultSet resultSet = statement.executeQuery(sql);while(resultSet.next()){
            list.add(resultSet.getInt(1));}System.out.println(list);for(int i=0;i<list.size();i++){if(list.get(i).equals(bid)){returntrue;}}returnfalse;}publicstaticBooleandecRend(int uid,int bid)throwsSQLException,ClassNotFoundException{Connection con =getCon();
        con.setAutoCommit(false);String sql="DELETE from rent WHERE uid=? and bid=?";PreparedStatement prepareStatement = con.prepareStatement(sql);
        prepareStatement.setInt(1,uid);
        prepareStatement.setInt(2,bid);int i =0;String sql2="UPDATE book set borrownum=borrownum-1 where bid="+bid;Statement statement = con.createStatement();int i1 =0;try{  i=prepareStatement.executeUpdate();
             i1=statement.executeUpdate(sql2);
             con.commit();}catch(Exception e){System.out.println("失败");
            con.rollback();}return(i1>0&&i>0)?true:false;}publicstaticintcontineDays(int bid,int uid,int days)throwsSQLException,ClassNotFoundException,ParseException{Connection con =getCon();String sql1="SELECT days from rent WHERE uid="+uid+" and bid="+bid;Statement statement = con.createStatement();ResultSet resultSet = statement.executeQuery(sql1);
        resultSet.next();String  nowtime= resultSet.getString(1);SimpleDateFormat format =newSimpleDateFormat("yyyy-MM-dd");Date parse = format.parse(nowtime);Calendar instance =Calendar.getInstance();
        instance.setTime(parse);
        instance.add(Calendar.DAY_OF_MONTH,days);String afterdays = format.format(instance.getTime());String sql2="UPDATE rent SET days=? WHERE uid=? and bid=?";PreparedStatement prepareStatement = con.prepareStatement(sql2);
        prepareStatement.setString(1,afterdays);
        prepareStatement.setInt(2,uid);
        prepareStatement.setInt(3,bid);int i = prepareStatement.executeUpdate();return i;}publicstaticintaddCom(String content,int bid,int uid)throwsSQLException,ClassNotFoundException{Connection con =getCon();String sql="INSERT `comment`VALUES(?,?,?,?)";PreparedStatement prepareStatement = con.prepareStatement(sql);
        prepareStatement.setInt(1,AutoId());
        prepareStatement.setString(2,content);
        prepareStatement.setInt(3,uid);
        prepareStatement.setInt(4,bid);int i = prepareStatement.executeUpdate();
        prepareStatement.close();
        con.close();return i;}publicstaticList<String>getComById(int bid)throwsSQLException,ClassNotFoundException{ArrayList<String> list =newArrayList<>();Connection con =getCon();Statement statement = con.createStatement();String sql="SELECT * from `comment` WHERE bid="+bid;ResultSet resultSet = statement.executeQuery(sql);while(resultSet.next()){String content = resultSet.getString(2);int uid = resultSet.getInt(3);String str=content+" By [user:"+uid+"]";
            list.add(str);}
        statement.close();
        con.close();return list;}publicstaticintRegisterUser(String uname,String passwd)throwsSQLException,ClassNotFoundException{Connection con =getCon();String sql="insert user values(?,?,?,?)";PreparedStatement prepareStatement = con.prepareStatement(sql);
        prepareStatement.setString(1,passwd);
        prepareStatement.setString(2,uname);int i = prepareStatement.executeUpdate();return i;}publicstaticvoidmain(String[] args)throwsSQLException,ClassNotFoundException{}}

登录页面类:

importjavax.swing.*;importjava.awt.*;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;publicclassLogin{publicvoidloginUi(){JFrame frame =newJFrame();//设置窗体对象的属性值
        frame.setTitle("Login");//设置窗体标题
        frame.setSize(400,250);//设置窗体大小,只对顶层容器生效
        frame.setDefaultCloseOperation(3);//设置窗体关闭操作,3表示关闭窗体退出程序
        frame.setLocationRelativeTo(null);//设置窗体相对于另一组间的居中位置,参数null表示窗体相对于屏幕的中央位置
        frame.setResizable(false);//禁止调整窗体大小
        frame.setFont(newFont("宋体",Font.PLAIN,14));//设置字体,显示格式正常,大小FlowLayout fl =newFlowLayout(FlowLayout.CENTER,10,10);//实例化流式布局类的对象
        frame.setLayout(fl);//实例化JLabel标签对象,该对象显示“账号”JLabel labname =newJLabel("账号id:");
        labname.setFont(newFont("宋体",Font.PLAIN,14));
        frame.add(labname);JTextField text_name =newJTextField();Dimension dim1 =newDimension(300,30);
        text_name.setPreferredSize(dim1);//设置除顶级容器组件以外其他组件的大小
        frame.add(text_name);JLabel labpass =newJLabel("密码:");
        labpass.setFont(newFont("宋体",Font.PLAIN,14));
        frame.add(labpass);JPasswordField text_password =newJPasswordField();//设置大小
        text_password.setPreferredSize(dim1);
        frame.add(text_password);JButton button1 =newJButton();JButton button2 =newJButton("注册");Dimension dim2 =newDimension(100,30);
        button1.setText("登录");
        button1.setFont(newFont("宋体",Font.PLAIN,14));
        button2.setFont(newFont("宋体",Font.PLAIN,14));
        button1.setSize(dim2);
        button2.setSize(dim2);
        button2.addActionListener(newActionListener(){@OverridepublicvoidactionPerformed(ActionEvent e){newRegister().reg();}});

        frame.add(button1);
        frame.add(button2);
        frame.setVisible(true);CListener listener =newCListener(frame, text_name, text_password);
        button1.addActionListener(listener);}}

效果:
在这里插入图片描述

登录按钮的监听类:

importcode6.entity.User;importcode6.menu.Common;importcode6.menu.MangerMenu;importcode6.utils.SqlUtils;importjavax.swing.*;importjava.awt.*;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.sql.SQLException;publicclassCListenerimplementsActionListener{JFrame login;JTextField text_name;JPasswordField text_passwd;publicCListener(){}publicCListener(JFrame login,JTextField text_name,JPasswordField text_passwd){this.login = login;this.text_name = text_name;this.text_passwd = text_passwd;}@OverridepublicvoidactionPerformed(ActionEvent e){Dimension dim3 =newDimension(300,30);JFrame login2 =newJFrame();
        login2.setSize(400,200);
        login2.setDefaultCloseOperation(3);
        login2.setLocationRelativeTo(null);
        login2.setFont(newFont("宋体",Font.PLAIN,14));//宋体,正常风格,14号字体JPanel jp1 =newJPanel();JPanel jp2 =newJPanel();User user =newUser();try{
            user=SqlUtils.getUserById(Integer.parseInt(text_name.getText()));}catch(SQLException throwables){
            throwables.printStackTrace();}catch(ClassNotFoundException classNotFoundException){
            classNotFoundException.printStackTrace();}String passwd=user.getPasswd();if(text_passwd.getText().equals(passwd)){JLabel message =newJLabel("登陆成功!");
            message.setFont(newFont("宋体",Font.PLAIN,14));//宋体,正常风格,14号字体
            message.setPreferredSize(dim3);
            jp1.add(message);
            login2.add(jp1,BorderLayout.CENTER);
            login2.setResizable(false);
            login2.setVisible(true);
            login.dispose();if(user.getIsmanger()==1){newMangerMenu().ui(user.getId());}else{newCommon().ui(user.getId());}}else{JLabel message =newJLabel("账号或密码错误");
            message.setFont(newFont("宋体",Font.PLAIN,14));
            message.setPreferredSize(dim3);
            jp1.add(message);
            login2.add(jp1,BorderLayout.CENTER);JButton close =newJButton("确定");
            close.setFont(newFont("宋体",Font.PLAIN,14));
            close.setSize(dim3);
            jp2.add(close);
            login2.add(jp2,BorderLayout.SOUTH);
            close.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvent e){
                    login2.dispose();}});
            login2.setResizable(false);
            login2.setVisible(true);
            login.dispose();}}}

注册按钮监听类:

importcode6.utils.SqlUtils;importjavax.swing.*;importjava.awt.*;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.sql.SQLException;publicclassRegister{publicstaticvoidreg(){JFrame f =newJFrame("注册普通用户");JPanel jPanel =newJPanel();JTextField tname =newJTextField();JTextField tpasswd =newJTextField();Dimension dimension =newDimension(100,30);JLabel lname=newJLabel("用户名");JLabel lpassd=newJLabel("密码");
        lname.setFont(newFont("宋体",Font.PLAIN,14));
        lpassd.setFont(newFont("宋体",Font.PLAIN,14));
        tname.setPreferredSize(dimension);
        tpasswd.setPreferredSize(dimension);JButton jbutton =newJButton("注册");
        jbutton.addActionListener(newActionListener(){@OverridepublicvoidactionPerformed(ActionEvent e){String bname=null;String passwd=null;

                bname=tname.getText();
                passwd= tpasswd.getText();int i=0;try{
                   i=SqlUtils.RegisterUser(bname,passwd);}catch(SQLException throwables){
                    throwables.printStackTrace();}catch(ClassNotFoundException classNotFoundException){
                    classNotFoundException.printStackTrace();}if(i>0){System.out.println("注册成功");}else{System.out.println("注册失败");}}});
        jPanel.add(lname);
        jPanel.add(tname);
        jPanel.add(lpassd);
        jPanel.add(tpasswd);
        jPanel.add(jbutton);
        f.add(jPanel);
        f.setSize(700,300);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);}}

登录成功后 普通用户的菜单界面类:
在这里插入图片描述

importcode6.menu.com.BorrowBook;importcode6.menu.com.ContinueBook;importcode6.menu.com.PersonalCenter;importcode6.menu.com.SendBook;importjavax.swing.*;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.sql.SQLException;publicclassCommon{publicvoidui(int uid){JFrame jframe =newJFrame("用户主页");
        jframe.setBounds(300,180,650,500);JPanel jPanel =newJPanel();JMenuBar jmenuBar=newJMenuBar();JMenu sf =newJMenu("借阅图书");JMenu af =newJMenu("归还图书");JMenu df =newJMenu("个人中心");JMenu uf =newJMenu("续借图书");JMenuItem d1 =newJMenuItem("借阅");JMenuItem d2 =newJMenuItem("归还");JMenuItem d3 =newJMenuItem("租借记录");JMenuItem d4 =newJMenuItem("续借");
        d1.addActionListener(newActionListener(){@OverridepublicvoidactionPerformed(ActionEvent e){try{newBorrowBook().add(uid);}catch(SQLException throwables){
                    throwables.printStackTrace();}catch(ClassNotFoundException classNotFoundException){
                    classNotFoundException.printStackTrace();}}});
        d2.addActionListener(newActionListener(){@OverridepublicvoidactionPerformed(ActionEvent e){newSendBook().send();}});
        d3.addActionListener(newActionListener(){@OverridepublicvoidactionPerformed(ActionEvent e){try{newPersonalCenter().center(uid);}catch(SQLException throwables){
                    throwables.printStackTrace();}catch(ClassNotFoundException classNotFoundException){
                    classNotFoundException.printStackTrace();}}});
        d4.addActionListener(newActionListener(){@OverridepublicvoidactionPerformed(ActionEvent e){newContinueBook().con(uid);}});
        af.add(d2);
        df.add(d3);
        sf.add(d1);
        uf.add(d4);
        jmenuBar.add(sf);
        jmenuBar.add(af);
        jmenuBar.add(df);
        jmenuBar.add(uf);
        jPanel.add(jmenuBar);
        jframe.add(jPanel);
        jframe.setVisible(true);
        jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);}publicstaticvoidmain(String[] args){newCommon().ui(1);}}

在这里插入图片描述

借书功能类:
在这里插入图片描述

importcode6.menu.manger.SelectBook;importcode6.utils.SqlUtils;importjavax.swing.*;importjava.awt.*;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.sql.SQLException;publicclassBorrowBook{publicvoidadd(int uid)throwsSQLException,ClassNotFoundException{JFrame f =newJFrame("借阅书籍");JPanel jPanel =newJPanel();JTextField tid =newJTextField();Dimension dimension =newDimension(100,30);newSelectBook().comTable();JLabel lid=newJLabel("图书id");JLabel lgroup=newJLabel("月份");
        lid.setFont(newFont("宋体",Font.PLAIN,14));
        lgroup.setFont(newFont("宋体",Font.PLAIN,14));
        tid.setPreferredSize(dimension);JLabel lday=newJLabel("天数(0< and <30)");JComboBox<String> box2 =newJComboBox<>();for(int i=0;i<30;i++){
            box2.addItem(i+"天");}JComboBox<String> box1 =newJComboBox<>();
        box1.addItem("0个月");
        box1.addItem("1个月");
        box1.addItem("2个月");JButton jbutton =newJButton("添加");
       jbutton.addActionListener(newActionListener(){@OverridepublicvoidactionPerformed(ActionEvent e){int id=Integer.parseInt(tid.getText());System.out.println(id);int months=Integer.parseInt(box1.getSelectedItem().toString().substring(0,1));int days=Integer.parseInt(box2.getSelectedItem().toString().substring(0,1));int i=0;try{if(SqlUtils.IsRent(uid,id)){if(SqlUtils.isAddRend(id)){
                           i=SqlUtils.addRent(uid,id,days,months);}}}catch(SQLException throwables){
                   throwables.printStackTrace();}catch(ClassNotFoundException classNotFoundException){
                   classNotFoundException.printStackTrace();}String res=  i>0?"借阅成功":"借阅失败";System.out.println(res);}});
        jPanel.add(lid);
        jPanel.add(tid);
        jPanel.add(lgroup);
        jPanel.add(box1);
        jPanel.add(lday);
        jPanel.add(box2);
        jPanel.add(jbutton);
        f.add(jPanel);
        f.setSize(700,300);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);}}

评论书籍类:

!

importcode6.utils.SqlUtils;importjavax.swing.*;importjava.awt.*;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.sql.SQLException;importjava.text.ParseException;importjava.util.List;publicclassCommentBook{publicvoidcomment(int uid){JFrame f =newJFrame("图书评价");JPanel jPanel =newJPanel();JLabel lid=newJLabel("图书的id");
        lid.setFont(newFont("宋体",Font.PLAIN,14));JTextField tid =newJTextField();Dimension dimension =newDimension(100,30);
        tid.setPreferredSize(dimension);JLabel lcon =newJLabel("评价内容");JTextArea tcon =newJTextArea(5,20);JTextArea tta =newJTextArea(10,20);
        tta.setFont(newFont("宋体",Font.PLAIN,14));
        tta.setVisible(false);//        JTextField tcon = new JTextField();JButton jbutton =newJButton("发表自己观点");
        tcon.setFont(newFont("宋体",Font.PLAIN,14));//        lcon.setFont(new Font("宋体",Font.PLAIN,14));
        tcon.setPreferredSize(dimension);JButton jbutton2 =newJButton("查看ta评价");

        jbutton.addActionListener(newActionListener(){@OverridepublicvoidactionPerformed(ActionEvent e){int i =0;int bid=Integer.parseInt(tid.getText());try{if(SqlUtils.IsRent(uid,bid)){
                        i=SqlUtils.addCom(tcon.getText(),bid,uid);}}catch(SQLException throwables){
                    throwables.printStackTrace();}catch(ClassNotFoundException classNotFoundException){
                    classNotFoundException.printStackTrace();}if(i>0){System.out.println("评价成功");}else{System.out.println("评价失败");}}});

        jbutton2.addActionListener(newActionListener(){@OverridepublicvoidactionPerformed(ActionEvent e){int bid=Integer.parseInt(tid.getText());try{System.out.println(SqlUtils.IsRent(uid, bid));List<String> com =SqlUtils.getComById(bid);
                        tta.setText("");for(int i=0;i<com.size();i++){String con=com.get(i);
                            tta.append(con+"\n");}if(com.size()==0){
                            tta.setText("当前书籍暂无评价");}

                        tta.setVisible(true);}catch(SQLException throwables){
                    throwables.printStackTrace();}catch(ClassNotFoundException classNotFoundException){
                    classNotFoundException.printStackTrace();}}});
        jPanel.add(lid);
        jPanel.add(tid);
        jPanel.add(lcon);
        jPanel.add(tcon);
        jPanel.add(jbutton2);
        jPanel.add(jbutton);
        jPanel.add(tta);
        f.add(jPanel);
        f.setSize(600,200);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);}publicstaticvoidmain(String[] args){newCommentBook().comment(1);}}

续借功能类:

importcode6.utils.SqlUtils;importjavax.swing.*;importjava.awt.*;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.sql.SQLException;importjava.text.ParseException;publicclassContinueBook{publicvoidcon(int uid){JFrame f =newJFrame("续借图书");JPanel jPanel =newJPanel();JLabel lid=newJLabel("图书id");
        lid.setFont(newFont("宋体",Font.PLAIN,14));JTextField tid =newJTextField();Dimension dimension =newDimension(100,30);
        tid.setPreferredSize(dimension);JButton jbutton =newJButton("续借");JComboBox<String> box1 =newJComboBox<>();for(int i=1;i<=30;i++){
            box1.addItem(i+"天");}

        jbutton.addActionListener(newActionListener(){@OverridepublicvoidactionPerformed(ActionEvent e){int i =0;int days=Integer.parseInt(box1.getSelectedItem().toString().substring(0,1));int bid=Integer.parseInt(tid.getText());try{
                    i =SqlUtils.contineDays(bid, uid, days);}catch(SQLException throwables){
                    throwables.printStackTrace();}catch(ClassNotFoundException classNotFoundException){
                    classNotFoundException.printStackTrace();}catch(ParseException parseException){
                    parseException.printStackTrace();}if(i>0){System.out.println("续借成功");}else{System.out.println("续借失败");}}});
        jPanel.add(lid);
        jPanel.add(tid);
        jPanel.add(box1);
        jPanel.add(jbutton);
        f.add(jPanel);
        f.setSize(600,200);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);}}

个人中心 负责查看自己所借书的功能类:

在这里插入图片描述

importcode6.utils.SqlUtils;importjavax.swing.*;importjava.awt.*;importjava.sql.SQLException;importjava.util.ArrayList;importjava.util.HashMap;importjava.util.List;importjava.util.Map;publicclassPersonalCenter{publicvoidcenter(int uid)throwsSQLException,ClassNotFoundException{JFrame f =newJFrame("个人中心");
            f.setSize(800,300);
            f.setLocation(200,200);
            f.setLayout(newBorderLayout());String[] th =newString[]{"书籍id","书籍名称","类型","借入时间","归还日期"};List<Map> centosByUid =SqlUtils.getCentosByUid(uid);HashMap<String,String> map =newHashMap<>();String[][] td=newString[centosByUid.size()][];for(int i=0;i<td.length;i++){
                map=(HashMap<String,String>) centosByUid.get(i);
                td[i]=newString[5];
                td[i][0]= map.get("bname");
                td[i][1]= map.get("bid");
                td[i][2]= map.get("type");
                td[i][3]= map.get("btime");
                td[i][4]= map.get("days");}JTable t =newJTable(td, th);JScrollPane sp =newJScrollPane(t);
            f.add(sp,BorderLayout.CENTER);
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.setVisible(true);}publicstaticvoidmain(String[] args){try{newPersonalCenter().center(1);}catch(SQLException throwables){
            throwables.printStackTrace();}catch(ClassNotFoundException e){
            e.printStackTrace();}}}

还书功能类:

在这里插入图片描述

importcode6.utils.SqlUtils;importjavax.swing.*;importjava.awt.*;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.sql.SQLException;publicclassSendBook{publicvoidsend(){JFrame f =newJFrame("归还图书");JPanel jPanel =newJPanel();JLabel lid=newJLabel("图书id");
        lid.setFont(newFont("宋体",Font.PLAIN,14));JTextField tid =newJTextField();Dimension dimension =newDimension(100,30);
        tid.setPreferredSize(dimension);JButton jbutton =newJButton("归还");
        jbutton.addActionListener(newActionListener(){@OverridepublicvoidactionPerformed(ActionEvent e){int i =0;try{
                    i=SqlUtils.delBook(Integer.parseInt(tid.getText()));}catch(SQLException throwables){
                    throwables.printStackTrace();}catch(ClassNotFoundException classNotFoundException){
                    classNotFoundException.printStackTrace();}if(i>0){System.out.println("归还成功");}else{System.out.println("归还失败");}}});
        jPanel.add(lid);
        jPanel.add(tid);

        jPanel.add(jbutton);
        f.add(jPanel);
        f.setSize(600,200);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);}}

管理员页面:

在这里插入图片描述

importcode6.menu.manger.AddBook;importcode6.menu.manger.DelBook;importcode6.menu.manger.SelectBook;importcode6.menu.manger.UpdateBook;importjavax.swing.*;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.sql.SQLException;publicclassMangerMenu{publicvoidui(int uid){JFrame jframe =newJFrame("管理员个人主页");
        jframe.setBounds(300,180,650,500);JPanel jPanel =newJPanel();JMenuBar jmenuBar=newJMenuBar();JMenu sf =newJMenu("查看所有书籍");JMenu af =newJMenu("添加书籍");JMenu df =newJMenu("删除书籍");JMenu uf =newJMenu("修改书籍");JMenuItem d1 =newJMenuItem("查询");JMenuItem d2 =newJMenuItem("添加");JMenuItem d3 =newJMenuItem("删除");JMenuItem d4 =newJMenuItem("修改");
        d1.addActionListener(newActionListener(){@OverridepublicvoidactionPerformed(ActionEvent e){try{newSelectBook().comTable();}catch(SQLException throwables){
                    throwables.printStackTrace();}catch(ClassNotFoundException classNotFoundException){
                    classNotFoundException.printStackTrace();}}});
        d2.addActionListener(newActionListener(){@OverridepublicvoidactionPerformed(ActionEvent e){newAddBook().add();}});
        d3.addActionListener(newActionListener(){@OverridepublicvoidactionPerformed(ActionEvent e){newDelBook().del();}});
        d4.addActionListener(newActionListener(){@OverridepublicvoidactionPerformed(ActionEvent e){newUpdateBook().update();}});
        af.add(d2);
        df.add(d3);
        sf.add(d1);
        uf.add(d4);

        jmenuBar.add(sf);
        jmenuBar.add(af);
        jmenuBar.add(df);
        jmenuBar.add(uf);
        jPanel.add(jmenuBar);
        jframe.add(jPanel);
        jframe.setVisible(true);
        jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);}}

添加书籍功能类:

在这里插入图片描述

importcode6.entity.Book;importcode6.utils.SqlUtils;importjavax.swing.*;importjava.awt.*;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.sql.SQLException;publicclassAddBook{publicstaticvoidadd(){JFrame f =newJFrame("添加书籍");JPanel jPanel =newJPanel();JTextField textfield =newJTextField();JTextField tnum =newJTextField();JTextField tbrrow =newJTextField();Dimension dimension =newDimension(100,30);JLabel lname=newJLabel("书籍名称");JLabel lnum=newJLabel("总数量");JLabel lbrrow=newJLabel("借出数量");JLabel lgroup=newJLabel("书籍类型");
        lname.setFont(newFont("宋体",Font.PLAIN,14));
        lnum.setFont(newFont("宋体",Font.PLAIN,14));
        lgroup.setFont(newFont("宋体",Font.PLAIN,14));
        lbrrow.setFont(newFont("宋体",Font.PLAIN,14));
        tnum.setPreferredSize(dimension);
        tbrrow.setPreferredSize(dimension);
        textfield.setPreferredSize(dimension);JButton jbutton =newJButton("添加");JComboBox<String> box =newJComboBox<>();
        box.addItem("小说");
        box.addItem("教材");
        box.addItem("科普");
        box.addItem("其他");
        jbutton.addActionListener(newActionListener(){@OverridepublicvoidactionPerformed(ActionEvent e){String bname=null;int allnum=0;int brrow=0;String type=null;
                bname=textfield.getText();
                allnum=Integer.parseInt(tnum.getText());
                brrow=Integer.parseInt(tbrrow.getText());
                type=box.getSelectedItem().toString();Book book =newBook(bname,null,allnum,brrow,type);int i=0;try{
                   i=SqlUtils.addBook(book);}catch(SQLException throwables){
                    throwables.printStackTrace();}catch(ClassNotFoundException classNotFoundException){
                    classNotFoundException.printStackTrace();}if(i>0){System.out.println("添加成功");}else{System.out.println("添加失败");}}});

        jPanel.add(lname);
        jPanel.add(textfield);
        jPanel.add(lnum);
        jPanel.add(tnum);
        jPanel.add(lbrrow);
        jPanel.add(tbrrow);
        jPanel.add(lgroup);
        jPanel.add(box);
        jPanel.add(jbutton);
        f.add(jPanel);
        f.setSize(700,300);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);}}

点击查看所有书籍:

在这里插入图片描述

importcode6.entity.Book;importcode6.utils.SqlUtils;importjavax.swing.*;importjava.awt.*;importjava.sql.SQLException;importjava.util.List;publicclassSelectBook{publicstaticvoidcomTable()throwsSQLException,ClassNotFoundException{JFrame f =newJFrame("图书管理");
        f.setSize(400,300);
        f.setLocation(200,200);
        f.setLayout(newBorderLayout());String[] th =newString[]{"书籍id","书籍名称","类型","总数","借出数量"};List<Book> all =SqlUtils.getAll();String[][] td=newString[all.size()][];Book book =newBook();for(int i=0;i<td.length;i++){
            td[i]=newString[5];
            book=all.get(i);
            td[i][0]=String.valueOf(book.getBid());
            td[i][1]=book.getBname();
            td[i][2]=book.getType();
            td[i][3]=String.valueOf(book.getAllnum());
            td[i][4]=String.valueOf(book.getBorrownum());}JTable t =newJTable(td, th);JScrollPane sp =newJScrollPane(t);
        f.add(sp,BorderLayout.CENTER);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);}}

删除书籍

在这里插入图片描述

importcode6.utils.SqlUtils;importjavax.swing.*;importjava.awt.*;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.sql.SQLException;publicclassDelBook{publicvoiddel(){JFrame f =newJFrame("删除图书");JPanel jPanel =newJPanel();JLabel lid=newJLabel("图书id");
            lid.setFont(newFont("宋体",Font.PLAIN,14));JTextField tid =newJTextField();Dimension dimension =newDimension(100,30);
            tid.setPreferredSize(dimension);JButton jbutton =newJButton("删除");
            jbutton.addActionListener(newActionListener(){@OverridepublicvoidactionPerformed(ActionEvent e){int i =0;try{
                        i=SqlUtils.delBook(Integer.parseInt(tid.getText()));}catch(SQLException throwables){
                        throwables.printStackTrace();}catch(ClassNotFoundException classNotFoundException){
                        classNotFoundException.printStackTrace();}if(i>0){System.out.println("删除成功");}else{System.out.println("删除失败");}}});
            jPanel.add(lid);
            jPanel.add(tid);

            jPanel.add(jbutton);
            f.add(jPanel);
            f.setSize(600,200);
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.setVisible(true);}}

更新书籍:

在这里插入图片描述

importcode6.entity.Book;importcode6.utils.SqlUtils;importjavax.swing.*;importjava.awt.*;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.sql.SQLException;publicclassUpdateBook{publicvoidupdate(){JFrame f =newJFrame("修改书籍");JPanel jPanel =newJPanel();JTextField textfield =newJTextField();JTextField tnum =newJTextField();JTextField tid =newJTextField();JTextField tbrrow =newJTextField();Dimension dimension =newDimension(100,30);JLabel lbid =newJLabel("要修改的图书id");JLabel lname=newJLabel("书籍名称");JLabel lnum=newJLabel("总数量");JLabel lbrrow=newJLabel("借出数量");JLabel lgroup=newJLabel("书籍类型");
        lbid.setFont(newFont("宋体",Font.PLAIN,14));
        lname.setFont(newFont("宋体",Font.PLAIN,14));
        lnum.setFont(newFont("宋体",Font.PLAIN,14));
        lgroup.setFont(newFont("宋体",Font.PLAIN,14));
        lbrrow.setFont(newFont("宋体",Font.PLAIN,14));
        tnum.setPreferredSize(dimension);
        tbrrow.setPreferredSize(dimension);
        textfield.setPreferredSize(dimension);
        tid.setPreferredSize(dimension);JButton jbutton =newJButton("修改");JComboBox<String> box =newJComboBox<>();
        box.addItem("小说");
        box.addItem("教材");
        box.addItem("科普");
        box.addItem("其他");
        jbutton.addActionListener(newActionListener(){@OverridepublicvoidactionPerformed(ActionEvent e){int bid=0;String bname=null;int allnum=0;int brrow=0;String type=null;
                bid=Integer.parseInt(tid.getText());
                bname=textfield.getText();
                allnum=Integer.parseInt(tnum.getText());
                brrow=Integer.parseInt(tbrrow.getText());
                type=box.getSelectedItem().toString();Book book =newBook();
                book.setBid(bid);
                book.setBorrownum(brrow);
                book.setType(type);
                book.setBname(bname);
                book.setAllnum(allnum);System.out.println(book);int i=0;try{SqlUtils.upBook(book);}catch(SQLException throwables){
                    throwables.printStackTrace();}catch(ClassNotFoundException classNotFoundException){
                    classNotFoundException.printStackTrace();}if(i>0){System.out.println("修改成功");}else{System.out.println("修改失败");}}});
        jPanel.add(lbid);
        jPanel.add(tid);
        jPanel.add(lname);
        jPanel.add(textfield);
        jPanel.add(lnum);
        jPanel.add(tnum);
        jPanel.add(lbrrow);
        jPanel.add(tbrrow);
        jPanel.add(lgroup);
        jPanel.add(box);
        jPanel.add(jbutton);
        f.add(jPanel);
        f.setSize(700,300);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);}publicstaticvoidmain(String[] args){newUpdateBook().update();}}

到此完成

标签: java mysql

本文转载自: https://blog.csdn.net/m0_52889702/article/details/124905337
版权归原作者 小那么小小猿 所有, 如有侵权,请联系我们删除。

“图书管理系统 ——mysql数据库”的评论:

还没有评论