zoukankan      html  css  js  c++  java
  • 团队作业六

    1、系统框架

       Java jdk+ eclipse + mysql数据库

    2、系统结构图

      

    3、各模块详细设计

      由于这次我们的项目主要是用来熟悉软件工程的流程,所以难度不大。主要模块介绍如下:

      1、帐号

    package cn.edu.sau.model;
    
    public class User {
        private int id;
        private String userName;
        private String userPassword;
        private int role = 0; //默认为0->读者 、 1->图书管理员
        private String pname;
        
        public User(){
            this.id = -1;
        }
        
        public User(int id, String userName) {
            super();
            this.id = id;
            this.userName = userName;
        }
    
        public User(String userName, String userPassword) {
            super();
            this.userName = userName;
            this.userPassword = userPassword;
        }
        
        public User(int id,String userName, String userPassword, int role,String pname) {
            super();
            this.id = id;
            this.userName = userName;
            this.userPassword = userPassword;
            this.role = role;
            this.pname = pname;
        }
    
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
        public String getUserName() {
            return userName;
        }
        public void setUserName(String userName) {
            this.userName = userName;
        }
        public String getUserPassword() {
            return userPassword;
        }
        public void setUserPassword(String userPassword) {
            this.userPassword = userPassword;
        }
    
        public int getRole() {
            return role;
        }
    
        public void setRole(int role) {
            this.role = role;
        }
    
        public String getPname() {
            return pname;
        }
    
        public void setPname(String pname) {
            this.pname = pname;
        }
        
    }
    cn.edu.sau.model.User

      User类实现了对帐号的抽象描述,主要包括用户名,密码,帐号角色以及账号所有人名称。

     1 package cn.edu.sau.dao;
     2 
     3 import java.sql.Connection;
     4 import java.sql.PreparedStatement;
     5 import java.sql.ResultSet;
     6 
     7 import cn.edu.sau.model.User;
     8 
     9 public class UserDao {
    10     public User login(Connection con,User user) throws Exception{
    11         User returnUser = null;
    12         String sql = "select * from t_user where userName=? and password=?";
    13         PreparedStatement pstmt = con.prepareStatement(sql);
    14         pstmt.setString(1, user.getUserName());
    15         pstmt.setString(2, user.getUserPassword());
    16         ResultSet rs = pstmt.executeQuery();
    17         if(rs.next()){
    18             returnUser = new User(rs.getString("userName"),rs.getString("password"),rs.getInt("role"));
    19         }
    20         return returnUser;
    21     }
    22 }
    cn.edu.sau.dao.UserDao

      UserDao提供帐号验证的方法并返回帐号对象,为空则帐号不存在。

    package cn.edu.sau.view;
    
    import java.awt.EventQueue;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.sql.Connection;
    import java.sql.SQLException;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.JPasswordField;
    import javax.swing.JTextField;
    import javax.swing.border.EmptyBorder;
    
    
    import cn.edu.sau.dao.UserDao;
    import cn.edu.sau.model.User;
    import cn.edu.sau.util.DbUtil;
    import cn.edu.sau.util.StringUtil;
    import javax.swing.ImageIcon;
    
    public class LogonFrm extends JFrame {
    
        private JPanel contentPane;
        private JTextField userNameTxt;
        private JPasswordField userPasswordTxt;
    
        /**
         * Launch the application.
         */
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
                public void run() {
                    try {
                        LogonFrm frame = new LogonFrm();
                        frame.setVisible(true);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        }
    
        /**
         * Create the frame.
         */
        public LogonFrm() {
            setTitle("u56FEu4E66u7BA1u7406u7CFBu7EDF");
            UserDao userDao = new UserDao();
            DbUtil dbUtil = new DbUtil();
            
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setBounds(100, 100, 450, 300);
            //设置frame居中
            this.setLocationRelativeTo(null);
            contentPane = new JPanel();
            contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
            setContentPane(contentPane);
            contentPane.setLayout(null);
            init();
        }
        private void reset(){
            userNameTxt.setText("");
            userPasswordTxt.setText("");
        }
        private void init() {
            DbUtil dbUtil = new DbUtil();
            UserDao userDao = new UserDao();
            JLabel lb_userName = new JLabel("u7528u6237u540DuFF1A");
            lb_userName.setBounds(102, 75, 54, 15);
            contentPane.add(lb_userName);
            
            userNameTxt = new JTextField();
            userNameTxt.setBounds(166, 72, 152, 21);
            contentPane.add(userNameTxt);
            userNameTxt.setColumns(10);
            
            JLabel lb_userPassword = new JLabel("u5BC6  u7801uFF1A");
            lb_userPassword.setBounds(102, 129, 54, 15);
            contentPane.add(lb_userPassword);
            
            JButton jb_login = new JButton("u767Bu5F55");
            jb_login.setIcon(new ImageIcon("D:\java\MyBookManager\image\login.png"));
            jb_login.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    String userName = userNameTxt.getText();
                    String userPassword = new String(userPasswordTxt.getPassword());
                    if(StringUtil.isEmpty(userName)){
                        JOptionPane.showMessageDialog(null, "用户名不能为空");
                        return;
                    }
                    if(StringUtil.isEmpty(userPassword)){
                        JOptionPane.showMessageDialog(null, "密码不能为空");
                        return;
                    }
                    User user = new User(userName,userPassword);
                    Connection con = null;
                    
                    try {
                        con = dbUtil.getCon();
                        User reUser = userDao.login(con, user);
                        if(reUser != null){
                            dispose();//销毁登录框
                            if(1 == reUser.getRole()){
                                new MainFrm().setVisible(true);//创建管理员主界面Firme
                            }
                            else if(0 == reUser.getRole()){
                                 new UserMainFrm(reUser).setVisible(true);
                            }
                        }else{
                            JOptionPane.showMessageDialog(null, "用户名或密码不正确");
                        }
                    } catch (Exception e1) {
                        JOptionPane.showMessageDialog(null, "登录失败");
                        e1.printStackTrace();
                    } finally{
                        try {
                            con.close();
                        } catch (SQLException e1) {
                            e1.printStackTrace();
                        }
                    }
                    
                }
            });
            jb_login.setBounds(102, 191, 93, 23);
            contentPane.add(jb_login);
            
            JButton jb_reset = new JButton("u91CDu7F6E");
            jb_reset.setIcon(new ImageIcon("D:\java\MyBookManager\image\reset.png"));
            jb_reset.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    reset();
                }
            });
            jb_reset.setBounds(225, 191, 93, 23);
            contentPane.add(jb_reset);
            
            userPasswordTxt = new JPasswordField();
            userPasswordTxt.setBounds(166, 126, 152, 21);
            contentPane.add(userPasswordTxt);
            
            JLabel lblNewLabel = new JLabel(" ");
            lblNewLabel.setIcon(new ImageIcon("D:\java\MyBookManager\image\logo.png"));
            lblNewLabel.setBounds(182, 10, 99, 52);
            contentPane.add(lblNewLabel);
        }
    }
    cn.edu.sau.view.LogonFrm

      登录框界面通过验证逻辑层返回的帐号数据判断帐号是否存在,以及判断帐号权限,打开新的界面。

      2、图书类别维护

    package cn.edu.sau.model;
    
    public class BookType {
        private int id;
        private String bookTypeName;
        private String bookTypeDesc;
        
        
        public BookType() {
            super();
        }
        
        public BookType(String bookTypeName, String bookTypeDesc) {
            super();
            this.bookTypeName = bookTypeName;
            this.bookTypeDesc = bookTypeDesc;
        }
    
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
        public String getBookTypeName() {
            return bookTypeName;
        }
        public void setBookTypeName(String bookTypeName) {
            this.bookTypeName = bookTypeName;
        }
        public String getBookTypeDesc() {
            return bookTypeDesc;
        }
        public void setBookTypeDesc(String bookTypeDesc) {
            this.bookTypeDesc = bookTypeDesc;
        }
    
        @Override
        public String toString() {
            return this.getBookTypeName();
        }
        
        
    }
    cn.edu.sau.model.BookType

       BookType类实现了对图书类别的对象描述,主要有图书类别名称以及图书类别的描述。

    package cn.edu.sau.dao;
    
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    
    import cn.edu.sau.model.BookType;
    import cn.edu.sau.util.StringUtil;
    
    public class BookTypeDao {
        public int bookTypeAdd(Connection con, BookType bookType) throws Exception{
            String sql = "insert into t_booktype values(null,?,?)";
            PreparedStatement pstmt = con.prepareStatement(sql);
            pstmt.setString(1,bookType.getBookTypeName());
            pstmt.setString(2,bookType.getBookTypeDesc());
            return pstmt.executeUpdate();
        }
        
        public ResultSet bookTypeList(Connection con , BookType bookType) throws Exception{
            StringBuffer sb = new StringBuffer("select * from t_booktype");
            String bookTypeName = bookType.getBookTypeName();
            if(!StringUtil.isEmpty(bookTypeName)){
                sb.append(" and bookTypeName like '%"+bookType.getBookTypeName()+"%'");
            }
            PreparedStatement pstmt = con.prepareStatement(sb.toString().replaceAll("and", "where"));
            return pstmt.executeQuery();
        }
        
        public int bookTypeModify(Connection con,BookType bookType) throws Exception{
            String sql = "update t_bookType set booktypename=? , booktypedesc=? where id=?";
            PreparedStatement pstmt = con.prepareStatement(sql);
            pstmt.setString(1, bookType.getBookTypeName());
            pstmt.setString(2, bookType.getBookTypeDesc());
            pstmt.setInt(3, bookType.getId());
            return pstmt.executeUpdate();
        }
        
        public int bookTypeDelete(Connection con,String id) throws Exception{
            String sql = "delete from t_bookType where id=?";
            PreparedStatement pstmt = con.prepareStatement(sql);
            pstmt.setString(1,id);
            return pstmt.executeUpdate();
        }
    }
    cn.edu.sau.dao.BookTypeDao

      BookTypeDao类提供了添加图书类别,获取图书类别列表,更改图书类别以及删除图书类别方法。

                    String bookTypeName = bookTypeNameTxt.getText();
                    String bookTypeDesc = bookTypeDescTxt.getText();
                    if(StringUtil.isEmpty(bookTypeName)){
                        JOptionPane.showMessageDialog(null,"图书类别名称不能为空");
                    }else{
                        Connection con = null;
                        
                        try {
                            con = dbUtil.getCon();
                            BookType bookType = new BookType(bookTypeName,bookTypeDesc);
                            int ret = bookTypeDao.bookTypeAdd(con, bookType);
                            if(1 == ret){
                                JOptionPane.showMessageDialog(null, "图书类别添加成功");
                                reset();
                            }else{
                                JOptionPane.showMessageDialog(null, "图书类别添加失败");
                            }
                        } catch (Exception e1) {
                            JOptionPane.showMessageDialog(null, "图书类别添加失败");
                            e1.printStackTrace();
                        }
                    }
                }
    cn.edu.sau.view.BookTypeAddInterFrm

      图书类别添加界面获取用户输入,进行图书类别的添加。

    private void fillTable(BookType bookType){
            DefaultTableModel dtm = (DefaultTableModel)bookTypeTable.getModel();
            dtm.setRowCount(0);
            Connection con = null;
            try {
                con = dbUtil.getCon();
                ResultSet res = bookTypeDao.bookTypeList(con, bookType);
                while(res.next()){
                    Vector v = new Vector();
                    v.add(res.getString("id"));
                    v.add(res.getString("bookTypeName"));
                    v.add(res.getString("bookTypeDesc"));
                    dtm.addRow(v);
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally{
                try {
                    dbUtil.closeCon(con);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    
    //鼠标点击表格填充内容至相应位置
    bookTypeTable.addMouseListener(new MouseAdapter() {
                @Override
                public void mousePressed(MouseEvent e) {
                    int row = bookTypeTable.getSelectedRow();
                    idTxt.setText((String)bookTypeTable.getValueAt(row, 0));
                    bookTypeNameTxt.setText((String)bookTypeTable.getValueAt(row, 1));
                    bookTypeDescTxt.setText((String)bookTypeTable.getValueAt(row, 2));
                }
            });
    //修改图书类别函数体
                    String bookTypeName = bookTypeNameTxt.getText();
                    String bookTypeDesc = bookTypeDescTxt.getText();
                    if(StringUtil.isEmpty(bookTypeName)){
                        JOptionPane.showMessageDialog(null, "图书类别名称不能为空");
                    }
                    BookType bookType = new BookType(bookTypeName,bookTypeDesc);
                    bookType.setId(Integer.parseInt(idTxt.getText()));
                    Connection con = null;
                    try {
                        con = dbUtil.getCon();
                        int re = bookTypeDao.bookTypeModify(con, bookType);
                        if(1 == re){
                            JOptionPane.showMessageDialog(null, "修改成功");
                            fillTable(new BookType("",""));
                            reset();
                        }else{
                            JOptionPane.showMessageDialog(null, "修改失败");
                        }
                    } catch (Exception e1) {
                        JOptionPane.showMessageDialog(null, "修改失败");
                        e1.printStackTrace();
                    }finally{
                        try {
                            dbUtil.closeCon(con);
                        } catch (Exception e1) {
                            e1.printStackTrace();
                        }
                    }        
    //删除图书类别函数体
                    String id = idTxt.getText();
                    Connection con = null;
                    try {
                        con = dbUtil.getCon();
                        int re = bookTypeDao.bookTypeDelete(con, id);
                        if(1 == re){
                            JOptionPane.showMessageDialog(null, "删除成功");
                            fillTable(new BookType("",""));
                            reset();
                        }else{
                            JOptionPane.showMessageDialog(null, "删除失败");
                        }
                    } catch (Exception e1) {
                        JOptionPane.showMessageDialog(null, "删除失败");
                        e1.printStackTrace();
                    } finally{
                        try {
                            dbUtil.closeCon(con);
                        } catch (Exception e1) {
                            e1.printStackTrace();
                        }
                    }        
    cn.edu.sau.view.BookTypeManageInterFrm

      fillTable为图书类别管理界面函数,填充界面表格元素的内容。其余函数在代码中有注释,分别为表格的点击自动填充、修改图书类别、删除图书类别代码实现

       3、图书维护

    package cn.edu.sau.model;
    /**
     * 图书类
     * */
    public class Book {
        private int id;
        private String bookName;
        private String bookAuthor;
        private float bookPrice;
        private String bookDesc;
        private int bookTypeId;
        private String bookTypeName;
        
        public Book() {
            super();
            this.id = -1;
            this.bookName = "";
            this.bookAuthor = "";
            this.bookPrice = 0;
            this.bookDesc = "";
            this.bookTypeId = -1;
        }
        
        public Book(int id, String bookName) {
            super();
            this.id = id;
            this.bookName = bookName;
        }
    
        public Book(int id, String bookName, String bookAuthor, float bookPrice,
                String bookDesc, int bookTypeId) {
            super();
            this.id = id;
            this.bookName = bookName;
            this.bookAuthor = bookAuthor;
            this.bookPrice = bookPrice;
            this.bookDesc = bookDesc;
            this.bookTypeId = bookTypeId;
        }
    
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
        public String getBookName() {
            return bookName;
        }
        public void setBookName(String bookName) {
            this.bookName = bookName;
        }
        public String getBookAuthor() {
            return bookAuthor;
        }
        public void setBookAuthor(String bookAuthor) {
            this.bookAuthor = bookAuthor;
        }
        public float getBookPrice() {
            return bookPrice;
        }
        public void setBookPrice(float bookPrice) {
            this.bookPrice = bookPrice;
        }
        public String getBookDesc() {
            return bookDesc;
        }
        public void setBookDesc(String bookDesc) {
            this.bookDesc = bookDesc;
        }
        public int getBookTypeId() {
            return bookTypeId;
        }
        public void setBookTypeId(int bookTypeId) {
            this.bookTypeId = bookTypeId;
        }
        public String getBookTypeName() {
            return bookTypeName;
        }
        public void setBookTypeName(String bookTypeName) {
            this.bookTypeName = bookTypeName;
        }
    }
    cn.edu.sau.model.Book

      该类为图书的抽象类,完成了对图书的抽象描述。

    package cn.edu.sau.dao;
    
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    
    import cn.edu.sau.model.Book;
    import cn.edu.sau.model.BookType;
    import cn.edu.sau.util.StringUtil;
    
    
    public class BookDao {
        public static int bookAdd(Connection con , Book book) throws Exception{
            String sql = "insert into t_book values(null,?,?,?,?,?)";
            PreparedStatement pstmt = con.prepareStatement(sql);
            pstmt.setString(1,book.getBookName());
            pstmt.setString(2,book.getBookAuthor());
            pstmt.setFloat(3,book.getBookPrice());
            pstmt.setString(4,book.getBookDesc());
            pstmt.setInt(5,book.getBookTypeId());
            return pstmt.executeUpdate();    
        }
        
        public ResultSet bookList(Connection con,Book book)throws Exception{
            StringBuffer sb=new StringBuffer("select * from t_book b,t_bookType bt where b.bookTypeId=bt.id");
            if(StringUtil.isNotEmpty(book.getBookName())){
                sb.append(" and b.bookName like '%"+book.getBookName()+"%'");
            }
            if(StringUtil.isNotEmpty(book.getBookAuthor())){
                sb.append(" and b.author like '%"+book.getBookAuthor()+"%'");
            }
            
            if(book.getBookTypeId()!=-1){
                sb.append(" and b.bookTypeId = "+book.getBookTypeId());
            }
            PreparedStatement pstmt=con.prepareStatement(sb.toString());
            return pstmt.executeQuery();
        }
        
        public int bookDelete(Connection con,String id)throws Exception{
            String sql="delete from t_book where id=?";
            PreparedStatement pstmt=con.prepareStatement(sql);
            pstmt.setString(1, id);
            return pstmt.executeUpdate();
        }
        
        public int bookModify(Connection con,Book book) throws Exception{
                String sql="update t_book set bookName=?,author=?,price=?,bookDesc=?,bookTypeId=? where id=?";
                PreparedStatement pstmt=con.prepareStatement(sql);
                pstmt.setString(1, book.getBookName());
                pstmt.setString(2, book.getBookAuthor());
                pstmt.setFloat(3, book.getBookPrice());
                pstmt.setString(4, book.getBookDesc());
                pstmt.setInt(5, book.getBookTypeId());
                pstmt.setInt(6, book.getId());
                return pstmt.executeUpdate();
        }
        
        public boolean getBookByBookTypeId(Connection con,String bookTypeId)throws Exception{
            String sql="select * from t_book where bookTypeId=?";
            PreparedStatement pstmt=con.prepareStatement(sql);
            pstmt.setString(1, bookTypeId);
            ResultSet rs=pstmt.executeQuery();
            return rs.next();
        }
    }
    cn.edu.sau.dao.BookDao

      该类和图书类别维护的Dao层类相似,实现了图书添加,图书类别,图书修改以及图书删除的方法。

    package cn.edu.sau.view;
    
    import java.awt.EventQueue;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.sql.Connection;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    
    import javax.swing.JButton;
    import javax.swing.JComboBox;
    import javax.swing.JInternalFrame;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    import javax.swing.JTextArea;
    import javax.swing.JTextField;
    
    import cn.edu.sau.dao.BookDao;
    import cn.edu.sau.dao.BookTypeDao;
    import cn.edu.sau.model.Book;
    import cn.edu.sau.model.BookType;
    import cn.edu.sau.util.DbUtil;
    import cn.edu.sau.util.StringUtil;
    import javax.swing.ImageIcon;
    
    public class BookAddInterFrm extends JInternalFrame {
        DbUtil dbUtil = new DbUtil();
        BookTypeDao bookTypeDao = new BookTypeDao();
        BookDao bookDao = new BookDao();
        
        private JTextField bookNameTxt;
        private JTextField bookAuthorTxt;
        private JTextField bookPriceTxt;
        private JComboBox jcb_bookType;
        private JTextArea bookDescTxt;
    
        /**
         * Launch the application.
         */
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
                public void run() {
                    try {
                        BookAddInterFrm frame = new BookAddInterFrm();
                        frame.setVisible(true);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        }
    
        /**
         * Create the frame.
         */
        public BookAddInterFrm() {
            
            
            
            setClosable(true);
            setIconifiable(true);
            setTitle("u56FEu4E66u6DFBu52A0");
            setBounds(100, 100, 474, 443);
            setLocation(500, 100);
            getContentPane().setLayout(null);
            
            init();
            fillBookType();
    
        }
        
        
        private void init() {
            JLabel lb_bookName = new JLabel("u56FEu4E66u540Du79F0uFF1A");
            lb_bookName.setBounds(29, 39, 72, 15);
            getContentPane().add(lb_bookName);
            
            bookNameTxt = new JTextField();
            bookNameTxt.setBounds(112, 36, 106, 21);
            getContentPane().add(bookNameTxt);
            bookNameTxt.setColumns(10);
            
            JLabel lb_bookAuthor = new JLabel("u56FEu4E66u4F5Cu8005uFF1A");
            lb_bookAuthor.setBounds(236, 39, 72, 15);
            getContentPane().add(lb_bookAuthor);
            
            bookAuthorTxt = new JTextField();
            bookAuthorTxt.setBounds(304, 36, 101, 21);
            getContentPane().add(bookAuthorTxt);
            bookAuthorTxt.setColumns(10);
            
            JLabel lb_bookPrice = new JLabel("u56FEu4E66u4EF7u683CuFF1A");
            lb_bookPrice.setBounds(29, 100, 72, 15);
            getContentPane().add(lb_bookPrice);
            
            bookPriceTxt = new JTextField();
            bookPriceTxt.setBounds(112, 97, 106, 21);
            getContentPane().add(bookPriceTxt);
            bookPriceTxt.setColumns(10);
            
            JLabel lb_bookType = new JLabel("u56FEu4E66u7C7Bu522BuFF1A");
            lb_bookType.setBounds(236, 100, 74, 15);
            getContentPane().add(lb_bookType);
            
            jcb_bookType = new JComboBox();
            jcb_bookType.setBounds(304, 97, 101, 21);
            getContentPane().add(jcb_bookType);
            
            JLabel lb_bookDesc = new JLabel("u56FEu4E66u7B80u4ECBuFF1A");
            lb_bookDesc.setBounds(28, 176, 73, 15);
            getContentPane().add(lb_bookDesc);
            
            bookDescTxt = new JTextArea();
            bookDescTxt.setBounds(112, 172, 297, 127);
            getContentPane().add(bookDescTxt);
            
            JButton jb_bookAdd = new JButton("u6DFBu52A0");
            jb_bookAdd.setIcon(new ImageIcon("D:\java\MyBookManager\image\add.png"));
            jb_bookAdd.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    String bookName = bookNameTxt.getText();
                    String bookAuthor = bookAuthorTxt.getText();
                    String bookPrice = bookPriceTxt.getText();
                    String bookDesc = bookDescTxt.getText();
                    if(StringUtil.isEmpty(bookName)){
                        JOptionPane.showMessageDialog(null, "图书名称不能为空");
                        return;
                    }
                    if(StringUtil.isEmpty(bookAuthor)){
                        JOptionPane.showMessageDialog(null, "图书作者不能为空");
                        return;
                    }
                    if(StringUtil.isEmpty(bookPrice)){
                        JOptionPane.showMessageDialog(null, "图书价格不能为空");
                        return;
                    }
                    BookType bookType = (BookType)jcb_bookType.getSelectedItem();
                    int bookTypeId = bookType.getId();
                    Book book = new Book(-1,bookName,bookAuthor,Float.parseFloat(bookPrice),bookDesc,bookTypeId);
                    Connection con = null;
                    try{
                        con = dbUtil.getCon();
                        int addNum = BookDao.bookAdd(con,book);
                        if(1 == addNum){
                            JOptionPane.showMessageDialog(null, "添加成功");
                            resetValue();
                        }else{
                            JOptionPane.showMessageDialog(null, "添加失败");
                        }
                    }catch(Exception ex){
                        ex.printStackTrace();
                        JOptionPane.showMessageDialog(null, "添加失败");
                    }finally{
                        try {
                            con.close();
                        } catch (SQLException e1) {
                            e1.printStackTrace();
                        }
                    }
                }
            });
            jb_bookAdd.setBounds(115, 339, 93, 23);
            getContentPane().add(jb_bookAdd);
            
            JButton jb_bookReset = new JButton("u91CDu7F6E");
            jb_bookReset.setIcon(new ImageIcon("D:\java\MyBookManager\image\reset.png"));
            jb_bookReset.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    resetValue();
                }
            });
            jb_bookReset.setBounds(264, 339, 93, 23);
            getContentPane().add(jb_bookReset);
        }
        
        private void fillBookType(){
            Connection con = null;
            BookType bookType = null;
            try{
                con = dbUtil.getCon();
                ResultSet rs = bookTypeDao.bookTypeList(con, new BookType("",""));
                while(rs.next()){
                    bookType = new BookType();
                    bookType.setId(rs.getInt("id"));
                    bookType.setBookTypeName(rs.getString("bookTypeName"));
                    jcb_bookType.addItem(bookType);
                }
            }catch(Exception ex){
                ex.printStackTrace();
            }finally{
                try {
                    con.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        
        private void resetValue(){
            bookNameTxt.setText("");
            bookAuthorTxt.setText("");
            bookPriceTxt.setText("");
            jcb_bookType.setSelectedIndex(0);
            bookDescTxt.setText("");
        }
    }
    cn.edu.sau.view.BookAddInterFrm

      界面中提供了添加图书的具体实现,以及其中将图书类别添加进下拉框的方法。

    package cn.edu.sau.view;
    
    import java.awt.Component;
    import java.awt.EventQueue;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.sql.Connection;
    import java.sql.ResultSet;
    import java.util.Vector;
    
    import javax.swing.JButton;
    import javax.swing.JComboBox;
    import javax.swing.JInternalFrame;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.JTextArea;
    import javax.swing.JTextField;
    import javax.swing.border.TitledBorder;
    import javax.swing.table.DefaultTableModel;
    
    import org.eclipse.wb.swing.FocusTraversalOnArray;
    
    import cn.edu.sau.dao.BookDao;
    import cn.edu.sau.dao.BookTypeDao;
    import cn.edu.sau.model.Book;
    import cn.edu.sau.model.BookType;
    import cn.edu.sau.util.DbUtil;
    import cn.edu.sau.util.StringUtil;
    
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import javax.swing.ImageIcon;
    
    public class BookManageInterFrm extends JInternalFrame {
        DbUtil dbUtil = new DbUtil();
        BookTypeDao bookTypeDao = new BookTypeDao();
        BookDao bookDao = new BookDao();
        
        
        private JTextField s_bookNameTxt;
        private JTextField s_bookAuthorTxt;
        private JTable bookTable;
        private JTextField idTxt;
        private JTextField bookNameTxt;
        private JTextField bookAuthorTxt;
        private JTextField bookPriceTxt;
        private JTextArea bookDescTxt;
        private JScrollPane scrollPane;
        private JComboBox<BookType> s_jcbBookType;
        private JComboBox<BookType> jcb_bookType;
    
        /**
         * Launch the application.
         */
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
                public void run() {
                    try {
                        BookManageInterFrm frame = new BookManageInterFrm();
                        frame.setVisible(true);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        }
    
        /**
         * Create the frame.
         */
        public BookManageInterFrm() {
            setClosable(true);
            setIconifiable(true);
            setTitle("u56FEu4E66u7BA1u7406");
            setBounds(100, 100, 812, 618);
            setLocation(300, 20);
            getContentPane().setLayout(null);
            
            init();
            
            fillTable(new Book());
            this.fillBookType("search");
            this.fillBookType("modify");
    
    
        }
    
        private void fillBookType(String type) {
            Connection con = null;
            BookType bookType = null;
            try {
                con = dbUtil.getCon();
                ResultSet rs = bookTypeDao.bookTypeList(con, new BookType());
                if ("search".equals(type)) {
                    bookType = new BookType();
                    bookType.setBookTypeName("请选择...");
                    bookType.setId(-1);
                    this.s_jcbBookType.addItem(bookType);
                }
                while (rs.next()) {
                    bookType = new BookType();
                    bookType.setId(rs.getInt("id"));
                    bookType.setBookTypeName(rs.getString("bookTypeName"));
                    if ("search".equals(type)) {
                        this.s_jcbBookType.addItem(bookType);
                    } else if ("modify".equals(type)) {
                        this.jcb_bookType.addItem(bookType);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    dbUtil.closeCon(con);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        
        private void fillTable(Book book) {
            DefaultTableModel dtm = (DefaultTableModel)bookTable.getModel();    
            dtm.setRowCount(0);
            Connection con = null;
            try {
                con = dbUtil.getCon();
                ResultSet rs = bookDao.bookList(con, book);
                while (rs.next()) {
                    Vector v = new Vector();
                    v.add(rs.getInt("id"));
                    v.add(rs.getString("bookName"));
                    v.add(rs.getString("author"));
                    v.add(rs.getFloat("price"));
                    v.add(rs.getString("bookDesc"));
                    v.add(rs.getString("bookTypeName"));
                    dtm.addRow(v);
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    dbUtil.closeCon(con);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        
        private void init() {
            scrollPane = new JScrollPane();
            scrollPane.setToolTipText("");
            scrollPane.setBounds(30, 91, 732, 178);
            getContentPane().add(scrollPane);
            
            bookTable = new JTable();
            bookTable.addMouseListener(new MouseAdapter() {
                @Override
                public void mousePressed(MouseEvent e) {
                    int row = bookTable.getSelectedRow();
                    idTxt.setText((Integer)bookTable.getValueAt(row, 0)+"");
                    bookNameTxt.setText((String) bookTable.getValueAt(row, 1));
                    bookAuthorTxt.setText((String) bookTable.getValueAt(row, 2));
                    bookPriceTxt.setText((Float) bookTable.getValueAt(row, 3) + "");
                    bookDescTxt.setText(bookTable.getValueAt(row, 4) + "");
                    String bookTypeName = (String) bookTable.getValueAt(row, 5);
                    int n = jcb_bookType.getItemCount();
                    for (int i = 0; i < n; i++) {
                        BookType item = (BookType) jcb_bookType.getItemAt(i);
                        if (item.getBookTypeName().equals(bookTypeName)) {
                            jcb_bookType.setSelectedIndex(i);
                        }
                    }
                }
            });
            bookTable.setToolTipText("u56FEu4E66");
            bookTable.setSurrendersFocusOnKeystroke(true);
            bookTable.setModel(new DefaultTableModel(
                new Object[][] {
                    {null, null, null, null, null, null},
                },
                new String[] {
                    "u56FEu4E66u7F16u53F7", "u56FEu4E66u540Du79F0", "u56FEu4E66u4F5Cu8005", "u56FEu4E66u4EF7u683C", "u56FEu4E66u63CFu8FF0", "u56FEu4E66u7C7Bu522B"
                }
            ) {
                boolean[] columnEditables = new boolean[] {
                    true, false, false, false, false, false
                };
                public boolean isCellEditable(int row, int column) {
                    return columnEditables[column];
                }
            });
            bookTable.getColumnModel().getColumn(1).setPreferredWidth(100);
            bookTable.getColumnModel().getColumn(2).setPreferredWidth(92);
            bookTable.getColumnModel().getColumn(3).setPreferredWidth(94);
            bookTable.getColumnModel().getColumn(4).setPreferredWidth(164);
            bookTable.getColumnModel().getColumn(5).setPreferredWidth(97);
            scrollPane.setViewportView(bookTable);
            
            JPanel panel = new JPanel();
            panel.setBorder(new TitledBorder(null, "u641Cu7D22u6761u4EF6", TitledBorder.LEADING, TitledBorder.TOP, null, null));
            panel.setToolTipText("u641Cu7D22u6761u4EF6");
            panel.setBounds(31, 14, 732, 68);
            getContentPane().add(panel);
            panel.setLayout(null);
            
            JLabel label = new JLabel("u56FEu4E66u7C7Bu522BuFF1A
    ");
            label.setBounds(410, 29, 72, 15);
            panel.add(label);
            
            JLabel lblNewLabel_1 = new JLabel("u56FEu4E66u4F5Cu8005uFF1A");
            lblNewLabel_1.setBounds(209, 29, 72, 15);
            panel.add(lblNewLabel_1);
            
            JLabel lblNewLabel = new JLabel("u56FEu4E66u540Du79F0uFF1A");
            lblNewLabel.setBounds(10, 29, 67, 15);
            panel.add(lblNewLabel);
            
            s_bookNameTxt = new JTextField();
            s_bookNameTxt.setBounds(84, 26, 114, 21);
            panel.add(s_bookNameTxt);
            s_bookNameTxt.setColumns(10);
            
            s_bookAuthorTxt = new JTextField();
            s_bookAuthorTxt.setBounds(285, 26, 114, 21);
            panel.add(s_bookAuthorTxt);
            s_bookAuthorTxt.setColumns(10);
            
            JButton jb_search = new JButton("u67E5u8BE2");
            jb_search.setIcon(new ImageIcon("D:\java\MyBookManager\image\search.png"));
            jb_search.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    Book book = new Book();
                    String bookName = s_bookNameTxt.getText();
                    String bookAuthor = s_bookAuthorTxt.getText();
                    
                    BookType bookType = (BookType)s_jcbBookType.getSelectedItem();
                    int bookTypeId = bookType.getId();
                    
                    
                    book.setBookName(bookName);
                    book.setBookAuthor(bookAuthor);
                    book.setBookTypeId(bookTypeId);
                    fillTable(book);
                }
            });
            jb_search.setBounds(629, 25, 93, 23);
            panel.add(jb_search);
            
            s_jcbBookType = new JComboBox<BookType>();
            s_jcbBookType.setBounds(472, 26, 131, 21);
            panel.add(s_jcbBookType);
            panel.setFocusTraversalPolicy(new FocusTraversalOnArray(new Component[]{label, lblNewLabel_1, lblNewLabel, s_bookNameTxt, s_bookAuthorTxt, jb_search, s_jcbBookType}));
            
            JPanel panel_1 = new JPanel();
            panel_1.setBorder(new TitledBorder(null, "u8868u5355u64CDu4F5C", TitledBorder.LEADING, TitledBorder.TOP, null, null));
            panel_1.setToolTipText("u8868u5355u64CDu4F5C");
            panel_1.setBounds(32, 283, 732, 274);
            getContentPane().add(panel_1);
            panel_1.setLayout(null);
            
            JLabel label_1 = new JLabel("u7F16u53F7uFF1A");
            label_1.setBounds(10, 41, 57, 15);
            panel_1.add(label_1);
            
            idTxt = new JTextField();
            idTxt.setEditable(false);
            idTxt.setBounds(96, 38, 118, 21);
            panel_1.add(idTxt);
            idTxt.setColumns(10);
            
            JLabel label_2 = new JLabel("u56FEu4E66u540Du79F0uFF1A");
            label_2.setBounds(10, 81, 76, 15);
            panel_1.add(label_2);
            
            bookNameTxt = new JTextField();
            bookNameTxt.setBounds(96, 78, 118, 21);
            panel_1.add(bookNameTxt);
            bookNameTxt.setColumns(10);
            
            JLabel label_3 = new JLabel("u56FEu4E66u4F5Cu8005uFF1A");
            label_3.setBounds(224, 81, 65, 15);
            panel_1.add(label_3);
            
            bookAuthorTxt = new JTextField();
            bookAuthorTxt.setColumns(10);
            bookAuthorTxt.setBounds(299, 78, 118, 21);
            panel_1.add(bookAuthorTxt);
            
            JLabel label_4 = new JLabel("u56FEu4E66u4EF7u683CuFF1A");
            label_4.setBounds(10, 160, 76, 15);
            panel_1.add(label_4);
            
            bookPriceTxt = new JTextField();
            bookPriceTxt.setBounds(96, 157, 117, 21);
            panel_1.add(bookPriceTxt);
            bookPriceTxt.setColumns(10);
            
            JLabel label_5 = new JLabel("u56FEu4E66u7C7Bu522BuFF1A");
            label_5.setBounds(224, 160, 65, 15);
            panel_1.add(label_5);
            
            jcb_bookType = new JComboBox<BookType>();
            jcb_bookType.setBounds(299, 157, 118, 21);
            panel_1.add(jcb_bookType);
            
            JLabel label_6 = new JLabel("u56FEu4E66u63CFu8FF0uFF1A");
            label_6.setBounds(439, 81, 65, 15);
            panel_1.add(label_6);
            
            bookDescTxt = new JTextArea();
            bookDescTxt.setBounds(514, 77, 208, 98);
            panel_1.add(bookDescTxt);
            
            JButton jb_modify = new JButton("u4FEEu6539");
            jb_modify.setIcon(new ImageIcon("D:\java\MyBookManager\image\modify.png"));
            jb_modify.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    String id = idTxt.getText();
                    if (StringUtil.isEmpty(id)) {
                        JOptionPane.showMessageDialog(null, "请选择要修改的记录!");
                        return;
                    }
                    
                    String bookName=bookNameTxt.getText();
                    String author=bookAuthorTxt.getText();
                    String price=bookPriceTxt.getText();
                    String bookDesc=bookDescTxt.getText();
                    
                    if(StringUtil.isEmpty(bookName)){
                        JOptionPane.showMessageDialog(null, "图书名称不能为空!");
                        return;
                    }
                    if(StringUtil.isEmpty(author)){
                        JOptionPane.showMessageDialog(null, "图书作者不能为空!");
                        return;
                    }
                    if(StringUtil.isEmpty(price)){
                        JOptionPane.showMessageDialog(null, "图书价格不能为空!");
                        return;
                    }
                    
                    BookType bookType=(BookType) jcb_bookType.getSelectedItem();
                    int bookTypeId=bookType.getId();
                    
                    Book book=new Book(Integer.parseInt(id),bookName,author,Float.parseFloat(price), bookDesc, bookTypeId);
                    Connection con = null;
                    try {
                        con = dbUtil.getCon();
                        int modifyNum = bookDao.bookModify(con, book);
                        if (modifyNum == 1) {
                            JOptionPane.showMessageDialog(null, "修改成功");
                            resetValue();
                            fillTable(new Book());
                        } else {
                            JOptionPane.showMessageDialog(null, "修改失败");
                        }
                    } catch (Exception ex) {
                        ex.printStackTrace();
                        JOptionPane.showMessageDialog(null, "修改失败");
                    } finally {
                        try {
                            dbUtil.closeCon(con);
                        } catch (Exception ec) {
                            ec.printStackTrace();
                        }
                    }
                }
            });
            jb_modify.setBounds(66, 225, 93, 23);
            panel_1.add(jb_modify);
            
            JButton jb_delete = new JButton("u5220u9664");
            jb_delete.setIcon(new ImageIcon("D:\java\MyBookManager\image\delete.png"));
            jb_delete.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    String id = idTxt.getText();
                    if (StringUtil.isEmpty(id)) {
                        JOptionPane.showMessageDialog(null, "请选择要删除的记录!");
                        return;
                    }
                    int n = JOptionPane.showConfirmDialog(null, "确定要删除这条记录吗?");
                    if (n == 0) {
                        Connection con=null;
                        try {
                            con = dbUtil.getCon();
                            int re = bookDao.bookDelete(con, id);
                            if(1 == re){
                                JOptionPane.showMessageDialog(null, "删除成功");
                                resetValue();
                                fillTable(new Book());
                            }else
                                JOptionPane.showMessageDialog(null, "删除失败");
                        } catch (Exception e1) {
                            JOptionPane.showMessageDialog(null, "删除失败");
                            e1.printStackTrace();
                        }
                    }
                }
            });
            jb_delete.setBounds(257, 225, 93, 23);
            panel_1.add(jb_delete);
        }
        
        private void resetValue() {
            this.idTxt.setText("");
            this.bookNameTxt.setText("");
            this.bookAuthorTxt.setText("");
            this.bookPriceTxt.setText("");
            this.bookDescTxt.setText("");
            if (jcb_bookType.getItemCount() > 0) {
                jcb_bookType.setSelectedIndex(0);
            }
        }
    }
    cn.edu.sau.view.BookManageInterFrm

      提供图书管理功能,主要功能和图书类别管理类似,这里不再赘述。

      4、读者借阅图书

    package cn.edu.sau.dao;
    
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    
    import cn.edu.sau.model.Book;
    import cn.edu.sau.model.User;
    import cn.edu.sau.util.StringUtil;
    
    public class UserDao {
        public User login(Connection con, User user) throws Exception {
            User returnUser = null;
            String sql = "select * from t_user where userName=? and password=?";
            PreparedStatement pstmt = con.prepareStatement(sql);
            pstmt.setString(1, user.getUserName());
            pstmt.setString(2, user.getUserPassword());
            ResultSet rs = pstmt.executeQuery();
            if (rs.next()) {
                returnUser = new User(rs.getInt("id"), rs.getString("userName"),
                        rs.getString("password"), rs.getInt("role"),
                        rs.getString("pname"));
            }
            return returnUser;
        }
    
        // 借书
        public int lendBook(Connection con, int userId, int bookId)
                throws Exception {
            String sql = "insert into t_lendbook values(null,?,?)";
            PreparedStatement pstmt = con.prepareStatement(sql);
            pstmt.setInt(1, userId);
            pstmt.setInt(2, bookId);
            return pstmt.executeUpdate();
        }
    
        // 还书
        public int returnBook(Connection con, String id) throws Exception {
            String sql = "delete from t_lendbook where id=?";
            PreparedStatement pstmt = con.prepareStatement(sql);
            pstmt.setString(1, id);
            return pstmt.executeUpdate();
        }
    
        public ResultSet lendBookList(Connection con, User user, Book book)
                throws Exception {
            StringBuffer sb = new StringBuffer("select * from t_lendbook ");
            if(user!=null && user.getId()!= -1){
                sb.append(" and userid = "+user.getId());
            }
            if(book!=null && book.getId()!=-1){
                sb.append(" and bookid ="+book.getId());
            }
            PreparedStatement pstmt = con.prepareStatement(sb.toString().replaceFirst("and", "where"));
            return pstmt.executeQuery();
        }
    
        public String getPnameById(Connection con, int userId) throws Exception {
            String sql = "select * from t_user where id=?";
            PreparedStatement pstmt = con.prepareStatement(sql);
            pstmt.setInt(1, userId);
            ResultSet rs = pstmt.executeQuery();
            if (rs.next())
                return rs.getString("pname");
            else
                return null;
        }
    }
    cn.edu.sau.dao.UserDao

      提供借书,还书,读取借书类别清单,以及辅助函数(根据用户id获取用户姓名)等方法。

    package cn.edu.sau.view;
    
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.sql.Connection;
    import java.sql.ResultSet;
    import java.util.Vector;
    
    import javax.swing.JButton;
    import javax.swing.JComboBox;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JMenuItem;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.JTextField;
    import javax.swing.border.EmptyBorder;
    import javax.swing.border.TitledBorder;
    import javax.swing.table.DefaultTableModel;
    
    import cn.edu.sau.dao.BookDao;
    import cn.edu.sau.dao.BookTypeDao;
    import cn.edu.sau.dao.UserDao;
    import cn.edu.sau.model.Book;
    import cn.edu.sau.model.BookType;
    import cn.edu.sau.model.User;
    import cn.edu.sau.util.DbUtil;
    import java.awt.Window.Type;
    import javax.swing.ImageIcon;
    
    
    public class UserMainFrm extends JFrame {
        private User user;
        DbUtil dbUtil = new DbUtil();
        BookDao bookDao = new BookDao();
        BookTypeDao bookTypeDao = new BookTypeDao();
        UserDao userDao = new UserDao();
        
        private JPanel contentPane;
        private JPanel panel;
        private JTextField bookNameTxt;
        private JTextField bookAuthorTxt;
        private JComboBox<BookType> jcb_bookType;
        private JButton jb_search;
        private JTable table;
        private JTextField s_idTxt;
        private JButton jb_lendBook;
        private JTextField s_bookNameTxt;
        
        /**
         * Launch the application.
         */
        /*public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
                public void run() {
                    try {
                        
                        UserMainFrm frame = new UserMainFrm();
                        frame.setVisible(true);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        }*/
    
        /**
         * Create the frame.
         */
        public UserMainFrm(User user) {
            
            setTitle("u501Fu9605u7CFBu7EDF");
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setBounds(100, 100, 835, 445);
            this.user = user;
            JOptionPane.showMessageDialog(null, "欢迎"+user.getPname()+"使用我们的系统");
            
            init();
            fillTable(new Book());
            this.fillBookType("search");
            this.fillBookType("modify");
        }
        
        
    
        private void fillBookType(String type) {
            Connection con = null;
            BookType bookType = null;
            try {
                con = dbUtil.getCon();
                ResultSet rs = bookTypeDao.bookTypeList(con, new BookType());
                if ("search".equals(type)) {
                    bookType = new BookType();
                    bookType.setBookTypeName("请选择...");
                    bookType.setId(-1);
                    this.jcb_bookType.addItem(bookType);
                }
                while (rs.next()) {
                    bookType = new BookType();
                    bookType.setId(rs.getInt("id"));
                    bookType.setBookTypeName(rs.getString("bookTypeName"));
                    if ("search".equals(type)) {
                        this.jcb_bookType.addItem(bookType);
                    } else if ("modify".equals(type)) {
                        this.jcb_bookType.addItem(bookType);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    dbUtil.closeCon(con);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        
        private void fillTable(Book book) {
            DefaultTableModel dtm = (DefaultTableModel)table.getModel();    
            dtm.setRowCount(0);
            Connection con = null;
            try {
                con = dbUtil.getCon();
                ResultSet rs = bookDao.bookList(con, book);
                while (rs.next()) {
                    Vector v = new Vector();
                    v.add(rs.getInt("id"));
                    v.add(rs.getString("bookName"));
                    v.add(rs.getString("author"));
                    v.add(rs.getFloat("price"));
                    v.add(rs.getString("bookDesc"));
                    v.add(rs.getString("bookTypeName"));
                    String state = rs.getInt("state")==1?"借阅中":"正常";
                    v.add(state);
                    dtm.addRow(v);
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    dbUtil.closeCon(con);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        
        private void init() {
            contentPane = new JPanel();
            contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
            setContentPane(contentPane);
            contentPane.setLayout(null);
            
            panel = new JPanel();
            panel.setLayout(null);
            panel.setToolTipText("u641Cu7D22u6761u4EF6");
            panel.setBorder(new TitledBorder(null, "u641Cu7D22u6761u4EF6", TitledBorder.LEADING, TitledBorder.TOP, null, null));
            panel.setBounds(43, 28, 732, 68);
            contentPane.add(panel);
            
            JLabel label = new JLabel("u56FEu4E66u7C7Bu522BuFF1A
    ");
            label.setBounds(410, 29, 72, 15);
            panel.add(label);
            
            JLabel label_1 = new JLabel("u56FEu4E66u4F5Cu8005uFF1A");
            label_1.setBounds(209, 29, 72, 15);
            panel.add(label_1);
            
            JLabel label_2 = new JLabel("u56FEu4E66u540Du79F0uFF1A");
            label_2.setBounds(10, 29, 67, 15);
            panel.add(label_2);
            
            bookNameTxt = new JTextField();
            bookNameTxt.setColumns(10);
            bookNameTxt.setBounds(84, 26, 114, 21);
            panel.add(bookNameTxt);
            
            bookAuthorTxt = new JTextField();
            bookAuthorTxt.setColumns(10);
            bookAuthorTxt.setBounds(285, 26, 114, 21);
            panel.add(bookAuthorTxt);
            
            jb_search = new JButton("u67E5u8BE2");
            jb_search.setIcon(new ImageIcon("D:\java\MyBookManager\image\search.png"));
            jb_search.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    Book book = new Book();
                    String bookName = bookNameTxt.getText();
                    String bookAuthor = bookAuthorTxt.getText();
                    
                    BookType bookType = (BookType)jcb_bookType.getSelectedItem();
                    int bookTypeId = bookType.getId();
                    
                    
                    book.setBookName(bookName);
                    book.setBookAuthor(bookAuthor);
                    book.setBookTypeId(bookTypeId);
                    fillTable(book);
                }
            });
            jb_search.setBounds(629, 25, 93, 23);
            panel.add(jb_search);
            
            jcb_bookType = new JComboBox<BookType>();
            jcb_bookType.setBounds(472, 26, 131, 21);
            panel.add(jcb_bookType);
            
            JScrollPane scrollPane = new JScrollPane();
            scrollPane.setToolTipText("");
            scrollPane.setBounds(43, 120, 732, 178);
            contentPane.add(scrollPane);
            
            table = new JTable();
            table.addMouseListener(new MouseAdapter() {
                @Override
                public void mousePressed(MouseEvent e) {
                    int row = table.getSelectedRow();
                    s_idTxt.setText((Integer)table.getValueAt(row, 0)+"");
                    s_bookNameTxt.setText(table.getValueAt(row, 1)+"");
                }
            });
            table.setModel(new DefaultTableModel(
                new Object[][] {
                    {null, null, null, null, null, null, null},
                },
                new String[] {
                    "u56FEu4E66u7F16u53F7", "u56FEu4E66u540Du79F0", "u56FEu4E66u4F5Cu8005", "u56FEu4E66u4EF7u683C", "u56FEu4E66u63CFu8FF0", "u56FEu4E66u7C7Bu522B", "u56FEu4E66u72B6u6001"
                }
            ));
            table.setToolTipText("u56FEu4E66");
            table.setSurrendersFocusOnKeystroke(true);
            scrollPane.setViewportView(table);
            
            jb_lendBook = new JButton("u501Fu4E66");
            jb_lendBook.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    int row = table.getSelectedRow();
                    String state = table.getValueAt(row,6)+"";
                    if("借阅中".equals(state)){
                        JOptionPane.showMessageDialog(null, "该书已经被借走,请选择其他书籍");
                    }else{
                        Connection con=null;
                        
                        try {
                            con = dbUtil.getCon();
                            int bookId = (Integer)table.getValueAt(row,0);
                            
                            int ret = userDao.lendBook(con, user.getId(), bookId);
                            if(1 == ret){
                                fillTable(new Book());
                                JOptionPane.showMessageDialog(null, "借阅成功");
                                bookDao.bookLend(con, bookId);
                            }
                            else
                                JOptionPane.showMessageDialog(null, "借阅失败");
                        } catch (Exception e1) {
                            JOptionPane.showMessageDialog(null, "借阅失败");
                            e1.printStackTrace();
                        } finally{
                            try {
                                dbUtil.closeCon(con);
                            } catch (Exception e1) {
                                e1.printStackTrace();
                            }
                        }
                    }
                }
            });
            jb_lendBook.setBounds(615, 322, 160, 23);
            contentPane.add(jb_lendBook);
            
            JLabel label_3 = new JLabel("u56FEu4E66u7F16u53F7uFF1A");
            label_3.setBounds(43, 326, 66, 15);
            contentPane.add(label_3);
            
            s_idTxt = new JTextField();
            s_idTxt.setEnabled(false);
            s_idTxt.setBounds(119, 323, 135, 21);
            contentPane.add(s_idTxt);
            s_idTxt.setColumns(10);
            
            JLabel label_4 = new JLabel("u56FEu4E66u540Du79F0uFF1A");
            label_4.setBounds(340, 326, 66, 15);
            contentPane.add(label_4);
            
            s_bookNameTxt = new JTextField();
            s_bookNameTxt.setEnabled(false);
            s_bookNameTxt.setColumns(10);
            s_bookNameTxt.setBounds(427, 323, 135, 21);
            contentPane.add(s_bookNameTxt);
        }
    }
    cn.edu.sau.view.UserMain

      这里书的状态主要由book表的state字段决定,为0正常,为1表示被借走。

      5、管理员借阅情况管理

    package cn.edu.sau.view;
    
    import java.awt.Color;
    import java.awt.Component;
    import java.awt.EventQueue;
    import java.sql.Connection;
    import java.sql.ResultSet;
    import java.util.Vector;
    
    import javax.swing.JButton;
    import javax.swing.JInternalFrame;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.JTextField;
    import javax.swing.UIManager;
    import javax.swing.border.TitledBorder;
    import javax.swing.table.DefaultTableModel;
    
    import org.eclipse.wb.swing.FocusTraversalOnArray;
    
    import cn.edu.sau.dao.BookDao;
    import cn.edu.sau.dao.UserDao;
    import cn.edu.sau.model.Book;
    import cn.edu.sau.model.User;
    import cn.edu.sau.util.DbUtil;
    import cn.edu.sau.util.StringUtil;
    
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    
    public class LendBookManageInterFrm extends JInternalFrame {
        DbUtil dbUtil = new DbUtil();
        UserDao userDao = new UserDao();
        BookDao bookDao = new BookDao();
        
        private JTextField userIdTxt;
        private JTextField bookIdTxt;
        private JTable table;
        private JTextField s_idTxt;
        private JTextField s_userNameTxt;
        private JTextField s_bookNameTxt;
    
        /**
         * Launch the application.
         */
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
                public void run() {
                    try {
                        LendBookManageInterFrm frame = new LendBookManageInterFrm();
                        frame.setVisible(true);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        }
    
        /**
         * Create the frame.
         */
        public LendBookManageInterFrm() {
            setIconifiable(true);
            setClosable(true);
            setTitle("u56FEu4E66u501Fu9605u7BA1u7406");
            setBounds(100, 100, 691, 448);
            getContentPane().setLayout(null);
            
            init();
            fillTable(null,null);
        }
        //填充借书记录表格
        private void fillTable(User user,Book book){
            DefaultTableModel dtm = (DefaultTableModel)table.getModel();    
            dtm.setRowCount(0);
            Connection con = null;
            try {
                con = dbUtil.getCon();
                ResultSet rs = userDao.lendBookList(con, user, book);
                while (rs.next()) {
                    Vector v = new Vector();
                    v.add(rs.getInt("id"));
                    v.add(userDao.getPnameById(con, rs.getInt("userid"))+"("+rs.getInt("userid")+")");
                    v.add(bookDao.getBookNameById(con, rs.getInt("bookid"))+"("+rs.getInt("bookid")+")");
                    dtm.addRow(v);
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    dbUtil.closeCon(con);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    
        private void init() {
            JPanel panel = new JPanel();
            panel.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "u641Cu7D22u6761u4EF6", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0, 0, 0)));
            panel.setToolTipText("u641Cu7D22u6761u4EF6");
            panel.setBounds(10, 24, 655, 58);
            getContentPane().add(panel);
            panel.setLayout(null);
            
            JLabel lblid = new JLabel("u7528u6237IduFF1A");
            lblid.setBounds(10, 20, 70, 15);
            panel.add(lblid);
            
            userIdTxt = new JTextField();
            userIdTxt.setBounds(78, 17, 150, 21);
            panel.add(userIdTxt);
            userIdTxt.setColumns(10);
            
            JLabel lblid_1 = new JLabel("u56FEu4E66Id
    uFF1A");
            lblid_1.setBounds(238, 20, 70, 15);
            panel.add(lblid_1);
            
            bookIdTxt = new JTextField();
            bookIdTxt.setBounds(318, 17, 155, 21);
            panel.add(bookIdTxt);
            bookIdTxt.setColumns(10);
            
            //搜索按钮
            JButton jb_search = new JButton("u641Cu7D22");
            jb_search.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    Connection con=null;
                    try {
                        con = dbUtil.getCon();
                        String userId = userIdTxt.getText();
                        String bookId = bookIdTxt.getText();
                        Book book = new Book();
                        User user = new User();
                        if(StringUtil.isNotEmpty(bookId)){
                            book.setId(Integer.parseInt(bookId));
                        }
                        if(StringUtil.isNotEmpty(userId))
                            user.setId(Integer.parseInt(userId));
                        fillTable(user,book);
                    } catch (Exception e1) {
                        e1.printStackTrace();
                    }finally{
                        try {
                            dbUtil.closeCon(con);
                        } catch (Exception e1) {
                            e1.printStackTrace();
                        }
                    }
                }
            });
            jb_search.setBounds(510, 16, 93, 23);
            panel.add(jb_search);
            panel.setFocusTraversalPolicy(new FocusTraversalOnArray(new Component[]{lblid, userIdTxt, lblid_1, bookIdTxt, jb_search}));
            
            JScrollPane scrollPane = new JScrollPane();
            scrollPane.setBounds(10, 92, 655, 174);
            getContentPane().add(scrollPane);
            
            
            //借书记录表格
            table = new JTable();
            table.addMouseListener(new MouseAdapter() {
                @Override
                public void mousePressed(MouseEvent e) {
                    int row = table.getSelectedRow();
                    s_idTxt.setText(table.getValueAt(row, 0)+"");
                    
                    String userName = table.getValueAt(row, 1)+"";
                    s_userNameTxt.setText(userName.replaceAll("\(\d*\)", ""));
                    String bookName = table.getValueAt(row, 2)+"";
                    s_bookNameTxt.setText(bookName.replaceAll("\(\d*\)", ""));
                    
                }
            });
            table.setModel(new DefaultTableModel(
                new Object[][] {
                    {null, null, null},
                },
                new String[] {
                    "u7F16u53F7", "u501Fu9605u4EBA", "u501Fu9605u56FEu4E66"
                }
            ));
            table.getColumnModel().getColumn(0).setPreferredWidth(83);
            table.getColumnModel().getColumn(1).setPreferredWidth(162);
            table.getColumnModel().getColumn(2).setPreferredWidth(187);
            scrollPane.setViewportView(table);
            
            JPanel panel_1 = new JPanel();
            panel_1.setBorder(new TitledBorder(null, "u8868u5355u64CDu4F5C", TitledBorder.LEADING, TitledBorder.TOP, null, null));
            panel_1.setBounds(10, 276, 655, 101);
            getContentPane().add(panel_1);
            panel_1.setLayout(null);
            
            JLabel label_2 = new JLabel("u7F16u53F7uFF1A");
            label_2.setBounds(10, 33, 60, 15);
            panel_1.add(label_2);
            
            s_idTxt = new JTextField();
            s_idTxt.setBounds(91, 30, 66, 21);
            panel_1.add(s_idTxt);
            s_idTxt.setColumns(10);
            
            JLabel label_3 = new JLabel("u501Fu9605u4EBAuFF1A");
            label_3.setBounds(195, 33, 65, 15);
            panel_1.add(label_3);
            
            s_userNameTxt = new JTextField();
            s_userNameTxt.setBounds(270, 30, 132, 21);
            panel_1.add(s_userNameTxt);
            s_userNameTxt.setColumns(10);
            
            JLabel label_4 = new JLabel("u56FEu4E66u540Du79F0uFF1A");
            label_4.setBounds(432, 33, 71, 15);
            panel_1.add(label_4);
            
            s_bookNameTxt = new JTextField();
            s_bookNameTxt.setBounds(513, 30, 132, 21);
            panel_1.add(s_bookNameTxt);
            s_bookNameTxt.setColumns(10);
            
            
            //还书按钮
            JButton jb_return = new JButton("u5F52u8FD8");
            jb_return.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    Connection con = null;
                    try {
                        con = dbUtil.getCon();
                        String id = s_idTxt.getText();
                        String bookName = s_bookNameTxt.getText();
                        if(StringUtil.isEmpty(bookName)){
                            JOptionPane.showMessageDialog(null, "请选择要归还书籍记录");
                            return ;
                        }
                        userDao.returnBook(con, id);
                        bookDao.bookReturn(con, bookName);
                        fillTable(null, null);
                    } catch (Exception e1) {
                        e1.printStackTrace();
                    } finally{
                        try {
                            dbUtil.closeCon(con);
                        } catch (Exception e1) {
                            e1.printStackTrace();
                        }
                    }
                    
                }
            });
            jb_return.setBounds(10, 68, 635, 23);
            panel_1.add(jb_return);
        }
    }
    cn.edu.sau.view.LendBookManageInterFrm

      界面提供了多级连表查询以及还书接口。

      6、数据库设计

        1)user表

        2)booktype表

        3)book表

        4)lendbook表

       根据数据库所学知识,我们将数据库分为4张表,user表中记录用户信息;booktype表中记录图书分类信息;book表中记录图书信息,同时将book表中的booktypeId关联到booktype表的id上;lendbook为借书信息,其中userid和bookid为两个外键,分别对应user表的id和book表的id。这里应该感谢这学期的数据库课程,让我们在设计表结构的时候免去了不少的麻烦。

      注:代码注释不是很详细,传至coding时会添加完整注释内容以及数据库文件等(虽然我现在不知道这种工程应该怎么传送到coding   T T )。

      Conding地址:https://coding.net/u/2013040101068/p/alarm-work/git

  • 相关阅读:
    web服务器-Apache
    nginx优化
    nginx下载限速
    nginx-URL重写
    HDU 5358 First One 求和(序列求和,优化)
    HDU 5360 Hiking 登山 (优先队列,排序)
    HDU 5353 Average 糖果分配(模拟,图)
    UVALive 4128 Steam Roller 蒸汽式压路机(最短路,变形) WA中。。。。。
    HDU 5348 MZL's endless loop 给边定向(欧拉回路,最大流)
    HDU 5344 MZL's xor (水题)
  • 原文地址:https://www.cnblogs.com/qmzmfteam/p/5516108.html
Copyright © 2011-2022 走看看