基于JavaSwing+mysql的图书管理系统设计和实现

这是我参与 8 月更文挑战的第 23天,活动详情查看: 8月更文挑战

前言:

        项目是使用Java swing开发,可实现基础数据维护、图书类型管理和维护、图书信息管理和维护、注销退出、关于作者简介等功能。界面设计比较简介、适合作为Java课设设计以及学习技术使用。

引言

       随着计算机及网络技术的飞速发展,Intranet 应用在全球范围内日益普及, 当今社会正快速向信息化社会前进,信息系统的作用也越来越大。图书馆在正常运营中总是面对大量的读者信息,书籍信息以及由两者相互作用产生的借书信息,还书信息。因此图书管理信息化是发展的必然趋势。用结构化系统分析与设计的方法,建立一套有效的图书信息管理系统,可以减轻工作,将工作科学化、规范化,提高了图书馆信息管理的工作质量因此根据图书馆目前实际的管理情况开发一套冬书管理系统是一分必要的。

主要技术和工具:

eclipse+JDK1..8+Navicat +swing +mysql

功能截图:

图书类型管理:

图书类型管理维护、可以根据图书类型查看图书信息、可以根据编号和信息删除修改图书类型信息

图书信息管理:

图书信息维护管理、点击图书维护可以根据图书名称、作者以及图书类型模糊查询图书信息、可以点击下面的输入框进行数据修改和删除操作

选中数据进行修改和删除操作

 

图书添加:

  

作者简介:

注销退出:

关键代码:

 主入口:

package com.HPioneer.view;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import java.awt.GridLayout;
import javax.swing.SpringLayout;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.ImageIcon;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JDesktopPane;
import java.awt.Color;
import java.awt.SystemColor;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class MainFrm extends JFrame {

	private JPanel contentPane;
	private JDesktopPane table = null;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MainFrm frame = new MainFrm();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public MainFrm() {
		setTitle("图书管理主界面");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		
		JMenuBar menuBar = new JMenuBar();
		setJMenuBar(menuBar);
		
		JMenu menu = new JMenu("基本数据维护");
		menu.setIcon(new ImageIcon(MainFrm.class.getResource("/images/base.png")));
		menuBar.add(menu);
		
		JMenu mnNewMenu = new JMenu("图书类别管理");
		mnNewMenu.setIcon(new ImageIcon(MainFrm.class.getResource("/images/bookTypeManager.png")));
		menu.add(mnNewMenu);
		
		JMenuItem menuItem = new JMenuItem("图书类别添加");
		menuItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				BookTypeAddInterFrm bookTypeAddInterFrm = new BookTypeAddInterFrm();
				bookTypeAddInterFrm.setVisible(true);
			    table.add(bookTypeAddInterFrm);
			}
		});
		menuItem.setIcon(new ImageIcon(MainFrm.class.getResource("/images/add.png")));
		mnNewMenu.add(menuItem);
		
		JMenuItem menuItem_2 = new JMenuItem("图书类别维护");
		menuItem_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				BookTypeManagerInterFrm bookTypeManagerInterFrm = new BookTypeManagerInterFrm();
				bookTypeManagerInterFrm.setVisible(true);
			    table.add(bookTypeManagerInterFrm);
			}
		});
		menuItem_2.setIcon(new ImageIcon(MainFrm.class.getResource("/images/edit.png")));
		mnNewMenu.add(menuItem_2);
		
		JMenu mnNewMenu_1 = new JMenu("图书管理");
		mnNewMenu_1.setIcon(new ImageIcon(MainFrm.class.getResource("/images/bookManager.png")));
		menu.add(mnNewMenu_1);
		
		JMenuItem menuItem_1 = new JMenuItem("图书添加");
		menuItem_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				BookAddInterFrm bookAddInterFrm = new BookAddInterFrm();
				bookAddInterFrm.setVisible(true);
			    table.add(bookAddInterFrm);
				
			}
		});
		menuItem_1.setIcon(new ImageIcon(MainFrm.class.getResource("/images/add.png")));
		mnNewMenu_1.add(menuItem_1);
		
		JMenuItem mntmNewMenuItem = new JMenuItem("图书维护");
		mntmNewMenuItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				BookManageInterFrm bookManagerInterFrm = new BookManageInterFrm();
				bookManagerInterFrm.setVisible(true);
			    table.add(bookManagerInterFrm);
			}
		});
		mntmNewMenuItem.setIcon(new ImageIcon(MainFrm.class.getResource("/images/edit.png")));
		mnNewMenu_1.add(mntmNewMenuItem);
		
		JMenuItem menuItem_3 = new JMenuItem("安全退出");
		menuItem_3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			    int result =JOptionPane.showConfirmDialog(null,"是否退出系统");
			    
			}
		});
		menuItem_3.setIcon(new ImageIcon(MainFrm.class.getResource("/images/exit.png")));
		menu.add(menuItem_3);
		
		JMenu menu_1 = new JMenu("关于作者");

		menu_1.setIcon(new ImageIcon(MainFrm.class.getResource("/images/about.png")));
		menuBar.add(menu_1);
		
		JMenuItem mntmhpioneer = new JMenuItem("关于奥斯卡");
		mntmhpioneer.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				 HPioneer1234InterFrm hPioneer1234InterFrm = new HPioneer1234InterFrm();
				    hPioneer1234InterFrm.setVisible(true);
				    table.add(hPioneer1234InterFrm);
			}
		});
		mntmhpioneer.setIcon(new ImageIcon(MainFrm.class.getResource("/images/userName.png")));
		menu_1.add(mntmhpioneer);
		contentPane = new JPanel();
		contentPane.setForeground(Color.BLUE);
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new BorderLayout(0, 0));
		
        table = new JDesktopPane();		
		table.setBackground(Color.WHITE);
		contentPane.add(table);
		
		//设置Jrame最大化
		this.setExtendedState(JFrame.MAXIMIZED_BOTH);
	}
}
复制代码

添加图书:

package com.HPioneer.view;

import java.awt.EventQueue;

import javax.swing.JInternalFrame;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.LineBorder;

import com.HPioneer.dao.BookDao;
import com.HPioneer.dao.BookTypeDao;
import com.HPioneer.model.Book;
import com.HPioneer.model.BookType;
import com.HPioneer.util.DbUtil;
import com.HPioneer.util.StringUtil;
import com.mysql.jdbc.Connection;

import javax.swing.JRadioButton;
import javax.swing.ButtonGroup;
import javax.swing.JTextArea;
import javax.swing.JComboBox;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.awt.event.ActionEvent;
import javax.swing.ImageIcon;

public class BookAddInterFrm extends JInternalFrame {
	private JTextField bookNameTxt;
	private JTextField authorTxt;
	private final ButtonGroup buttonGroup = new ButtonGroup();
	private JTextField priceTxt;
	
	private DbUtil dbUtil = new DbUtil();
	private BookTypeDao bookTypeDao = new BookTypeDao();
	private BookDao bookDao = new BookDao();
	private	JComboBox bookTypeJcb;
	private JTextArea bookDescTxt;
	private JRadioButton manJrb; 
	private JRadioButton femaleJrb;
	
	/**
	 * 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() {
		setIconifiable(true);
		setClosable(true);
		setTitle("图书添加");
		setBounds(100, 100, 450, 463);
		
		JLabel lblNewLabel = new JLabel("图书名字:");
		
		bookNameTxt = new JTextField();
		bookNameTxt.setColumns(10);
		
		JLabel lblNewLabel_1 = new JLabel("图书作者:");
		
		authorTxt = new JTextField();
		authorTxt.setColumns(10);
		
		JLabel lblNewLabel_2 = new JLabel("作者性别:");
		
		manJrb = new JRadioButton("男");
		buttonGroup.add(manJrb);
		manJrb.setSelected(true);
		
		femaleJrb = new JRadioButton("女");
		buttonGroup.add(femaleJrb);
		
		JLabel lblNewLabel_3 = new JLabel("图书价格:");
		
		priceTxt = new JTextField();
		priceTxt.setColumns(10);
		
		JLabel lblNewLabel_4 = new JLabel("图书描述:");
		
	    bookDescTxt = new JTextArea();
		
		JLabel lblNewLabel_5 = new JLabel("图书类别:");
		
	    bookTypeJcb = new JComboBox();
		
		JButton btnNewButton = new JButton("添加");
		btnNewButton.setIcon(new ImageIcon(BookAddInterFrm.class.getResource("/images/add.png")));
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				bookAddActionPerformed(e);
			}
		});
		
		JButton btnNewButton_1 = new JButton("重置");
		btnNewButton_1.setIcon(new ImageIcon(BookAddInterFrm.class.getResource("/images/add.png")));
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				bookValueActionPerformed(e);
			}
		});
		
		btnNewButton_1.setIcon(new ImageIcon(BookAddInterFrm.class.getResource("/images/reset.png")));
		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(28)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup()
							.addComponent(lblNewLabel_4)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(bookDescTxt, GroupLayout.DEFAULT_SIZE, 262, Short.MAX_VALUE))
						.addGroup(groupLayout.createSequentialGroup()
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING, false)
								.addGroup(groupLayout.createSequentialGroup()
									.addComponent(lblNewLabel_2)
									.addPreferredGap(ComponentPlacement.UNRELATED)
									.addComponent(manJrb)
									.addPreferredGap(ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
									.addComponent(femaleJrb))
								.addGroup(groupLayout.createSequentialGroup()
									.addComponent(lblNewLabel)
									.addPreferredGap(ComponentPlacement.RELATED)
									.addComponent(bookNameTxt, GroupLayout.PREFERRED_SIZE, 91, GroupLayout.PREFERRED_SIZE)))
							.addGap(18)
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
								.addComponent(lblNewLabel_3)
								.addComponent(lblNewLabel_1))
							.addPreferredGap(ComponentPlacement.RELATED)
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
								.addComponent(authorTxt, GroupLayout.DEFAULT_SIZE, 89, Short.MAX_VALUE)
								.addComponent(priceTxt, GroupLayout.DEFAULT_SIZE, 89, Short.MAX_VALUE)))
						.addGroup(groupLayout.createSequentialGroup()
							.addComponent(btnNewButton)
							.addGap(33)
							.addComponent(btnNewButton_1))
						.addGroup(groupLayout.createSequentialGroup()
							.addComponent(lblNewLabel_5)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(bookTypeJcb, 0, 262, Short.MAX_VALUE)))
					.addGap(80))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(33)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblNewLabel)
						.addComponent(bookNameTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(authorTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblNewLabel_1))
					.addGap(26)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblNewLabel_2)
						.addComponent(manJrb)
						.addComponent(femaleJrb)
						.addComponent(lblNewLabel_3)
						.addComponent(priceTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(26)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblNewLabel_5)
						.addComponent(bookTypeJcb, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(30)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addComponent(lblNewLabel_4)
						.addComponent(bookDescTxt, GroupLayout.PREFERRED_SIZE, 140, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.RELATED, 38, Short.MAX_VALUE)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(btnNewButton)
						.addComponent(btnNewButton_1))
					.addGap(50))
		);
		getContentPane().setLayout(groupLayout);

		//显示文本域边框
		bookDescTxt.setBorder(new LineBorder(new java.awt.Color(127,157,185),1,false));
				
		fillBookType();
	}
	/**
	 * 重置事件处理
	 * @param e
	 */
	private void bookValueActionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		this.resetValue();
	}

	private void bookAddActionPerformed(ActionEvent evt) {
		// TODO Auto-generated method stub
		String bookName = this.bookNameTxt.getText();
		String author = this.authorTxt.getText();
		String price = this.priceTxt.getText();
		String bookDesc = this.bookDescTxt.getText();
		
		 if(StringUtil.isEmpty(bookName)){
	        	JOptionPane.showMessageDialog(null,"图书名称不能为空");
	        }
		 if(StringUtil.isEmpty(author)){
	        	JOptionPane.showMessageDialog(null,"图书作者不能为空");
	        }
		 if(StringUtil.isEmpty(price)){
	        	JOptionPane.showMessageDialog(null,"图书价格不能为空");
	        }
		 
		 String sex ="";
		 if(manJrb.isSelected()){
			 sex="男";
		 }else{
			 sex="女";
		 }
		 
		 BookType bookType =(BookType) bookTypeJcb.getSelectedItem();
		 int bookTypeId = bookType.getId();
		 
		 Book book = new Book(bookName,author,sex,Float.parseFloat(price),bookTypeId,bookDesc);
		 
		 Connection con = null;
	        try{
	        	con=dbUtil.getCon();
	        	int addNum = bookDao.add(con, book);  	
	            if(addNum == 1){
	            	JOptionPane.showMessageDialog(null,"图书类别添加成功");	
	                resetValue();
	            }else{
	            	JOptionPane.showMessageDialog(null,"图书类别添加失败");
	            }
	        }catch(Exception e){
	        	e.printStackTrace();  
	        	JOptionPane.showMessageDialog(null,"图书类别添加失败");
	        }finally{
	        	try{
	        	dbUtil.closeCon(con);
	        	}catch (Exception e) {
					// TODO: handle exception
	        		e.printStackTrace();	
				}
	        	
	        }
	    }
	/**
	 * 重置表单
	 */
	private void resetValue() {
		// TODO Auto-generated method stub
		this.bookNameTxt.setText("");
		this.authorTxt.setText("");
		this.priceTxt.setText("");
		this.manJrb.setSelected(true);
		this.bookDescTxt.setText("");
		if(this.bookTypeJcb.getItemCount()>0){
			this.bookTypeJcb.setSelectedIndex(0);
			
		}
		
	}
	

	/**
	 * 初始化图书类别下拉框
	 */
	
	private void fillBookType(){
		Connection con = null;
		BookType bookType = null;
		try{
			con = dbUtil.getCon();
			ResultSet rs = bookTypeDao.list(con, new BookType());
		    while( rs.next() ){
		    	bookType = new BookType();
		    	bookType.setId(rs.getInt("id"));
		    	bookType.setBookTypeName(rs.getString("bookTypeName"));
		    	this.bookTypeJcb.addItem(bookType);
		     }
		 }catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally{
			
		}
	}
}

		
	
复制代码

数据库设计:

用户表:

CREATE TABLE `NewTable` (
`id`  int(11) NOT NULL AUTO_INCREMENT ,
`userName`  varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL ,
`password`  varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL ,
PRIMARY KEY (`id`)
)
ENGINE=InnoDB
DEFAULT CHARACTER SET=utf8 COLLATE=utf8_general_ci
AUTO_INCREMENT=2
ROW_FORMAT=COMPACT
;
复制代码

图书表:

CREATE TABLE `NewTable` (
`id`  int(11) NOT NULL AUTO_INCREMENT ,
`bookName`  varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL ,
`author`  varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL ,
`sex`  varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL ,
`price`  float NULL DEFAULT NULL ,
`bookTypeId`  int(255) NULL DEFAULT NULL ,
`bookTypeName`  varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL ,
`bookDesc`  varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL ,
PRIMARY KEY (`id`),
FOREIGN KEY (`bookTypeId`) REFERENCES `t_booktype` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT,
INDEX `by` (`bookTypeId`) USING BTREE 
)
ENGINE=InnoDB
DEFAULT CHARACTER SET=utf8 COLLATE=utf8_general_ci
AUTO_INCREMENT=3
ROW_FORMAT=COMPACT
;
复制代码

图书类型表 

CREATE TABLE `NewTable` (
`id`  int(11) NOT NULL AUTO_INCREMENT ,
`bookTypeName`  varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL ,
`bookTypeDesc`  varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL ,
PRIMARY KEY (`id`)
)
ENGINE=InnoDB
DEFAULT CHARACTER SET=utf8 COLLATE=utf8_general_ci
AUTO_INCREMENT=9
ROW_FORMAT=COMPACT
;
复制代码

备注:项目来于网络、作者整理优化测试、若有侵权联系作者删除

总结:

本系统是在汤蓉老师的悉心指导下顺利完成的,从系统的选题、方案的制定以及论文的撰写,每一步都倾注着我们整个团队的心血。在此,衷心感谢大家对我们这个项目整个过程的积极筹划以及出谋划策,在面对困难的时候大家共通过不放弃,通过各种办法解决各种困难在此同时感谢汤蓉和徐振明老师,缜密的逻辑,活跃的思维,敏锐的洞察力,严谨的治学态度以及民主的作风给我留下了深刻的印象,为我开阔了视野,丰富了学识,并将使我受益终身,我学习的楷模;汤蓉老师的工作态度和说教方式让我们感到很亲切与他相处的也很融洽。感谢计算机科学与技术专业、计算机系的全体老师辛勤培养和教诲!

打卡JavaSwing项目更新 2 / 100篇

大家可以点赞、收藏、关注、评论我啦