目錄
運行效果:
註冊登陸界面
註冊存在的賬戶時
登陸之後主界面如下
點擊圖書管理-圖書更新界面如下
圖書列表
項目說明:
由於平時工作比較忙,也沒時間寫,可是我在公衆號後臺看見好多小夥伴討論,我就抽時間寫個當作參考。本系統界面我個人就從簡設計了,本來打算使用windowbuilder插件設計的,可想到使用windowbuilder插件之後導致代碼冗餘,會影響到代碼可讀性,可能對小白不友好。雖然界面設計簡單,但是功能上我會盡量想着寫全,當然對於頁面你可以增加自己的設計,比如增加一個背景圖片等,網上都有指導,對於界面美觀度從簡了。
關鍵代碼:
用戶登陸
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;// 失敗
}
}
圖書更新
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();
}
}
}
}
}
圖書列表
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);
}
}
數據庫
/*
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');
源碼獲取:
本來打算放在github上的,考慮到許多小夥伴不會用github,就放在我個人公衆號,關注公衆號 java一號 回覆 “圖書” 即可
如何運行:
1. 先說下運行環境吧,java+eclipse+mysql,所以首先java環境得有,本地安裝了mysql數據庫,關於數據庫圖形化界面工具我用得是navicat;
2. 創建數據庫db_book,在獲取代碼文件中有個db_book.sql文件,記事本打開復制到剛纔創建得db_book數據庫直接查詢運行即可
3. 打開eclipse
點擊運行
友情提示
項目主要是用來練習java的面向對象思想,至於java中的swing編程知識,也就是界面編程建議不要花過多時間美化設計,完全沒有必要,市場上很少會使用java Swing去編cs軟件。
幫到了 評論一波666666哦
有問題可以聯繫 公衆號 java一號