资源描述
Java图书管理系统附源码
- 52 -
2020年4月19日
文档仅供参考
目 录
题目简述 -------------------------------- 01
需求分析 -------------------------------- 01
数据结构 -------------------------------- 01
功能模块 -------------------------------- 02
程序设计 -------------------------------- 02
运行截图 -------------------------------- 04
分析总结 -------------------------------- 08
程序源码 -------------------------------- 08
图书信息管理系统
题目简述:
题目名称:图书信息管理系统
要求:使用图形用户界面,用数据库建立1或2个图书信息表,能连接数据库并实现查询、增加、删除、修改等功能。
需求分析:
图书信息管理系统应该具备图书的信息管理功能和流通管理功能。其中,信息管理功能包括查找,增加,修改,删除,显示全部信息等模块。流通管理功能包括图书借阅,归还等模块。因此分别设计各个模块,实现不同的功能。
数据结构:
用SQL Sever 建立数据库的表,用一张表存放图书的ID号码,图书名称,图书的所有者,图书状态,图书的使用者(允许为空)。具体设计如下图所示:
图书ID,图书名称,图书所有者,图书的状态,图书使用者(允许为空)均为varchar(50)类型,在Java中能够方便的查询。图书存储信息的具体内容如下图所示:
功能模块:
主程序窗口
系统操作
图书信息管理
图书流通管理
退出
增加图书
显示图书
编辑图书
查找图书
删除图书
图书借阅
图书归还
程序设计:
主框架设计:
主框架上方包含三个按钮,分别是“系统管理”、“图书信息管理”和“图书流通管理”,定义JMenuBar类的对象、JMenu类的对象和JMenuItem类的对象,分别表示菜单栏、菜单组和菜单选项,然后调用初始化函数,将不同类的对象经过setText()函数设定不同的文本,然后将其添加到窗口容器中。对每个按钮分别添加不同的消息监听,响应相应的消息,调用不同的类完成不同的功能。消息监听功能详见源代码bookMain类。
图书信息管理模块包括增加图书,删除图书,编辑图书,查找图书和显示图书信息。具体的方法实现在bookBean中实现,当用户点击相应的按钮时,消息监听模块就会调用bookBean的构造函数产生一个bookBean的对象,然后经过对象调用bookBean类中的相应方法,完成事件的相应。
增加图书信息模块:
调用bookAdd类的构造函数产生一个该类的对象,在初始化函数中显示信息输入对话框,提示用户输入图书ID,图书名称和图书所有者,由于刚增加的图书没有被借阅,因此图书状态和图书使用者为默认值,分别为空闲和null,因此用户不必输入这两个属性值。完成输入后,点击确定按钮,消息监听模块将调用bookBean的构造函数产生一个该类的对象,经过该对象调用bookBean的bookAdd函数,执行SQL语句,经过insert语句完成图书信息的增加插入功能。
删除图书信息模块:
调用bookDel类的构造函数产生一个该类的对象,在初始化函数中显示信息输入对话框,提示用户输入要删除图书的ID。完成输入后,点击确定按钮,消息监听模块将调用bookBean的构造函数产生一个该类的对象,经过该对象调用bookBean的bookDel函数,执行删除的SQL语句,经过delete语句完成图书信息的删除功能。
修改图书信息模块:
点击按钮后调用bookEdit类的构造函数产生一个该类的对象,在初始化函数中显示信息输入对话框,提示用户输入要修改图书的ID和其它图书信息,用户将信息输入完成后,点击确定按钮,消息监听模块将调用bookBean的构造函数产生一个该类的对象,经过该对象调用bookBean的bookEdit函数,执行修改的SQL语句,经过update语句完成图书信息的编辑功能。
查找图书信息模块:
点击按钮后调用bookSearch类的构造函数产生一个该类的对象,在初始化函数中显示信息输入对话框,提示用户输入要查找图书的ID,用户将信息输入完成后,点击确定按钮,消息监听模块将调用bookResult的构造函数产生一个该类的对象,经过该对象的构造函数,执行查找的SQL语句,经过select语句完成图书信息的查找功能,然后构造图标,将查询到的信息显示在图表中。
显示图书信息模块:
点击按钮后调用bookDisplay类的构造函数产生一个该类的对象,在该对象的构造函数中调用bookAllSearch函数,查询所有的图书信息,显示在表格中。
图书流通管理模块包含图书的借阅与归还功能,其实就是对数据库中的某一记录集的某一属性进行修改。
图书借阅模块:
用户输入要借阅的图书名称和本人姓名,点击确定后,将调用bookBean的构造函数产生一个对象,经过该对象调用bookBook函数,修改数据库中的信息,将图书状态属性变为已借,将图书用户属性变为用户姓名,完成图书的借阅功能。
图书归还模块:
用户输入要归还的图书名称,点击确定后,将调用bookBean的构造函数产生一个对象,经过该对象调用bookReturn函数,修改数据库中的信息,把图书的状态置为空闲,将图书用户置为空,从而完成图书的归还功能。
备注:报告此部分未附源代码,详见程序源码部分。
运行截图:
图书信息增加:
图书信息修改:
图书信息查询:
图书信息删除:
图书信息显示:
图书借阅:
图书归还:
分析总结:
本程序在数据库设计方面能够改为多张表存储的方式,用三张表来记录图书信息,借阅关系和学生信息,这样能够减少数据冗余,还能够增加一些其它功能,比如图书挂失等功能。由于刚刚接触到Java界面设计,因此本程序在界面设计方面还有有待改进的地方。
程序源码:
DatabaseConn.java
package bookDB;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class DatabaseConn {
private Statement stmt = null;
ResultSet rs = null;
private Connection conn = null;
String sql;
public DatabaseConn(){ }
public void OpenConn() throws Exception {
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn = DriverManager.getConnection("jdbc:odbc:library");
}catch (Exception e) {System.err.println("数据库连接:"+e.getMessage());}
}
public ResultSet executeQuery(String sql){
stmt = null;
rs = null;
try{
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs = stmt.executeQuery(sql);
}catch (SQLException e){System.err.println("查询数据:"+e.getMessage());}
return rs;
}
public void executeUpdate(String sql){
stmt = null;
rs = null;
try{
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs = stmt.executeQuery(sql);
mit();
}catch (SQLException e){System.err.println("更新数据:"+e.getMessage());}
}
public void closeStmt(){
try {stmt.close();}catch (SQLException e){
System.err.println("释放对象:"+e.getMessage());
}
}
public void closeConn(){
try {conn.close();}catch (SQLException ex){
System.err.println("释放对象:"+ex.getMessage());
}
}
public static String toGBK(String str){
try{
if(str == null)
str = null;
else
str = new String(str.getBytes("ISO-8859-1"),"GBK");
}catch (Exception e){System.out.println(e);}
return str;
}
}
package bookDB;
import java.awt.AWTEvent;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
bookMain.java
public class bookMain extends JFrame implements ActionListener{
private static final long serialVersionUID = 1L;
//----------建立菜单栏----------
JMenuBar mainMenu = new JMenuBar();
//--------------建立系统管理菜单组--------------
JMenu menuSystem = new JMenu();
JMenuItem itemExit = new JMenuItem();
JMenu menubook = new JMenu();
JMenu menuevent = new JMenu();
JMenuItem itemAdd = new JMenuItem();
JMenuItem itemEdit = new JMenuItem();
JMenuItem itemDelete = new JMenuItem();
JMenuItem itemSelect = new JMenuItem();
JMenuItem itemBook = new JMenuItem();
JMenuItem itemReturn = new JMenuItem();
JMenuItem itemDisplay = new JMenuItem();
//------------创立窗体模板对象--------------
public static bookInfo m_stu = new bookInfo();
public bookMain(){
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.pack();
this.setSize(600,480);
this.setTitle("图书管理系统");
try{
Init();
}catch (Exception e) {e.printStackTrace();}
}
private void Init() throws Exception {
Container contentPane = this.getContentPane();
contentPane.setLayout(new BorderLayout());
//------添加菜单组-------
menuSystem.setText("系统操作");
menuSystem.setFont(new Font("Dialog",0,12));
menubook.setText("图书信息管理");
menubook.setFont(new Font("Dialog",0,12));
menuevent.setText("图书流通管理");
menuevent.setFont(new Font("Dialog",0,12));
//---------生成系统管理菜单组---------
itemExit.setText("退出");
itemExit.setFont(new Font("Dialog",0,12));
//--------------生成学生菜单组----------------
itemAdd.setText("增加图书");
itemAdd.setFont(new Font("Dialog",0,12));
itemEdit.setText("修改信息");
itemEdit.setFont(new Font("Dialog",0,12));
itemDelete.setText("删除图书");
itemDelete.setFont(new Font("Dialog",0,12));
itemSelect.setText("查询");
itemSelect.setFont(new Font("Dialog",0,12));
itemDisplay.setText("显示全部信息");
itemDisplay.setFont(new Font("Dialog",0,12));
//--------------生成管理事务菜单组----------------
itemBook.setText("图书借阅");
itemBook.setFont(new Font("Dialog",0,12));
itemReturn.setText("图书归还");
itemReturn.setFont(new Font("Dialog",0,12));
menuSystem.add(itemExit);
menubook.add(itemAdd);
menubook.add(itemEdit);
menubook.add(itemDelete);
menubook.add(itemSelect);
menubook.add(itemDisplay);
menuevent.add(itemBook);
menuevent.add(itemReturn);
//----组合菜单栏---
mainMenu.add(menuSystem);
mainMenu.add(menubook);
mainMenu.add(menuevent);
this.setJMenuBar(mainMenu);
//添加事件监听
itemExit.addActionListener(this);
itemAdd.addActionListener(this);
itemEdit.addActionListener(this);
itemDelete.addActionListener(this);
itemSelect.addActionListener(this);
itemBook.addActionListener(this);
itemReturn.addActionListener(this);
itemDisplay.addActionListener(this);
setVisible(true);
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){System.exit(0);}
}) ;
}
public void actionPerformed(ActionEvent e){
Object obj = e.getSource();
if(obj == itemExit){System.exit(0);}
else if(obj == itemAdd){
bookAdd asi = new bookAdd();
//asi.downInit();
asi.pack();
asi.setVisible(true);
}else if(obj == itemEdit){
bookEdit esi = new bookEdit();
//esi.downInit();
esi.pack();
esi.setVisible(true);
}else if(obj == itemDelete){
bookDel dsi = new bookDel();
//dsi.downInit();
dsi.pack();
dsi.setVisible(true);
}else if(obj == itemSelect){
bookSearch sbid = new bookSearch();
sbid.pack();
sbid.setVisible(true);
}else if(obj == itemBook){
bookBook sboo = new bookBook();
sboo.pack();
sboo.setVisible(true);
}else if(obj == itemReturn){
bookReturn sre = new bookReturn();
sre.pack();
sre.setVisible(true);
}else if(obj == itemDisplay){
bookDisplay sre = new bookDisplay();
sre.pack();
sre.setVisible(true);
}
}
public static void main(String[] args){new bookMain();}
}
bookAdd.java
package bookDB;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class bookAdd extends JFrame implements ActionListener{
Container c;
JLabel jLabel1 = new JLabel();
JLabel jLabel2 = new JLabel();
JLabel jLabel3 = new JLabel();
JLabel jLabel4 = new JLabel();
JLabel jLabel5 = new JLabel();
JTextField sid = new JTextField(10);
JTextField sname = new JTextField(10);
JTextField sowner = new JTextField(10);
JTextField sstatus = new JTextField(10);
JTextField suser = new JTextField(10);
JButton addconfirm = new JButton();
public bookAdd(){
this.setTitle("增加图书信息");
this.setResizable(false);
try{Init();}catch (Exception e){e.printStackTrace();}
//设置居中
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
this.setLocation((int)(screenSize.width - 400)/2,(int)(screenSize.height - 300)/2 +45);
}
public void Init() throws Exception{
this.setSize(300,500);
c = this.getContentPane();
c.setLayout(new FlowLayout());
jLabel1.setText("图书ID: ");
jLabel1.setFont(new Font("Dialog",0,12));
c.add(jLabel1);
sid.setText(null);
sid.setFont(new Font("Dialog",0,12));
c.add(sid);
jLabel2.setText("图书名称: ");
jLabel2.setFont(new Font("Dialog",0,12));
c.add(jLabel2);
sname.setText(null);
sname.setFont(new Font("Dialog",0,12));
c.add(sname);
jLabel3.setText("图书所有者: ");
jLabel3.setFont(new Font("Dialog",0,12));
c.add(jLabel3);
sowner.setText(null);
sowner.setFont(new Font("Dialog",0,12));
c.add(sowner);
addconfirm.setText("确认增加");
addconfirm.setFont(new Font("Dialog",0,12));
c.add(addconfirm);
addconfirm.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
Object obj = e.getSource();
if(obj == addconfirm){
bookBean rs = new bookBean();
rs.bookAdd(sid.getText(),sname.getText(),sowner.getText());
this.dispose();
}
}
}
bookInfo.java
package bookDB;
public class bookInfo {
String m_book_id;
String m_book_name;
String m_book_owner;
String m_book_status;
String m_book_user;
}
bookEdit.java
package bookDB;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class bookEdit extends JFrame implements ActionListener{
Container c;
JLabel jLabel1 = new JLabel();
JLabel jLabel2 = new JLabel();
JLabel jLabel3 = new JLabel();
JLabel jLabel4 = new JLabel();
JLabel jLabel5 = new JLabel();
JTextField sid = new JTextField(10);
JTextField sname = new JTextField(10);
JTextField sowner = new JTextField(10);
JTextField sstatus = new JTextField(10);
JTextField suser = new JTextField(10);
JButton editconfirm = new JButton();
public bookEdit(){
this.setTitle("修改图书信息");
this.setResizable(false);
try{Init();}catch (Exception e){e.printStackTrace();}
//设置居中
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
this.setLocation((int)(screenSize.width - 400)/2,(int)(screenSize.height - 300)/2 +45);
}
public void Init() throws Exception{
this.setSize(300,500);
c = this.getContentPane();
c.setLayout(new FlowLayout());
jLabel1.setText("要修改的图书ID: ");
jLabel1.setFont(new Font("Dialog",0,12));
c.add(jLabel1);
sid.setText(null);
sid.setFont(new Font("Dialog",0,12));
c.add(sid);
jLabel2.setText("图书名称: ");
jLabel2.setFont(new Font("Dialog",0,12));
c.add(jLabel2);
sname.setText(null);
sname.setFont(new Font("Dialog",0,12));
c.add(sname);
jLabel3.setText("图书所有者: ");
jLabel3.setFont(new Font("Dialog",0,12));
c.add(jLabel3);
sowner.setText(null);
sowner.setFont(new Font("Dialog",0,12));
c.add(sowner);
editconfirm.setText("确认修改");
editconfirm.setFont(new Font("Dialog",0,12));
c.add(editconfirm);
editconfirm.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
Object obj = e.getSource();
if(obj == editconfirm){
bookBean rs = new bookBean();
rs.bookModify(sid.getText(),sname.getText(),sowner.getText());
this.dispose();
}
}
}
bookDel.java
package bookDB;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class bookDel extends JFrame implements ActionListener{
Container c;
JLabel jLabel1 = new JLabel();
JTextField sid = new JTextField(10);
JButton delconfirm = new JButton();
public bookDel(){
this.setTitle("删除图书信息");
this.setResizable(false);
try{Init();}catch (Exception e){e.printStackTrace();}
//设置居中
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
this.setLocation((int)(screenSize.width - 400)/2,(int)(screenSize.height - 300)/2 +45);
}
public void Init() throws Exception{
this.setSize(300,500);
c = this.getContentPane();
c.setLayout(new FlowLayout());
jLabel1.setText("要删除的图书ID: ");
jLabel1.setFont(new Font("Dialog",0,12));
c.add(jLabel1);
sid.setText(null);
sid.setFont(new Font("Dialog",0,12));
c.add(sid);
delconfirm.setText("确认删除");
delconfirm.setFont(new Font("Dialog",0,12));
c.add(delconfirm);
delconfirm.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
Object obj = e.getSource();
if(obj == delconfirm){
bookBean rs = new bookBean();
rs.bookDel(sid.getText());
this.dispose();
}
}
}
bookDisplay.java
package bookDB;
import java.awt.*;
import javax.swing.*;
public class bookDisplay extends JFrame{
JLabel jLabel1 = new JLabel();
JButton jBExit = new JButton();
JScrollPane jScrollPane1;
JTable jTabstuInfo;
String sNum;
String[] 列名 = {"图书ID","图书名","图书所有者","图书状态","使用者"};
String [][] 列值;
String sColValue;
String sColName;
String sFromValue;
String sToValue;
public bookDisplay(){
this.setTitle("学生信息查询结果");
//--------设置运行位置居中---------
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
this.setLocation((int)(screenSize.width - 400)/2,(int)(screenSize.height - 300)/2 +45);
bookBean rstu = new
展开阅读全文