Eclipse+Java+Swing实现图书管理系统(上)

简介: Eclipse+Java+Swing实现图书管理系统

一、系统介绍


该系统实现用户注册登录功能。用户端实现书籍查询,借书,还书功能。用户能够查询自己的借书记录。管理员能够上架图书,下架图书,添加图书类别,修改图书类别,删除图书类别,添加图书信息,修改图书信息,修改用户信息,查询图书借阅归还记录。


二、系统展示


1.注册


20200920094716901.jpg


2.登录


20200920094742375.jpg


3.用户端-图书查询及借阅


20200920094817140.jpg


4.用户端-借阅记录及归还


20200920094920456.jpg


5.管理员端-添加图书类别


20200920094946314.jpg


6.管理员端-修改图书类别


20200920095051752.jpg


7.管理员端-添加图书


2020092009514184.jpg


8.管理员端-修改图书


20200920095218457.jpg


9.管理员端-管理用户信息


20200920095252954.jpg


10.管理员端-查询借阅信息


20200920095332600.jpg


三、部分代码


基本类什么的就不列出来了,直接上页面。


AdminMainFrm.java

package com.sjsq.view;
import java.awt.event.ActionEvent;
import javax.swing.GroupLayout;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.WindowConstants;
public class AdminMainFrm extends JFrame {
  private JDesktopPane jD_table;
  private JMenu jMenu1;
  private JMenu jMenu2;
  private JMenu jMenu3;
  private JMenu jMenu4;
  private JMenuBar jMenuBar1;
  private JMenuItem jMenuItem1;
  private JMenuItem jMenuItem2;
  private JMenuItem jMenuItem3;
  private JMenuItem jMenuItem4;
  private JMenuItem jMenuItem5;
  private JMenuItem jMenuItem6;
  private JMenuItem jMenuItem7;
  public AdminMainFrm() {
    initComponents();
    // 设置位置
    setBounds(100, 200, 1050, 650);
    // 居中显示
    this.setLocationRelativeTo(null);
  }
  private void initComponents() {
    jD_table = new JDesktopPane();
    jMenuBar1 = new JMenuBar();
    jMenu1 = new JMenu();
    jMenu2 = new JMenu();
    jMenuItem1 = new JMenuItem();
    jMenuItem2 = new JMenuItem();
    jMenu3 = new JMenu();
    jMenuItem4 = new JMenuItem();
    jMenuItem5 = new JMenuItem();
    jMenu4 = new JMenu();
    jMenuItem6 = new JMenuItem();
    jMenuItem7 = new JMenuItem();
    jMenuItem3 = new JMenuItem();
    setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    setTitle("管理员主界面");
    jMenu1.setText("基本数据管理");
    jMenu2.setText("类别管理");
    jMenu2.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        jMenu2ActionPerformed(evt);
      }
      private void jMenu2ActionPerformed(ActionEvent evt) {
      }
    });
    jMenuItem1.setText("图书类别添加");
    jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        jMenuItem1ActionPerformed(evt);
      }
    });
    jMenu2.add(jMenuItem1);
    jMenuItem2.setText("图书类别修改");
    jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        jMenuItem2ActionPerformed(evt);
      }
    });
    jMenu2.add(jMenuItem2);
    jMenu1.add(jMenu2);
    jMenu3.setText("书籍管理");
    jMenuItem4.setText("书籍添加");
    jMenuItem4.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        jMenuItem4ActionPerformed(evt);
      }
    });
    jMenu3.add(jMenuItem4);
    jMenuItem5.setText("书籍修改");
    jMenuItem5.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        jMenuItem5ActionPerformed(evt);
      }
    });
    jMenu3.add(jMenuItem5);
    jMenu1.add(jMenu3);
    jMenu4.setText("用户管理");
    jMenuItem6.setText("用户信息");
    jMenuItem6.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        jMenuItem6ActionPerformed(evt);
      }
    });
    jMenu4.add(jMenuItem6);
    jMenuItem7.setText("借还信息");
    jMenuItem7.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        jMenuItem7ActionPerformed(evt);
      }
    });
    jMenu4.add(jMenuItem7);
    jMenu1.add(jMenu4);
    jMenuItem3.setText("退出系统");
    jMenuItem3.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        jMenuItem3ActionPerformed(evt);
      }
    });
    jMenu1.add(jMenuItem3);
    jMenuBar1.add(jMenu1);
    setJMenuBar(jMenuBar1);
    GroupLayout layout = new GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
        .addComponent(jD_table, GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE));
    layout.setVerticalGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
        .addComponent(jD_table, GroupLayout.DEFAULT_SIZE, 279, Short.MAX_VALUE));
    pack();
  }
  // 借还信息
  private void jMenuItem7ActionPerformed(java.awt.event.ActionEvent evt) {
    BorrowDetailFrm b = new BorrowDetailFrm();
    b.setVisible(true);
    this.jD_table.add(b);
  }
  // 用户信息
  private void jMenuItem6ActionPerformed(java.awt.event.ActionEvent evt) {
    UserInfoFrm userInfoFrm = new UserInfoFrm();
    userInfoFrm.setVisible(true);
    this.jD_table.add(userInfoFrm);
  }
  private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {
    BookTypeAddFrm addFrm = new BookTypeAddFrm();
    addFrm.setVisible(true);
    this.jD_table.add(addFrm);
  }
  private void jMenuItem5ActionPerformed(java.awt.event.ActionEvent evt) {
    BookManagerFrm bookManagerFrm = new BookManagerFrm();
    bookManagerFrm.setVisible(true);
    this.jD_table.add(bookManagerFrm);
  }
  private void jMenuItem4ActionPerformed(java.awt.event.ActionEvent evt) {
    BookAddFrm bookAddFrm = new BookAddFrm();
    bookAddFrm.setVisible(true);
    this.jD_table.add(bookAddFrm);
  }
  private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {
    BookTypeManagerFrm bookTypeManagerFrm = new BookTypeManagerFrm();
    bookTypeManagerFrm.setVisible(true);
    this.jD_table.add(bookTypeManagerFrm);
  }
  private void jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {
    int i = JOptionPane.showConfirmDialog(null, "确认退出系统");
    if (i == 0) {
      this.dispose();
    }
  }
  public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
      public void run() {
        new AdminMainFrm().setVisible(true);
      }
    });
  }
}

BookAddFrm.java

package com.sjsq.view;
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.Vector;
import javax.swing.GroupLayout;
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.LayoutStyle;
import javax.swing.table.DefaultTableModel;
import com.sjsq.dao.BookDao;
import com.sjsq.dao.BorrowDetailDao;
import com.sjsq.model.Book;
import com.sjsq.model.BorrowDetail;
import com.sjsq.util.DbUtil;
import com.sjsq.util.StringUtil;
import com.sjsq.util.TimeUtil;
public class BookInfoFrm extends JInternalFrame {
  private JTextField bookIdText;
  private JTextField bookName;
  private JTextField bookNameText;
  private JTable bookTable;
  private JButton jB_borrow;
  private JButton jButton1;
  private JLabel jLabel1;
  private JLabel jLabel2;
  private JLabel jLabel3;
  private JPanel jPanel1;
  private JScrollPane jScrollPane1;
  DbUtil dbUtil = new DbUtil();
  BookDao bookDao = new BookDao();
  BorrowDetailDao borrowDetailDao = new BorrowDetailDao();
  public BookInfoFrm() {
    initComponents();
    fillTable(new Book());
    setTitle("书籍信息");
    this.setLocation(200, 50);
  }
  private void fillTable(Book book) {
    DefaultTableModel model = (DefaultTableModel) bookTable.getModel();
    model.setRowCount(0);
    Connection con = null;
    try {
      con = dbUtil.getCon();
      book.setStatus(1);
      ResultSet list = bookDao.list(con, book);
      while (list.next()) {
        Vector rowData = new Vector();
        rowData.add(list.getInt("id"));
        rowData.add(list.getString("book_name"));
        rowData.add(list.getString("type_name"));
        rowData.add(list.getString("author"));
        rowData.add(list.getString("remark"));
        model.addRow(rowData);
      }
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      try {
        dbUtil.closeCon(con);
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
  }
  private void initComponents() {
    jScrollPane1 = new JScrollPane();
    bookTable = new JTable();
    jPanel1 = new JPanel();
    jLabel1 = new JLabel();
    bookNameText = new JTextField();
    jButton1 = new JButton();
    jB_borrow = new JButton();
    jLabel2 = new JLabel();
    bookIdText = new JTextField();
    jLabel3 = new JLabel();
    bookName = new JTextField();
    setClosable(true);
    bookTable.setModel(new DefaultTableModel(new Object[][] {
    }, new String[] { "编号", "书名", "类型", "作者", "描述" }) {
      boolean[] canEdit = new boolean[] { false, false, false, false, false };
      public boolean isCellEditable(int rowIndex, int columnIndex) {
        return canEdit[columnIndex];
      }
    });
    bookTable.addMouseListener(new java.awt.event.MouseAdapter() {
      public void mousePressed(java.awt.event.MouseEvent evt) {
        bookTableMousePressed(evt);
      }
    });
    jScrollPane1.setViewportView(bookTable);
    jLabel1.setText("书籍名称:");
    bookNameText.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        bookNameTextActionPerformed(evt);
      }
    });
    jButton1.setText("查询");
    jButton1.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        jButton1ActionPerformed(evt);
      }
    });
    GroupLayout jPanel1Layout = new GroupLayout(jPanel1);
    jPanel1.setLayout(jPanel1Layout);
    jPanel1Layout.setHorizontalGroup(jPanel1Layout.createParallelGroup(GroupLayout.Alignment.LEADING)
        .addGroup(GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
            .addGap(42, 42, 42).addComponent(jLabel1).addGap(36, 36, 36)
            .addComponent(bookNameText, GroupLayout.PREFERRED_SIZE, 130,
                GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 149, Short.MAX_VALUE)
            .addComponent(jButton1).addGap(45, 45, 45)));
    jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(GroupLayout.Alignment.LEADING)
        .addGroup(jPanel1Layout.createSequentialGroup().addGap(23, 23, 23)
            .addGroup(jPanel1Layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                .addComponent(jButton1).addComponent(jLabel1).addComponent(bookNameText,
                    GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,
                    GroupLayout.PREFERRED_SIZE))
            .addContainerGap(30, Short.MAX_VALUE)));
    jB_borrow.setText("借书");
    jB_borrow.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        jB_borrowActionPerformed(evt);
      }
    });
    jLabel2.setText("编号:");
    bookIdText.setEditable(false);
    jLabel3.setText("书名:");
    bookName.setEditable(false);
    GroupLayout layout = new GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(layout
        .createSequentialGroup().addGap(22, 22, 22)
        .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(layout
            .createSequentialGroup().addComponent(jLabel2)
            .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(bookIdText, GroupLayout.PREFERRED_SIZE, 95,
                GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED).addComponent(jLabel3)
            .addGap(18, 18, 18)
            .addComponent(bookName, GroupLayout.PREFERRED_SIZE, 127,
                GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 102, Short.MAX_VALUE)
            .addComponent(jB_borrow).addGap(88, 88, 88))
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
                    .addComponent(jPanel1, GroupLayout.Alignment.TRAILING,
                        GroupLayout.DEFAULT_SIZE,
                        GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jScrollPane1, GroupLayout.Alignment.TRAILING,
                        GroupLayout.DEFAULT_SIZE, 507, Short.MAX_VALUE))
                .addContainerGap(38, Short.MAX_VALUE)))));
    layout.setVerticalGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addComponent(jPanel1, GroupLayout.PREFERRED_SIZE,
                GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(jScrollPane1, GroupLayout.PREFERRED_SIZE, 225,
                GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 35, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                .addComponent(jLabel2).addComponent(jLabel3)
                .addComponent(bookIdText, GroupLayout.PREFERRED_SIZE,
                    GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                .addComponent(bookName, GroupLayout.PREFERRED_SIZE,
                    GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                .addComponent(jB_borrow))
            .addGap(27, 27, 27)));
    pack();
  }
  private void jB_borrowActionPerformed(java.awt.event.ActionEvent evt) {
    String bookId = this.bookIdText.getText();
    String bookName = this.bookName.getText();
    if (StringUtil.isEmpty(bookId) || StringUtil.isEmpty(bookName)) {
      JOptionPane.showMessageDialog(null, "请选择相关书籍");
      return;
    }
    BorrowDetail borrowDetail = new BorrowDetail();
    borrowDetail.setUserId(LoginFrm.currentUser.getUserId());
    borrowDetail.setBookId(Integer.parseInt(bookId));
    borrowDetail.setStatus(1);
    borrowDetail.setBorrowTime(TimeUtil.getTime());
    Connection con = null;
    try {
      con = dbUtil.getCon();
      // 先查询是否有该书在借
      ResultSet list = borrowDetailDao.list(con, borrowDetail);
      while (list.next()) {
        JOptionPane.showMessageDialog(null, "该书已在借,请先还再借");
        return;
      }
      int i = borrowDetailDao.add(con, borrowDetail);
      if (i == 1) {
        JOptionPane.showMessageDialog(null, "借书成功");
      } else {
        JOptionPane.showMessageDialog(null, "借书失败");
      }
    } catch (Exception e) {
      e.printStackTrace();
      JOptionPane.showMessageDialog(null, "借书异常");
    } finally {
      try {
        dbUtil.closeCon(con);
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
  }
  private void bookTableMousePressed(java.awt.event.MouseEvent evt) {
    int row = this.bookTable.getSelectedRow();
    Object bookId = this.bookTable.getValueAt(row, 0);
    Object bookName = this.bookTable.getValueAt(row, 1);
    this.bookIdText.setText(bookId.toString());
    this.bookName.setText(bookName.toString());
  }
  private void bookNameTextActionPerformed(java.awt.event.ActionEvent evt) {
  }
  private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    String bookName = this.bookNameText.getText();
    Book book = new Book();
    book.setBookName(bookName);
    fillTable(book);
  }
}

BookManagerFrm.java

package com.sjsq.view;
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.Vector;
import javax.swing.BorderFactory;
import javax.swing.GroupLayout;
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.JTextField;
import javax.swing.LayoutStyle;
import javax.swing.table.DefaultTableModel;
import com.sjsq.dao.BookDao;
import com.sjsq.dao.BookTypeDao;
import com.sjsq.model.Book;
import com.sjsq.model.BookType;
import com.sjsq.util.DbUtil;
import com.sjsq.util.StringUtil;
public class BookManagerFrm extends JInternalFrame {
  private JTextField authorText;
  private JTextField bookId;
  private JTextField bookName;
  private JTextField bookNameText;
  private JTable bookTable;
  private JButton jButton1;
  private JButton jButton2;
  private JComboBox jCB_status;
  private JComboBox jCB_type2;
  private JLabel jLabel1;
  private JLabel jLabel10;
  private JLabel jLabel11;
  private JLabel jLabel3;
  private JLabel jLabel4;
  private JLabel jLabel5;
  private JLabel jLabel6;
  private JLabel jLabel7;
  private JLabel jLabel8;
  private JLabel jLabel9;
  private JPanel jPanel1;
  private JPanel jPanel2;
  private JScrollPane jScrollPane1;
  private JTextField numberText;
  private JTextField priceText;
  private JTextField publishText;
  private JTextField remarkText;
  DbUtil dbUtil = new DbUtil();
  BookTypeDao bookTypeDao = new BookTypeDao();
  BookDao bookDao = new BookDao();
  public BookManagerFrm() {
    initComponents();
    this.jCB_status.addItem("上架");
    this.jCB_status.addItem("下架");
    fillBookTable(new Book());
    fillBookType();
    setTitle("书籍修改");
    this.setLocation(125, 10);
  }
  private void fillBookType() {
    Connection con = null;
    try {
      con = dbUtil.getCon();
      ResultSet list = bookTypeDao.list(con, new BookType());
      while (list.next()) {
        BookType bookType = new BookType();
        bookType.setTypeId(list.getInt("id"));
        bookType.setTypeName(list.getString("type_name"));
        this.jCB_type2.addItem(bookType);
      }
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      try {
        dbUtil.closeCon(con);
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
  }
  private void fillBookTable(Book book) {
    DefaultTableModel model = (DefaultTableModel) bookTable.getModel();
    model.setRowCount(0);
    Connection con = null;
    try {
      con = dbUtil.getCon();
      ResultSet resultSet = bookDao.list(con, book);
      while (resultSet.next()) {
        Vector rowData = new Vector();
        rowData.add(resultSet.getInt("id"));
        rowData.add(resultSet.getString("book_name"));
        rowData.add(resultSet.getString("type_name"));
        rowData.add(resultSet.getString("author"));
        rowData.add(resultSet.getDouble("price"));
        rowData.add(resultSet.getInt("number"));
        if (resultSet.getInt("status") == 1) {
          rowData.add("上架");
        } else {
          rowData.add("下架");
        }
        model.addRow(rowData);
      }
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      try {
        dbUtil.closeCon(con);
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
  }
  private void initComponents() {
    jPanel1 = new JPanel();
    jLabel1 = new JLabel();
    bookNameText = new JTextField();
    jButton1 = new JButton();
    jScrollPane1 = new JScrollPane();
    bookTable = new JTable();
    jPanel2 = new JPanel();
    jLabel3 = new JLabel();
    bookId = new JTextField();
    jLabel4 = new JLabel();
    bookName = new JTextField();
    jLabel5 = new JLabel();
    authorText = new JTextField();
    jLabel6 = new JLabel();
    publishText = new JTextField();
    jLabel7 = new JLabel();
    priceText = new JTextField();
    jLabel8 = new JLabel();
    numberText = new JTextField();
    jLabel9 = new JLabel();
    jLabel10 = new JLabel();
    jCB_type2 = new JComboBox();
    jLabel11 = new JLabel();
    remarkText = new JTextField();
    jButton2 = new JButton();
    jCB_status = new JComboBox();
    setClosable(true);
    jPanel1.setBorder(BorderFactory.createTitledBorder("查询操作"));
    jLabel1.setText("书名:");
    jButton1.setText("查询");
    jButton1.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        jButton1ActionPerformed(evt);
      }
    });
    GroupLayout jPanel1Layout = new GroupLayout(jPanel1);
    jPanel1.setLayout(jPanel1Layout);
    jPanel1Layout.setHorizontalGroup(jPanel1Layout.createParallelGroup(GroupLayout.Alignment.LEADING)
        .addGroup(jPanel1Layout.createSequentialGroup().addGap(25, 25, 25).addComponent(jLabel1)
            .addGap(18, 18, 18)
            .addComponent(bookNameText, GroupLayout.PREFERRED_SIZE, 171,
                GroupLayout.PREFERRED_SIZE)
            .addGap(62, 62, 62).addComponent(jButton1).addContainerGap(299, Short.MAX_VALUE)));
    jPanel1Layout
        .setVerticalGroup(jPanel1Layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup().addGroup(jPanel1Layout
                .createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(jLabel1)
                .addComponent(bookNameText, GroupLayout.PREFERRED_SIZE,
                    GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                .addComponent(jButton1)).addContainerGap(22, Short.MAX_VALUE)));
    bookTable.setModel(new DefaultTableModel(new Object[][] {
    }, new String[] { "编号", "书名", "类别", "作者", "价格", "库存", "状态" }) {
      boolean[] canEdit = new boolean[] { false, false, false, false, false, false, false };
      public boolean isCellEditable(int rowIndex, int columnIndex) {
        return canEdit[columnIndex];
      }
    });
    bookTable.addMouseListener(new java.awt.event.MouseAdapter() {
      public void mousePressed(java.awt.event.MouseEvent evt) {
        bookTableMousePressed(evt);
      }
    });
    jScrollPane1.setViewportView(bookTable);
    jPanel2.setBorder(BorderFactory.createTitledBorder("书籍操作"));
    jLabel3.setText("编号:");
    bookId.setEditable(false);
    jLabel4.setText("书名:");
    jLabel5.setText("作者:");
    jLabel6.setText("出版:");
    jLabel7.setText("价格:");
    jLabel8.setText("库存:");
    jLabel9.setText("状态:");
    jLabel10.setText("类别:");
    jLabel11.setText("描述:");
    jButton2.setText("修改");
    jButton2.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        jButton2ActionPerformed(evt);
      }
    });
    GroupLayout jPanel2Layout = new GroupLayout(jPanel2);
    jPanel2.setLayout(jPanel2Layout);
    jPanel2Layout.setHorizontalGroup(jPanel2Layout.createParallelGroup(GroupLayout.Alignment.LEADING)
        .addGroup(jPanel2Layout.createSequentialGroup()
            .addGroup(jPanel2Layout.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(
                jPanel2Layout.createSequentialGroup().addGap(26, 26, 26).addGroup(jPanel2Layout
                    .createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(
                        jPanel2Layout.createSequentialGroup().addComponent(jLabel9)
                            .addPreferredGap(
                                LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jCB_status, 0, 117, Short.MAX_VALUE))
                    .addGroup(GroupLayout.Alignment.TRAILING,
                        jPanel2Layout
                            .createSequentialGroup().addGroup(jPanel2Layout
                                .createParallelGroup(
                                    GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel3).addComponent(jLabel6))
                            .addPreferredGap(
                                LayoutStyle.ComponentPlacement.RELATED,
                                GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGroup(jPanel2Layout
                                .createParallelGroup(
                                    GroupLayout.Alignment.TRAILING,
                                    false)
                                .addComponent(publishText,
                                    GroupLayout.Alignment.LEADING)
                                .addComponent(bookId,
                                    GroupLayout.Alignment.LEADING,
                                    GroupLayout.DEFAULT_SIZE, 117,
                                    Short.MAX_VALUE))))
                    .addGap(39, 39, 39)
                    .addGroup(jPanel2Layout.createParallelGroup(
                        GroupLayout.Alignment.LEADING, false)
                        .addGroup(jPanel2Layout.createSequentialGroup().addComponent(jLabel10)
                            .addGap(27, 27,
                                27)
                            .addComponent(jCB_type2, 0,
                                GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGroup(GroupLayout.Alignment.TRAILING,
                            jPanel2Layout.createSequentialGroup().addComponent(jLabel7)
                                .addGap(27, 27, 27).addComponent(priceText))
                        .addGroup(jPanel2Layout.createSequentialGroup().addComponent(jLabel4)
                            .addGap(27, 27, 27).addComponent(
                                bookName, GroupLayout.PREFERRED_SIZE, 108,
                                GroupLayout.PREFERRED_SIZE)))
                    .addGap(18, 18, 18)
                    .addGroup(jPanel2Layout
                        .createParallelGroup(GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel2Layout.createSequentialGroup().addComponent(jLabel11)
                            .addGap(18, 18, 18).addComponent(remarkText,
                                GroupLayout.PREFERRED_SIZE, 172,
                                GroupLayout.PREFERRED_SIZE))
                        .addGroup(jPanel2Layout
                            .createParallelGroup(GroupLayout.Alignment.TRAILING,
                                false)
                            .addGroup(GroupLayout.Alignment.LEADING,
                                jPanel2Layout.createSequentialGroup()
                                    .addComponent(jLabel8).addGap(18, 18, 18)
                                    .addComponent(numberText))
                            .addGroup(GroupLayout.Alignment.LEADING,
                                jPanel2Layout.createSequentialGroup()
                                    .addComponent(jLabel5).addGap(20, 20, 20)
                                    .addComponent(authorText,
                                        GroupLayout.PREFERRED_SIZE,
                                        106,
                                        GroupLayout.PREFERRED_SIZE))))
                    .addGap(97, 97, 97))
                .addGroup(jPanel2Layout.createSequentialGroup().addGap(108, 108, 108)
                    .addComponent(jButton2)))
            .addContainerGap()));
    jPanel2Layout.setVerticalGroup(jPanel2Layout.createParallelGroup(GroupLayout.Alignment.LEADING)
        .addGroup(jPanel2Layout.createSequentialGroup().addGroup(jPanel2Layout
            .createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup().addGroup(jPanel2Layout
                .createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(jLabel3)
                .addComponent(bookId, GroupLayout.PREFERRED_SIZE,
                    GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(publishText, GroupLayout.PREFERRED_SIZE,
                        GroupLayout.DEFAULT_SIZE,
                        GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7)
                    .addComponent(priceText, GroupLayout.PREFERRED_SIZE,
                        GroupLayout.DEFAULT_SIZE,
                        GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8).addComponent(numberText,
                        GroupLayout.PREFERRED_SIZE,
                        GroupLayout.DEFAULT_SIZE,
                        GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(jCB_status, GroupLayout.PREFERRED_SIZE,
                        GroupLayout.DEFAULT_SIZE,
                        GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel10)
                    .addComponent(jCB_type2, GroupLayout.PREFERRED_SIZE,
                        GroupLayout.DEFAULT_SIZE,
                        GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel11).addComponent(remarkText,
                        GroupLayout.PREFERRED_SIZE,
                        GroupLayout.DEFAULT_SIZE,
                        GroupLayout.PREFERRED_SIZE)))
            .addGroup(jPanel2Layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                .addComponent(jLabel4)
                .addComponent(bookName, GroupLayout.PREFERRED_SIZE,
                    GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                .addComponent(authorText, GroupLayout.PREFERRED_SIZE,
                    GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                .addComponent(jLabel5)))
            .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 42, Short.MAX_VALUE)
            .addComponent(jButton2).addGap(28, 28, 28)));
    GroupLayout layout = new GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(layout
        .createSequentialGroup().addGap(31, 31, 31)
        .addGroup(layout.createParallelGroup(GroupLayout.Alignment.TRAILING, false)
            .addComponent(jPanel1, GroupLayout.Alignment.LEADING,
                GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE,
                Short.MAX_VALUE)
            .addComponent(jScrollPane1, GroupLayout.Alignment.LEADING).addComponent(jPanel2,
                GroupLayout.Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 668,
                Short.MAX_VALUE))
        .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));
    layout.setVerticalGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addComponent(jPanel1, GroupLayout.PREFERRED_SIZE,
                GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(jScrollPane1, GroupLayout.PREFERRED_SIZE, 186,
                GroupLayout.PREFERRED_SIZE)
            .addGap(18, 18, 18).addComponent(jPanel2, GroupLayout.DEFAULT_SIZE,
                GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addContainerGap()));
    pack();
  }
  private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    String bookName = this.bookNameText.getText();
    Book book = new Book();
    book.setBookName(bookName);
    fillBookTable(book);
  }
  private void bookTableMousePressed(java.awt.event.MouseEvent evt) {
    int row = bookTable.getSelectedRow();
    Integer bookId = (Integer) bookTable.getValueAt(row, 0);
    Book book = new Book();
    book.setBookId(bookId);
    Connection con = null;
    try {
      con = dbUtil.getCon();
      ResultSet list = bookDao.list(con, book);
      if (list.next()) {
        this.bookId.setText(list.getString("id"));
        this.bookName.setText(list.getString("book_name"));
        this.authorText.setText(list.getString("author"));
        this.publishText.setText(list.getString("publish"));
        this.priceText.setText(list.getString("price"));
        this.numberText.setText(list.getString("number"));
        this.remarkText.setText(list.getString("remark"));
        int status = list.getInt("status");
        if (status == 1) {
          this.jCB_status.setSelectedIndex(0);
        } else {
          this.jCB_status.setSelectedIndex(1);
        }
        int typeId = list.getInt("type_id");
        int count = this.jCB_type2.getItemCount();
        for (int i = 0; i < count; i++) {
          BookType bookType = (BookType) this.jCB_type2.getItemAt(i);
          if (bookType.getTypeId() == typeId) {
            this.jCB_type2.setSelectedIndex(i);
          }
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      try {
        dbUtil.closeCon(con);
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
  }
  private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
    String bookName = this.bookName.getText();
    String author = this.authorText.getText();
    String publish = this.publishText.getText();
    String priceStr = this.priceText.getText();
    String numberStr = this.numberText.getText();
    String remark = this.remarkText.getText();
    String bookId = this.bookId.getText();
    if (StringUtil.isEmpty(bookId) || StringUtil.isEmpty(bookName) || StringUtil.isEmpty(author)
        || StringUtil.isEmpty(publish) || StringUtil.isEmpty(priceStr) || StringUtil.isEmpty(numberStr)
        || StringUtil.isEmpty(remark)) {
      JOptionPane.showMessageDialog(null, "请输入相关内容");
      return;
    }
    BookType selectedItem = (BookType) jCB_type2.getSelectedItem();
    Integer typeId = selectedItem.getTypeId();
    int index = jCB_status.getSelectedIndex();
    int number;
    double price;
    try {
      number = Integer.parseInt(numberStr);
      price = new BigDecimal(priceStr).setScale(2, BigDecimal.ROUND_DOWN).doubleValue();
    } catch (Exception e) {
      JOptionPane.showMessageDialog(null, "参数错误");
      return;
    }
    Book book = new Book();
    book.setBookId(Integer.parseInt(bookId));
    book.setBookName(bookName);
    book.setAuthor(author);
    book.setBookTypeId(typeId);
    book.setNumber(number);
    book.setPrice(price);
    book.setPublish(publish);
    book.setRemark(remark);
    book.setStatus(1);
    if (index == 0) {
      book.setStatus(1);
    } else if (index == 1) {
      book.setStatus(2);
    }
    Connection con = null;
    try {
      con = dbUtil.getCon();
      int i = bookDao.update(con, book);
      if (i == 1) {
        JOptionPane.showMessageDialog(null, "修改成功");
      } else {
        JOptionPane.showMessageDialog(null, "修改失败");
      }
    } catch (Exception e) {
      e.printStackTrace();
      JOptionPane.showMessageDialog(null, "修改异常");
    } finally {
      try {
        dbUtil.closeCon(con);
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
    fillBookTable(new Book());
  }
}
相关文章
|
1月前
|
Java Android开发
Eclipse Java 构建路径
Eclipse Java 构建路径
33 3
|
1月前
|
Java Android开发
Eclipse 创建 Java 项目
Eclipse 创建 Java 项目
39 4
|
1月前
|
Java Android开发
Eclipse 创建 Java 接口
Eclipse 创建 Java 接口
27 1
|
1月前
|
Java Android开发
Eclipse 创建 Java 包
Eclipse 创建 Java 包
31 1
|
3月前
|
存储 Java
Java——图书管理系统
该文档详细介绍了一个图书管理系统的设计与实现。系统包含普通用户和管理员两种角色,通过书架操作图书,如添加、查找、借阅、归还及删除图书等功能。文档展示了各个功能的具体代码实现,并使用继承和接口等方式优化了系统结构。通过多态技术实现了不同用户角色调用相应功能。整体设计清晰,逻辑严谨,便于理解和实现。
180 17
Java——图书管理系统
|
1月前
|
Java Android开发
Eclipse 创建 Java 类
Eclipse 创建 Java 类
26 0
|
4月前
|
安全 Java API
|
4月前
|
Java 网络安全 开发工具
新手入门Java。如何下载Eclipse、写出最基本的“Hello word”以及如何连接github并且上传项目。
新手入门Java。如何下载Eclipse、写出最基本的“Hello word”以及如何连接github并且上传项目。
64 0
|
4月前
|
SQL Java 数据库连接
【Java】Java Swing 图书管借阅管理系统(源码+论文)【独一无二】
【Java】Java Swing 图书管借阅管理系统(源码+论文)【独一无二】
236 0
|
4月前
|
存储 数据可视化 Java
【Java】Java swing 民宿管理系统 GUI(源码+可视化界面)【独一无二】
【Java】Java swing 民宿管理系统 GUI(源码+可视化界面)【独一无二】

推荐镜像

更多
下一篇
DataWorks