圖書管理系統是智能辦公系統的重要組成部分。目前,圖書管理系統正以方便、快捷的優點慢慢滲入人們的生活,將傳統的圖書管理方式徹底的解脫出來,提高管理效率,減輕管理人員的工作量,減小出概率,使讀者可以花更多的時間在選擇圖書上。從而使人們有更多時間來獲取信息、了解信息、掌握信息。采用數據庫技術生成的圖書管理系統將會極大的方便借閌者并簡化圖書館管理人員的勞動,使理人員從繁忙、復雜的工作進入到一個簡單.高效的工作中;谶@個問題,開發了圖書管理系統。系統實現了借還書的方便性、高效性、有效性和及時性。
public class MainFrame {
JFrame frame = new JFrame("個人書屋");
Container container = frame.getContentPane();
public MainFrame() {
// 設置背景圖片
new BackgroundImage(frame,container,"mainFrame.jpg");
// 添加工具欄以及各組件和監聽事件
new MenuBar(frame);
// 設置窗口大小、位置、可視、默認關閉方式等
new FrameOption(frame);
}
}
工具欄類
public class MenuBar {
JMenuBar menuBar;
JMenuItem menuItemBookInformation;
JMenuItem menuItemBorrowManage;
JMenuItem menuItemChangUser;
JMenuItem menuItemExit;
public MenuBar(JFrame frame) {
menuBar = new JMenuBar();
// 圖書信息管理菜單項
menuItemBookInformation = new JMenuItem();
setMenuItemBookInformationn(frame);
// 圖書借閱管理菜單項
menuItemBorrowManage = new JMenuItem();
setMenuItemBorrowManage(frame);
// 用戶信息更改菜單項
menuItemChangUser = new JMenuItem();
setMenuItemChangeUser(frame);
// 退出系統菜單項
menuItemExit = new JMenuItem();
setMenuItemExit(frame);
menuBar.add(menuItemBorrowManage);
menuBar.add(menuItemBookInformation);
menuBar.add(menuItemChangUser);
menuBar.add(menuItemExit);
frame.setJMenuBar(menuBar);
}
/**
* 設置退出系統菜單項
*/
private void setMenuItemExit(JFrame frame) {
menuItemExit.setIcon(new ImageIcon("res/menuItemExit.jpg"));
menuItemExit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// 使父窗體不可見
frame.setVisible(false);
new Login();
}
});
}
private void setMenuItemChangeUser(JFrame frame) {
menuItemChangUser.setIcon(new ImageIcon("res/menuItemChangePassword.jpg"));
menuItemChangUser.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// 使父窗體不可見
frame.setVisible(false);
new ChangeUserInformation();
}
});
}
private void setMenuItemBorrowManage(JFrame frame) {
menuItemBorrowManage.setIcon(new ImageIcon("res/menuBookCategoryManage.jpg"));
menuItemBorrowManage.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// 使父窗體不可見
frame.setVisible(false);
new BookBorrow();
}
});
}
private void setMenuItemBookInformationn(JFrame frame) {
menuItemBookInformation.setIcon(new ImageIcon("res/menuBookInformationManage.jpg"));
menuItemBookInformation.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// 使父窗體不可見
frame.setVisible(false);
new BookInformation();
}
});
}
}
設置窗口相關設置類
public class FrameOption {
public FrameOption(JFrame frame) {
frame.setVisible(true);
// 窗口不可調整大小
frame.setResizable(false);
frame.setSize(800, 508);
frame.setLocation(200,100);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
public class BookBorrow {
JFrame frame = new JFrame("個人書屋");
Container container = frame.getContentPane();
// 表格
JTable table;
// 顯示表格的滾動面板
JScrollPane scrollPane;
// 歸還圖書按鈕
JButton buttonReturn;
BorrowAction borrowAction;
public BookBorrow() {
frame.setLayout(null);
// 設置背景圖片
new BackgroundImage(frame,container,"BookBorrow.jpg");
// 添加工具欄以及各組件和監聽事件
new MenuBar(frame);
// 設置表格
setTable();
// 歸還圖書按鈕
buttonReturn = new JButton();
setButtonReturn();
container.add(buttonReturn);
container.add(scrollPane);
// 設置窗口大小、位置、可視、默認關閉方式等
new FrameOption(frame);
}
/**
* 設置歸還圖書按鈕
*/
private void setButtonReturn() {
buttonReturn.setBounds(580,390,100,25);
buttonReturn.setIcon(new ImageIcon("res/button_return.jpg"));
buttonReturn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
borrowAction = new BorrowAction();
borrowAction.BorrowBook(table);
frame.setVisible(false);
new BookBorrow();
} catch(Exception e1) {
JOptionPane.showMessageDialog(null,"請先選中要歸還的表格項","錯誤"
, JOptionPane.PLAIN_MESSAGE);
}
}
});
}
/**
* 設置表格
*/
private void setTable() {
String[] columnNames = {"ID", "圖書名稱", "借書人姓名", "借書人電話"};
try {
BorrowAction borrowAction = new BorrowAction();
Object[][] results = borrowAction.initializTable(columnNames);
table = new JTable(results,columnNames);
new SetTableColumnCenter(table);
scrollPane = new JScrollPane(table);
scrollPane.setViewportView(table);
scrollPane.setBounds(20,80,760,190);
} catch(Exception e) {
e.printStackTrace();
}
}
}
借還書相應行為類
public class BorrowAction {
BookDao borrowerDao;
/**
* 初始化借還書管理窗體表格
* @return
* results
*/
@SuppressWarnings("rawtypes")
public Object[][] initializTable(String[] columnNames) throws Exception{
borrowerDao = new BookDao();
List list = borrowerDao.borrowQuery();
Object[][] results = new Object[list.size()][columnNames.length];
for(int i = 0; i < list.size(); i++) {
Book book = (Book)list.get(i);
results[ i][0] = book.getID();
results[ i][1] = book.getBookName();
results[ i][2] = book.getBorrowerName();
results[ i][3] = book.getBorrowerPhone();
}
return results;
}
/**
* 歸還圖書
*/
public void BorrowBook (JTable table) throws Exception{
borrowerDao=new BookDao();
Book book=new Book();
int selRow = table.getSelectedRow();
int ID = Integer.parseInt(table.getValueAt(selRow, 0).toString());
book.setID(ID);
book.setBorrowerName(null);
book.setBorrowerPhone(null);
// 歸還圖書
borrowerDao.returnBook(book);
}
}
借還書數據庫操作類
/**
* 查詢借閱信息
*
* @return
* bookList
*/
public List<Book> borrowQuery() throws Exception{
Connection con = DBUtil.getConnection();
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(""
// ID、書名、借書人姓名、借書人電話
+ "SELECT ID, book_name, borrower_name, borrower_phone "
+ "FROM tb_books "
+ "WHERE borrower_name IS NOT NULL"
);
List<Book> bookList = new ArrayList<Book>();
Book book = null;
while (rs.next()){//如果對象中有數據,就會循環打印出來
book = new Book();
book.setID(rs.getInt("ID"));
book.setBookName(rs.getString("book_name"));
book.setBorrowerName(rs.getString("borrower_name"));
book.setBorrowerPhone(rs.getString("borrower_phone"));
bookList.add(book);
}
return bookList;
}
/**
* 更新圖書信息,歸還圖書
*/
public void returnBook(Book book) throws SQLException{
Connection con=DBUtil.getConnection();//首先拿到數據庫的連接
String sql="UPDATE tb_books "
// ISBN、圖書名稱、作者、價格
+ "SET "
// 借書人姓名、借書人電話
+ "borrower_name = ?, borrower_phone = ? "
// 參數用?表示,相當于占位符
+ "WHERE ID = ?";
// 預編譯sql語句
PreparedStatement psmt = con.prepareStatement(sql);
// 先對應SQL語句,給SQL語句傳遞參數
psmt.setString(1, book.getBorrowerName());
psmt.setString(2, book.getBorrowerPhone());
psmt.setInt(3, book.getID());
// 執行SQL語句
psmt.execute();
}
@SuppressWarnings("serial")
public class BookInformation extends JFrame {
JFrame frame = new JFrame("個人書屋");
Container container = frame.getContentPane();
// 增加、刪除、修改按鈕
JButton buttonAdd, buttonDel, buttonChange,buttonReset;
// ISBN、圖書名稱、圖書價格、圖書作者文本框
JTextField textFieldISBN, textFieldBookName, textFieldPrice, textFieldAuthor;
// 出版社、圖書分類號、借書人姓名文本框
JTextField textFieldPublishedHouse, textFieldBookCategory;
// 借書人姓名、借書人電話
JTextField textFieldBorrowName, textFieldBorrowPhone;
// 表格
JTable table;
// 顯示表格的滾動面板
JScrollPane scrollPane;
BookAction bookAction;
public BookInformation() {
frame.setLayout(null);
// 設置背景圖片
new BackgroundImage(frame,container,"BookInformation.jpg");
// 添加工具欄以及各組件和監聽事件
new MenuBar(frame);
bookAction = new BookAction();
// ISBN文本框
textFieldISBN = new JTextField();
setTextFieldISBN();
// 圖書名稱文本框
textFieldBookName = new JTextField();
setTextFieldBookName();
// 圖書價格文本框
textFieldPrice = new JTextField();
setTextFieldBookPrice();
// 圖書作者文本框
textFieldAuthor = new JTextField();
setTextFieldAuthor();
// 出版社文本框
textFieldPublishedHouse = new JTextField();
setTextFieldPublishedHouse();
// 圖書分類號文本框
textFieldBookCategory = new JTextField();
setTextFieldBookCategory();
// 借書人姓名文本框
textFieldBorrowName = new JTextField();
setTextFieldBorrowName();
// 借書人電話文本框
textFieldBorrowPhone = new JTextField();
setTextFieldBorrowPhone();
// 設置窗體表格
setTable();
// 增加按鈕
buttonAdd = new JButton();
setButtonAdd();
// 刪除按鈕
buttonDel = new JButton();
setButtonDel();
// 修改按鈕
buttonChange = new JButton();
setButtonChange();
// 重置按鈕
buttonReset = new JButton();
setButtonReset();
container.add(scrollPane);
container.add(buttonAdd);
container.add(buttonDel);
container.add(buttonReset);
container.add(buttonChange);
container.add(textFieldISBN);
container.add(textFieldBookName);
container.add(textFieldAuthor);
container.add(textFieldPrice);
container.add(textFieldBookCategory);
container.add(textFieldPublishedHouse);
container.add(textFieldBorrowName);
container.add(textFieldBorrowPhone);
// 設置窗口大小、位置、可視、默認關閉方式等
new FrameOption(frame);
}
/**
* 設置借書人電話文本框
*/
private void setTextFieldBorrowPhone() {
textFieldBorrowPhone.setBounds(490,312,232,23);
}
/**
* 設置借書人姓名文本框
*/
private void setTextFieldBorrowName() {
textFieldBorrowName.setBounds(150,312,200,23);
}
/**
* 設置修改按鈕
*/
private void setButtonChange() {
buttonChange.setBounds(470,390,60,25);
buttonChange.setIcon(new ImageIcon("res/button_change.jpg"));
buttonChange.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
bookAction.changeBookInformation(textFieldISBN, textFieldBookName
,textFieldPrice, textFieldAuthor, textFieldPublishedHouse
, textFieldBookCategory, textFieldBorrowName, textFieldBorrowPhone, table);
frame.setVisible(false);
new BookInformation();
}catch(Exception e1) {
JOptionPane.showMessageDialog(null,"表中沒有該數據","錯誤"
, JOptionPane.PLAIN_MESSAGE);
}
}
});
}
/**
* 設置圖書分類號文本框
*/
private void setTextFieldBookCategory() {
textFieldBookCategory.setBounds(582,340,140,23);
}
/**
* 設置出版社文本框
*/
private void setTextFieldPublishedHouse() {
textFieldPublishedHouse.setBounds(348,340,140,23);
}
/**
* 設置圖書作者文本框
*/
private void setTextFieldAuthor() {
textFieldAuthor.setBounds(586,280,140,23);
}
/**
* 設置圖書價格文本框
*/
private void setTextFieldBookPrice() {
textFieldPrice.setBounds(120,340,140,23);
}
/**
* 設置圖書名稱文本框
*/
private void setTextFieldBookName() {
textFieldBookName.setBounds(348,280,140,23);
}
/**
* 設置ISBN文本框
*/
private void setTextFieldISBN() {
// 限制文本框長度為17
textFieldISBN.setDocument(new LimitTextLength(17));
textFieldISBN.setBounds(120,280,140,23);
}
/**
* 設置刪除按鈕
*/
private void setButtonDel() {
buttonDel.setBounds(580,390,60,25);
buttonDel.setIcon(new ImageIcon("res/button_del.jpg"));
buttonDel.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
try {
bookAction.delBookInformation(table);
frame.setVisible(false);
new BookInformation();
} catch(Exception e1) {
e1.printStackTrace();
}
}
});
}
/**
* 設置文本框重置按鈕
*/
private void setButtonReset() {
buttonReset.setBounds(270,390,150,25);
buttonReset.setIcon(new ImageIcon("res/button_textReset.jpg"));
buttonReset.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
textFieldBookName.setText("");
textFieldAuthor.setText("");
textFieldISBN.setText("");
textFieldPrice.setText("");
textFieldBookCategory.setText("");
textFieldPublishedHouse.setText("");
}
});
}
/**
* 設置添加按鈕
*/
private void setButtonAdd() {
buttonAdd.setBounds(700,390,60,25);
buttonAdd.setIcon(new ImageIcon("res/button_add.jpg"));
buttonAdd.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(textFieldISBN.getText().length() == 0) {
JOptionPane.showMessageDialog(null,"ISBN號不能為空","錯誤"
, JOptionPane.PLAIN_MESSAGE);
}
else if(textFieldISBN.getText().length() != 17) {
JOptionPane.showMessageDialog(null,"ISBN號位數必須是13位","錯誤"
, JOptionPane.PLAIN_MESSAGE);
}
else if(textFieldBookName.getText().length() == 0) {
JOptionPane.showMessageDialog(null, "圖書名稱不能為空", "錯誤"
, JOptionPane.PLAIN_MESSAGE);
}
else if(textFieldAuthor.getText().length() == 0) {
JOptionPane.showMessageDialog(null, "圖書作者不能為空", "錯誤"
, JOptionPane.PLAIN_MESSAGE);
}
else if(textFieldPrice.getText().length() == 0) {
JOptionPane.showMessageDialog(null, "圖書價格不能為空", "錯誤"
, JOptionPane.PLAIN_MESSAGE);
}
else if(textFieldPublishedHouse.getText().length() == 0) {
JOptionPane.showMessageDialog(null, "出版社不能為空", "錯誤"
, JOptionPane.PLAIN_MESSAGE);
}
else if(textFieldBookCategory.getText().length() == 0) {
JOptionPane.showMessageDialog(null, "圖書分類號不能為空", "錯誤"
, JOptionPane.PLAIN_MESSAGE);
}
else if(textFieldPrice.getText().length() > 4) {
JOptionPane.showMessageDialog(null, "圖書價格不能超過4位數", "錯誤"
, JOptionPane.PLAIN_MESSAGE);
}
else {
try {
bookAction.addBookInformation(textFieldISBN, textFieldBookName, textFieldPrice,textFieldAuthor
,textFieldPublishedHouse,textFieldBookCategory,textFieldBorrowName,textFieldBorrowPhone);
frame.setVisible(false);
new BookInformation();
}catch(Exception e1) {
e1.printStackTrace();
}
}
}
});
}
/**
* 設置窗體表格
*/
private void setTable() {
String[] columnNames = {"ID", "圖書名稱","圖書作者","圖書價格(元)","ISBN"
,"出版社","分類號", "借書人姓名", "借書人電話"
};
try {
BookAction bookAction = new BookAction();
Object[][] results = bookAction.initializTable(columnNames);
table = new JTable(results,columnNames);
// 設置表格字段居中
new SetTableColumnCenter(table);
scrollPane = new JScrollPane(table);
scrollPane.setViewportView(table);
scrollPane.setBounds(20,80,760,190);
table.addMouseListener(new MouseListener() {
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
String ISBN, bookName, price, author;
String publishedHouse, category, borrowName, borrowPhone;
int selRow = table.getSelectedRow();
bookName = table.getValueAt(selRow, 1).toString();
author = table.getValueAt(selRow, 2).toString();
price = table.getValueAt(selRow, 3).toString();
ISBN = table.getValueAt(selRow, 4).toString();
publishedHouse = table.getValueAt(selRow, 5).toString();
category = table.getValueAt(selRow, 6).toString();
if (table.getValueAt(selRow, 7).toString() == null) {
borrowName = "";
}
else {
borrowName = table.getValueAt(selRow, 7).toString();
}
if (table.getValueAt(selRow, 8).toString() == null) {
borrowPhone = "";
}
else {
borrowPhone = table.getValueAt(selRow, 8).toString();
}
textFieldBookName.setText(bookName);
textFieldAuthor.setText(author);
textFieldPrice.setText(price);
textFieldISBN.setText(ISBN);
textFieldPublishedHouse.setText(publishedHouse);
textFieldBookCategory.setText(category);
textFieldBorrowName.setText(borrowName);
textFieldBorrowPhone.setText(borrowPhone);
}
});
} catch(Exception e) {
e.printStackTrace();
}
}
}
|