目 录
1.项目概述及需求分析................................ 1
2.系统设计......................................... 1
2.1程序总体设计......................................... 1
2.2数据库设计........................................... 2
2.3公共模块设计......................................... 2
3.系统实现......................................... 4
3.1登录界面............................................. 4
3.2主界面............................................... 5
3.3图片添加的实现....................................... 8
3.4用户页面的实现...................................... 10
3.5图书类别实现........................................ 15
4.总结............................................ 15
随着人们生活水平的提高,知识水平蓬勃发展,日渐增多的读书分子的学习带来了一定的压力,怎样的借用图书是亟待解决的重要问题,传统的管理方式带来了许多问题,例如去哪里买图书,需要买什么图书,这些问题导致无法做到随时随地阅读图书全面把控,本项目面向所有借书,目的是提高图书馆的管理效率,把人工从繁琐的工作中解放出来。
该系统主要实现的系统需求有如下几个方面:
庞大的消费群体拉动了图书的消费增长,同时为了安全要对借书人进行实名制的统计。
制定可控的图书管理系统,能自由的控制每天售出的图书本数,对书名,作者,出版社是本系统不可缺少的一部分,将图书的基本信息和数量写入到系统中,当顾客来借书时可以查询图书的价格。信息社会中Excel表格被广泛运用,实现将数据库的信息导出为Excel信息是必要的,这将方便信息的共享,同时Excel丰富的功能可以成为本程序的扩展功能,提高程序的兼容性,一举多得。
图2.1 图书馆管理系统功能结构图
创建DB类用来连接mysql数据库,以及对数据库进行查询和更改操作,将所得到的值放到集合,传递给程序使用,实现程序与数据库之间的数据共享。
加载数据库驱动,并连接数据库,具体代码如下:
package com.demo.utils; import java.sql.Connection; import java.sql.DriverManager; public class DbUtil { //private String dbUrl="jdbc:mysql://localhost:3306/db_book_manager?useUnicode=true&characterEncoding=utf8"; // 数据库连接地址 5.x private String dbUrl="jdbc:mysql://localhost:3306/wgt?useSSL=false&serverTimezone=GMT&useUnicode=true&characterEncoding=UTF8"; // 数据库连接地址 8.0 private String dbUserName="root"; // 用户名 private String dbPassword="123456"; // 密码 // 驱动名称 //private String jdbcName="com.mysql.jdbc.Driver"; //myslq5.x private String jdbcName="com.mysql.cj.jdbc.Driver"; //myslq8.0 public Connection getConnection()throws Exception{ Class.forName(jdbcName); Connection con = (Connection) DriverManager.getConnection(dbUrl,dbUserName,dbPassword); return con; } public void closeCon (Connection con)throws Exception { if(con!=null){ con.close(); } } public static void main(String[] args) throws Exception { //运行后直接结束则代表连接成功 DbUtil dbUtil=new DbUtil(); dbUtil.getConnection(); } }
public class toolUtil { public static int getCenterWidth(int width){ GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment(); Rectangle rect=ge.getMaximumWindowBounds(); int w=rect.width; return w/2-width/2; } public static int getCenterHeight(int height){ GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment(); Rectangle rect=ge.getMaximumWindowBounds(); int h=rect.height; return h/2-height/2; } public static boolean isEmpty(String str){ if(str != null && !"".equals(str.trim())){ return false; } return true; } public static Long getTime(){ long time = System.currentTimeMillis(); return time; } public static String getDateByTime(Long time){ SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String string = format.format(new Date(time)); return string; } public static User getUser(HttpSession session){ User user = (User) session.getAttribute("user"); return user; } public static void setUser(HttpSession session,User user){ session.setAttribute("user", user); } }
登录界面,输入用户名和密码,本系统的初始用户名为:admin,密码为:admin,界面如下:
图3.1 登录界面
登录页面实现代码:
public class AdminBookAdd extends JFrame { private JFrame jf; private JTextField textField; private JTextField textField_1; private JTextField textField_2; private JTextField textField_3; private JTextField textField_4; private JTextField textField_6; private JComboBox comboBox; BookDao bookDao=new BookDao(); DbUtil dbUtil=new DbUtil(); BookTypeDao bookTypeDao=new BookTypeDao(); public AdminBookAdd(){ jf=new JFrame("管理员界面"); int width = 600; int height =400; jf.setBounds(toolUtil.getCenterWidth(width), toolUtil.getCenterHeight(height),width, height); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jf.getContentPane().setLayout(null); JPanel panel = new JPanel(); panel.setBorder(new TitledBorder(null, "\u4E66\u7C4D\u6DFB\u52A0", TitledBorder.LEADING, TitledBorder.TOP, null, Color.RED)); panel.setBounds(23, 21, 540, 275); jf.getContentPane().add(panel); panel.setLayout(null);
主界面为空布局,顶部为菜单栏,具体界面如下:
图3.2 主界面
主页面实现代码:
public AdminBookEdit(){ jf=new JFrame("管理员界面"); int width = 600; int height =650; jf.setBounds(toolUtil.getCenterWidth(width), toolUtil.getCenterHeight(height),width, height); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JMenuBar menuBar = new JMenuBar(); jf.setJMenuBar(menuBar); JMenu mnNewMenu = new JMenu("类别管理"); menuBar.add(mnNewMenu); JMenuItem mntmNewMenuItem = new JMenuItem("类别添加"); mntmNewMenuItem.addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent evt) { jf.dispose(); new AdminMenuFrm(); } }); mnNewMenu.add(mntmNewMenuItem); JMenuItem mntmNewMenuItem_1 = new JMenuItem("类别修改"); mntmNewMenuItem_1.addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent evt) { jf.dispose(); new AdminBTypeEdit(); } }); mnNewMenu.add(mntmNewMenuItem_1); JMenu mnNewMenu_2 = new JMenu("书籍管理"); menuBar.add(mnNewMenu_2); JMenuItem mntmNewMenuItem_2 = new JMenuItem("书籍添加"); mntmNewMenuItem_2.addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent evt) { jf.dispose(); new AdminBookAdd(); } }); mnNewMenu_2.add(mntmNewMenuItem_2); JMenuItem mntmNewMenuItem_3 = new JMenuItem("书籍修改"); mnNewMenu_2.add(mntmNewMenuItem_3); JMenu menu1 = new JMenu("用户管理"); menuBar.add(menu1); JMenuItem mntmNewMenuItem_4 = new JMenuItem("用户信息"); mntmNewMenuItem_4.addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent evt) { jf.dispose(); new AdminUserInfo(); } }); menu1.add(mntmNewMenuItem_4); JMenuItem mntmNewMenuItem_5 = new JMenuItem("借阅信息"); mntmNewMenuItem_5.addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent evt) { jf.dispose(); new AdminBorrowInfo(); } }); menu1.add(mntmNewMenuItem_5); JMenu mnNewMenu_1 = new JMenu("退出系统"); mnNewMenu_1.addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent evt) { JOptionPane.showMessageDialog(null, "欢迎再次使用"); jf.dispose(); } }); menuBar.add(mnNewMenu_1); jf.getContentPane().setLayout(null); JPanel panel = new JPanel(); panel.setBorder(new TitledBorder(null, "\u4E66\u76EE\u67E5\u8BE2", TitledBorder.LEADING, TitledBorder.TOP, null, Color.RED)); panel.setBounds(20, 10, 541, 78); jf.getContentPane().add(panel); panel.setLayout(null); comboBox = new JComboBox(); comboBox.setFont(new Font("幼圆", Font.BOLD, 15)); comboBox.setBounds(55, 28, 109, 24); comboBox.addItem("书籍名称"); comboBox.addItem("书籍作者"); panel.add(comboBox); textField = new JTextField(); textField.setBounds(185, 28, 146, 24); panel.add(textField); textField.setColumns(10); JButton btnNewButton = new JButton("查询"); btnNewButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { int index = comboBox.getSelectedIndex(); if(index==0){ String bookName = textField.getText(); Book book = new Book(); book.setBookName(bookName); putDates(book); }else{ String authoerName = textField.getText(); Book book = new Book(); book.setAuthor(authoerName); putDates(book); } } });
图书界面,输入书名、作者,选择出版社,点击添加按钮。如图3.3:
图3.3 管理员界面图
图书添加的实现代码:
btnNewButton.setFont(new Font("幼圆", Font.BOLD, 14)); btnNewButton.setBounds(352, 28, 81, 25); panel.add(btnNewButton); JPanel panel_1 = new JPanel(); panel_1.setLayout(null); panel_1.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "\u4E66\u7C4D\u4FE1\u606F", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(255, 0, 0))); panel_1.setBounds(20, 105, 541, 195); /*做一个表头栏数据 一位数组 * */ String[] title={"编号", "书名", "类别", "作者", "价格", "库存", "状态" }; /*具体的各栏行记录 先用空的二位数组占位*/ String[][] dates={}; /*然后实例化 上面2个控件对象*/ model=new DefaultTableModel(dates,title); table=new JTable(model); putDates(new Book());//获取数据库数据放置table中 panel_1.setLayout(null); JScrollPane jscrollpane = new JScrollPane(); jscrollpane.setBounds(20, 22, 496, 154); jscrollpane.setViewportView(table); panel_1.add(jscrollpane); jf.getContentPane().add(panel_1); jf.getContentPane().add(panel_1); table.addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent evt) { tableMousePressed(evt); } }); JPanel panel_2 = new JPanel(); panel_2.setBounds(20, 310, 541, 292); jf.getContentPane().add(panel_2); panel_2.setLayout(null); JLabel label = new JLabel("编号:"); label.setFont(new Font("幼圆", Font.BOLD, 14)); label.setBounds(58, 10, 45, 27); panel_2.add(label); textField_1 = new JTextField(); textField_1.setColumns(10); textField_1.setBounds(101, 10, 129, 27); panel_2.add(textField_1); JLabel label_1 = new JLabel("书名:"); label_1.setFont(new Font("幼圆", Font.BOLD, 14)); label_1.setBounds(294, 10, 45, 27); panel_2.add(label_1); textField_2 = new JTextField(); textField_2.setColumns(10); textField_2.setBounds(338, 10, 128, 27); panel_2.add(textField_2);
public UserMenuFrm() { jf=new JFrame(); jf.setTitle("用户页面"); int width = 700; int height =900; jf.setBounds(toolUtil.getCenterWidth(width), toolUtil.getCenterHeight(height),width, height); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jf.getContentPane().setLayout(null); JPanel panel_1 = new JPanel(); panel_1.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "\u501F\u9605\u4FE1\u606F", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(255, 0, 0))); panel_1.setBounds(23, 48, 651, 239); /*做一个表头栏数据 一位数组 * */ String[] title={"编号", "书名", "状态", "借书时间", "还书时间"}; /*具体的各栏行记录 先用空的二位数组占位*/ String[][] dates={}; /*然后实例化 上面2个控件对象*/ model=new DefaultTableModel(dates,title); table=new JTable(); table.setModel(model); putDates(new BorrowDetail());//获取数据库数据放置table中 panel_1.setLayout(null); JScrollPane jscrollpane = new JScrollPane(); jscrollpane.setBounds(20, 22, 607, 188); jscrollpane.setViewportView(table); panel_1.add(jscrollpane); jf.getContentPane().add(panel_1); lblNewLabel_1 = new JLabel("New label"); lblNewLabel_1.setForeground(Color.RED); lblNewLabel_1.setFont(new Font("Dialog", Font.BOLD, 18)); lblNewLabel_1.setBounds(315, 10, 197, 28); jf.getContentPane().add(lblNewLabel_1); lblNewLabel_1.setText(LoginFrm.currentUser.getUserName()); lblNewLabel_2 = new JLabel("欢迎您,"); lblNewLabel_2.setFont(new Font("Dialog", Font.BOLD, 18)); lblNewLabel_2.setBounds(254, 11, 258, 28); jf.getContentPane().add(lblNewLabel_2); JPanel panel = new JPanel(); panel.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "\u8FD8\u4E66", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(255, 0, 0))); panel.setBounds(23, 294, 651, 70); jf.getContentPane().add(panel); panel.setLayout(null); JLabel lblNewLabel = new JLabel("编号:"); lblNewLabel.setBounds(90, 25, 51, 27); panel.add(lblNewLabel); lblNewLabel.setFont(new Font("幼圆", Font.BOLD, 16)); textField = new JTextField(); textField.setBounds(145, 28, 116, 24); panel.add(textField); textField.setColumns(10); btnBackBook = new JButton("还书"); btnBackBook.setFont(new Font("Dialog", Font.BOLD, 15)); btnBackBook.setBounds(299, 25, 85, 31); panel.add(btnBackBook); button = new JButton("退出系统"); button.setFont(new Font("Dialog", Font.BOLD, 15)); button.setBounds(407, 25, 103, 31); panel.add(button); panel_2 = new JPanel(); panel_2.setBorder(new TitledBorder(null, "借阅信息", TitledBorder.LEADING, TitledBorder.TOP, null, Color.RED)); panel_2.setBounds(23, 374, 651, 346); jf.getContentPane().add(panel_2); panel_2.setLayout(null); textField_1 = new JTextField(); textField_1.setColumns(10); textField_1.setBounds(252, 23, 135, 27); panel_2.add(textField_1); button_1 = new JButton("查询"); button_1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { int index = comboBox.getSelectedIndex(); if(index==0){ String bookName = textField_1.getText(); Book book = new Book(); book.setBookName(bookName); putDates(book); }else{ String authoerName = textField_1.getText(); Book book = new Book(); book.setAuthor(authoerName); putDates(book); } } }); button_1.setFont(new Font("幼圆", Font.BOLD, 16)); button_1.setBounds(408, 20, 93, 33); panel_2.add(button_1); comboBox = new JComboBox(); comboBox.setFont(new Font("幼圆", Font.BOLD, 15)); comboBox.setBounds(123, 26, 109, 24); comboBox.addItem("书籍名称"); comboBox.addItem("书籍作者"); panel_2.add(comboBox); String[] BookTitle={"编号", "书名", "类型", "作者", "描述" }; /*具体的各栏行记录 先用空的二位数组占位*/ String[][] BookDates={}; /*然后实例化 上面2个控件对象*/ BookModel=new DefaultTableModel(BookDates,BookTitle); BookTable=new JTable(BookModel); putDates(new Book());//获取数据库数据放置table中 panel_2.setLayout(null); JScrollPane jscrollpane1 = new JScrollPane(); jscrollpane1.setBounds(22, 74, 607, 250); jscrollpane1.setViewportView(BookTable); panel_2.add(jscrollpane1); jf.getContentPane().add(panel_1); JPanel panel_3 = new JPanel(); panel_3.setBorder(new TitledBorder(null, "\u501F\u4E66", TitledBorder.LEADING, TitledBorder.TOP, null, Color.RED)); panel_3.setBounds(23, 730, 645, 87); jf.getContentPane().add(panel_3); panel_3.setLayout(null); JLabel label = new JLabel("编号:"); label.setFont(new Font("Dialog", Font.BOLD, 15)); label.setBounds(68, 31, 48, 33); panel_3.add(label); textField_2 = new JTextField(); textField_2.setEditable(false); textField_2.setColumns(10); textField_2.setBounds(126, 34, 135, 27); panel_3.add(textField_2); JLabel label_1 = new JLabel("书名:"); label_1.setFont(new Font("Dialog", Font.BOLD, 15)); label_1.setBounds(281, 31, 48, 33); panel_3.add(label_1); textField_3 = new JTextField(); textField_3.setEditable(false); textField_3.setColumns(10); textField_3.setBounds(339, 34, 135, 27); panel_3.add(textField_3); JButton button_2 = new JButton("借书"); //借书 button_2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String bookId = textField_2.getText(); String bookName = textField_3.getText(); if (toolUtil.isEmpty(bookId) || toolUtil.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(toolUtil.getTime()); Connection con = null; try { con = dbUtil.getConnection(); //先查询是否有该书 ResultSet list = bdetailDao.list(con, borrowDetail); while(list.next()){ JOptionPane.showMessageDialog(null, "该书已在借,请先还再借"); return; } int i = bdetailDao.add(con, borrowDetail); if (i == 1) { JOptionPane.showMessageDialog(null, "借书成功"); putDates(new BorrowDetail()); } else { JOptionPane.showMessageDialog(null, "借书失败"); } } catch (Exception e1) { // TODO Auto-generated catch block e1.printStackTrace(); JOptionPane.showMessageDialog(null, "借书异常"); }finally{ try { dbUtil.closeCon(con); } catch (Exception e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } } });
btnNewButton.setFont(new Font("幼圆", Font.BOLD, 14)); btnNewButton.setBounds(352, 28, 81, 25); panel.add(btnNewButton); JPanel panel_1 = new JPanel(); panel_1.setLayout(null); panel_1.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "\u4E66\u7C4D\u4FE1\u606F", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(255, 0, 0))); panel_1.setBounds(20, 105, 541, 195); /*做一个表头栏数据 一位数组 * */ String[] title={"编号", "书名", "类别", "作者", "价格", "库存", "状态" }; /*具体的各栏行记录 先用空的二位数组占位*/ String[][] dates={}; /*然后实例化 上面2个控件对象*/ model=new DefaultTableModel(dates,title); table=new JTable(model); putDates(new Book());//获取数据库数据放置table中 panel_1.setLayout(null); JScrollPane jscrollpane = new JScrollPane(); jscrollpane.setBounds(20, 22, 496, 154); jscrollpane.setViewportView(table); panel_1.add(jscrollpane); jf.getContentPane().add(panel_1); jf.getContentPane().add(panel_1); table.addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent evt) { tableMousePressed(evt); } });
通过这次Java大作业我深刻认识到了Java作为一门被广泛应用的高级语言的强大之处,丰富的类让开发变得得心应手,这次大作业让我明白了不要讲目光仅仅聚焦到课本上,要合理运用网络资源进行自主性学习,正所谓师傅领进门,修行在个人,学习不是光靠老师就能行的,自己还要付出加倍的努力。
该系统实现的优点:界面直观简洁,设计对用户友好;方便查找图书,登陆简单;实际应用性强,通用型较好;数据库设计合理,。
系统还存在一定的不足,表现在:代码不够成熟,简洁性不强;程序受众面较窄,用户局限性高;没有考虑并行操作和根据用户的使用的情况。
作为一名青岛学院的本科生,我们要学习的不光是一项技能还有学习分析问题、的方法,以及在现代软件开发中应该用到的基本技能,这些细节看似可有可无但是这恰恰是体现专业性的地方。所以我认为要学会触类旁通,利用丰富的网络资源去打磨细节,提升自己的综合能力。