java library management system (interface version)

table of Contents

 

running result:

Register login screen

After landing the main interface as follows

The book list

project instruction:

Key Code:

Source Access:

How to run:


running result:

Register login screen

When registering an existing account

After landing the main interface as follows

Click on library management - books update interface as follows

The book list

project instruction:

        Because usually busy working, there is no time to write, but I saw a lot of small partners in the public discussion of the background numbers, I would take the time to write as a reference. The system interface is designed to be a simple on my own, and had intended to use windowbuilder plug-in design, lead to code redundancy, can affect the readability of the code, it may be conceivable to use windowbuilder after unfriendly white plug. Although simple interface design, but I will try to think of the function to write the whole, of course, to the page you can add your own design, such as adding a background image, etc., have online guide for the interface from simple aesthetics.

Key Code:

user login

package bookmanage.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import bookmanage.model.User;
import bookmanage.utils.DbUtil;

/**
 * @Description 连接数据库工具类
 * @author com.javayihao.top
 */
public class UserDao {
	/**
	 * 根据用户账号查询用户
	 * 
	 * @param accout
	 *            入参:用户账号
	 * @return 查找的用户
	 */
	public User getUserByAccout(String accout) {
		Connection connection = DbUtil.getConnection();
		String sql = "select accout,pass from t_user where accout=?";
		try {
			PreparedStatement ps = connection.prepareStatement(sql);
			ps.setString(1, accout);
			ResultSet rs = ps.executeQuery();
			if (rs.next()) {// 存在用户,封装用户返回
				User user = new User(rs.getString("accout"), rs.getString("pass"));
				DbUtil.close(connection, ps);// 关闭连接
				return user;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}

	public boolean insertUser(User user) {
		Connection connection = DbUtil.getConnection();// 获得数据库连接对象
		String sql = "insert into t_user(accout,pass)values(?,?)";
		try {
			PreparedStatement ps = connection.prepareStatement(sql);
			ps.setString(1, user.getAccout());
			ps.setString(2, user.getPass());
			if (!ps.execute()) {// 成功
				DbUtil.close(connection, ps);// 关闭连接
				return true;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;// 失败
	}
}

 

Book Update

package bookmanage.view;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import bookmanage.dao.BookDao;
import bookmanage.model.Book;

/**
 * @Description 用于图书增删改查面板
 * @author com.javayihao.top
 */
public class CrudBookPanel extends JPanel implements ActionListener {
	// 定义首页按钮、图书列表按钮、 其他功能按钮,增加图书、删除图书、修改图书、查询图书
	private JButton addBtn, deleteBtn, updateBtn, findBtn;
	// 定义标签 底部信息标签、 图书编号、 图书名称、 图书数量、 图书价格
	private JLabel idLabel, nameLabel, numLabel, priceLabel;
	// 定义图书编号、 名称、 数量、 价格文本框
	private JTextField idJTextField, nameJTextField, numJTextField, priceJTextField;
	// 定义文本对象
	private String bookIdText;
	private String bookNameText;
	private String bookNumText;
	private String bookPriceText;
	// 图书数量和价格
	private Integer numBook;
	private Float priceBook;
	// 定义对象BookDao
	private BookDao bookDao;

	public CrudBookPanel() {
		bookDao = new BookDao();//实例化图书操作对象
		// 实例化增删改查按钮
		addBtn = new JButton("增加图书");
		addBtn.addActionListener(this);// 设置图书增加按钮监听
		addBtn.setActionCommand("addbook");
		deleteBtn = new JButton("删除图书");
		deleteBtn.addActionListener(this);// 设置图书删除按钮监听
		deleteBtn.setActionCommand("deletebook");
		updateBtn = new JButton("修改图书");
		updateBtn.addActionListener(this);// 设置图书修改按钮监听
		updateBtn.setActionCommand("updatebook");
		findBtn = new JButton("查询图书");
		findBtn.addActionListener(this);// 设置图书查询按钮监听
		findBtn.setActionCommand("findbook");
		// 实例化图书编号 名称 数量 价格标签
		idLabel = new JLabel("图书编号");
		nameLabel = new JLabel("图书名称");
		priceLabel = new JLabel("图书价格");
		numLabel = new JLabel("图书数量");
		// 实例化文本框
		idJTextField = new JTextField(12);
		nameJTextField = new JTextField(12);
		numJTextField = new JTextField(12);
		priceJTextField = new JTextField(12);
		this.setLayout(new GridLayout(6, 2, 2, 2));
		// 给增删改查面板添加图书编号 名称 数量 价格标签以及文本框
		this.add(idLabel);
		this.add(idJTextField);
		this.add(nameLabel);
		this.add(nameJTextField);
		this.add(priceLabel);
		this.add(priceJTextField);
		this.add(numLabel);
		this.add(numJTextField);
		// 给增删改查面板添加图书编号 名称 数量 价格按钮
		this.add(addBtn);
		this.add(deleteBtn);
		this.add(updateBtn);
		this.add(findBtn);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getActionCommand().equals("addbook")) {// 添加图书
			addbook();
		} else if (e.getActionCommand().equals("deletebook")) {// 删除图书
			deleteBook();
		} else if (e.getActionCommand().equals("updatebook")) {// 修改图书
			updateBook();
		} else if (e.getActionCommand().equals("findbook")) {// 查询图书
			findBook();
		}
	}

	/**
	 * 查询图书
	 */
	private void findBook() {
		bookIdText = idJTextField.getText().trim().toString();
		Book book = bookDao.getBookById(bookIdText);
		if (bookIdText.equals("")) {
			JOptionPane.showMessageDialog(this, "图书编号不能为空");
		} else if (book != null) {// 当前输入的图书编号存在
			try {
				nameJTextField.setText(book.getName());// 填充图书名称文本框
				numJTextField.setText(book.getNum() + "");// 将数字类型转成字符串并填充文本框
				priceJTextField.setText(book.getPrice() + "");// 将数字类型转成字符串并填充文本框
			} catch (Exception e) {
				JOptionPane.showMessageDialog(this, "图书查询异常  请联系管理员");
			}
		} else {
			JOptionPane.showMessageDialog(this, "图书不存在");
		}

	}

	/**
	 * 修改图书
	 */
	private void updateBook() {
		bookIdText = idJTextField.getText().trim().toString();
		bookNameText = nameJTextField.getText().trim().toString();
		bookNumText = numJTextField.getText().trim().toString();
		bookPriceText = priceJTextField.getText().trim().toString();
		if (bookIdText.equals("")) {
			JOptionPane.showMessageDialog(this, "图书编号不能为空");
		} else if (bookNameText.equals("")) {
			JOptionPane.showMessageDialog(this, "图书名称不能为空");
		} else if (bookNumText.equals("")) {
			JOptionPane.showMessageDialog(this, "图书数量不能为空");
		} else if (bookPriceText.equals("")) {
			JOptionPane.showMessageDialog(this, "图书价格不能为空");
		} else {
			// 将图书数量和图书价格转成对应的数字类型
			if (bookDao.getBookById(bookIdText) == null) {// 图书不存在
				JOptionPane.showMessageDialog(this, "输入正确的图书编号");
			} else {
				try {
					numBook = Integer.parseInt(bookNumText);
					priceBook = Float.parseFloat(bookPriceText);
					bookDao.updateBook(new Book(bookIdText, bookNameText, numBook, priceBook));
					JOptionPane.showMessageDialog(this, "图书修改成功");
				} catch (Exception e) {
					JOptionPane.showMessageDialog(this, "输入正确的图书数量和价格");
					e.printStackTrace();
				}
			}
		}

	}

	/**
	 * 删除图书
	 */
	private void deleteBook() {
		bookIdText = idJTextField.getText().trim().toString();
		if (bookIdText.equals("")) {
			JOptionPane.showMessageDialog(this, "图书编号不能为空");
		} else if (bookDao.getBookById(bookIdText) != null) {// 当前输入的图书编号是否存在
			try {
				bookDao.deleteBootByid(bookIdText);
				JOptionPane.showMessageDialog(this, "图书删除成功");
			} catch (Exception e) {
				JOptionPane.showMessageDialog(this, "图书删除异常  请联系管理员");
			}
		} else {
			JOptionPane.showMessageDialog(this, "图书不存在");
		}

	}

	/**
	 * 增加图书
	 */
	private void addbook() {
		bookIdText = idJTextField.getText().trim().toString();
		bookNameText = nameJTextField.getText().trim().toString();
		bookNumText = numJTextField.getText().trim().toString();
		bookPriceText = priceJTextField.getText().trim().toString();
		if (bookIdText.equals("")) {
			JOptionPane.showMessageDialog(this, "图书编号不能为空");
		} else if (bookNameText.equals("")) {
			JOptionPane.showMessageDialog(this, "图书名称不能为空");
		} else if (bookNumText.equals("")) {
			JOptionPane.showMessageDialog(this, "图书数量不能为空");
		} else if (bookPriceText.equals("")) {
			JOptionPane.showMessageDialog(this, "图书价格不能为空");
		} else {
			// 将图书数量和图书价格转成对应的数字类型
			if (bookDao.getBookById(bookIdText) != null) {// 编号重复
				JOptionPane.showMessageDialog(this, "图书编号重复");
			} else {
				try {
					numBook = Integer.parseInt(bookNumText);
					priceBook = Float.parseFloat(bookPriceText);
					bookDao.insertBoot(new Book(bookIdText, bookNameText, numBook, priceBook));
					JOptionPane.showMessageDialog(this, "图书增加成功");
				} catch (Exception e) {
					JOptionPane.showMessageDialog(this, "输入正确的图书数量和价格");
					e.printStackTrace();
				}
			}
		}

	}
}

 

The book list

package bookmanage.view;

import java.awt.BorderLayout;
import java.util.ArrayList;
import java.util.Vector;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import bookmanage.dao.BookDao;
import bookmanage.model.Book;

/**
 * 自定义图书列表面板
 * @author com.javayihao.top
 */
public class ListPanel extends JPanel {
	// 从数据库中取出信息
	// rowData用来存放行数据
	// columnNames存放列名
	Vector rowData, columnNames;
	JTable jt = null;
	JScrollPane jsp = null;

	// 构造函数
	public ListPanel() {
		ArrayList<Book> books = new BookDao().getBookList();
		columnNames = new Vector();
		// 设置列名
		columnNames.add("图书编号");
		columnNames.add("图书名称");
		columnNames.add("图书价格");
		columnNames.add("图书数量");
		rowData = new Vector();
		for (int i = 0; i < books.size(); i++) {
			//实例化每一行数据
			Vector hang = new Vector();
			hang.add(books.get(i).getId());
			hang.add(books.get(i).getName());
			hang.add(books.get(i).getPrice());
			hang.add(books.get(i).getNum());
			// 加入到rowData
			rowData.add(hang);
		}
		// 初始化Jtable
		jt = new JTable(rowData, columnNames);
		// 初始化 jsp
		jsp = new JScrollPane(jt);
		this.add(jsp);
	}
}

database

/*
Navicat MySQL Data Transfer

Source Server         : localhost
Source Server Version : 50527
Source Host           : localhost:3306
Source Database       : db_book

Target Server Type    : MYSQL
Target Server Version : 50527
File Encoding         : 65001

Date: 2019-12-23 13:01:43
*/

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for t_book
-- ----------------------------
DROP TABLE IF EXISTS `t_book`;
CREATE TABLE `t_book` (
  `id` varchar(20) NOT NULL,
  `name` varchar(255) NOT NULL,
  `num` int(11) NOT NULL,
  `price` float(10,2) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of t_book
-- ----------------------------
INSERT INTO `t_book` VALUES ('123456', 'java入门到精通', '100', '22.00');
INSERT INTO `t_book` VALUES ('123457', 'c++实战', '100', '50.00');
INSERT INTO `t_book` VALUES ('123458', '微服务电商实战', '22', '1.00');

-- ----------------------------
-- Table structure for t_user
-- ----------------------------
DROP TABLE IF EXISTS `t_user`;
CREATE TABLE `t_user` (
  `accout` varchar(255) NOT NULL,
  `pass` varchar(255) NOT NULL,
  PRIMARY KEY (`accout`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of t_user
-- ----------------------------
INSERT INTO `t_user` VALUES ('123', '123');
INSERT INTO `t_user` VALUES ('123456', '123456');
INSERT INTO `t_user` VALUES ('1234567', '123456');
INSERT INTO `t_user` VALUES ('admin', 'admin');
INSERT INTO `t_user` VALUES ('dsfasdfa', 'afasdf');
INSERT INTO `t_user` VALUES ('fdsaf', 'adfas');

Source Access:

Was going on github, given the many small partners will not use github, lay in my public personal number, number of public attention java on the 1st reply "Book" button

How to run:

1. Under the first operating environment now, java + eclipse + mysql, java environment so first of all must have a local installation of the mysql database, the database on the graphical interface tool I use is navicat;

2. Create a database db_book, there db_book.sql get the code file in the file, Notepad to open the copy you just created to get db_book database query can be run directly

3. Open the eclipse

Click Run

friendly reminder

Project is mainly used to practice java object-oriented thinking, as to swing in java programming knowledge, which is the programming interface is recommended not to spend too much time landscaping design, there is no need, the market rarely use java Swing to compile cs software.

Help to the comment oh wave 666666

There are number of questions you can contact the public   java One

 

 

Published 260 original articles · won praise 112 · views 260 000 +

Guess you like

Origin blog.csdn.net/qq_34491508/article/details/103649573