数据库课设:简易图书管理系统
实验设计开发一个简单的图书管理系统。包括图书馆内的书籍信息和读者信息以及相关的借阅信息。用户面向管理员和读者,管理员可以进行对数据库中图书的增删改查。读者可以进行图书的查询、借阅,续借和归还功能。主要表:系统功能模块图:ER图:建立数据库及其相关对象:创建表空间和用户:CREATE TABLESPACE BOOKSYSTEMDATAFILE 'D:\study\SQL\BOOKSYSTEM_DAT
·
源码:https://download.csdn.net/download/qq_45664450/14504336?spm=1001.2014.3001.5501
实验设计开发一个简单的图书管理系统。包括图书馆内的书籍信息和读者信息以及相关的借阅信息。用户面向管理员和读者,管理员可以进行对数据库中图书的增删改查。读者可以进行图书的查询、借阅,续借和归还功能。
主要表:
系统功能模块图:
ER图:
建立数据库及其相关对象:
创建表空间和用户:
CREATE TABLESPACE BOOKSYSTEM
DATAFILE 'D:\study\SQL\BOOKSYSTEM_DATA.DBF'SIZE 50M;
CREATE USER BOOKMANAGER identified by BOOKMANAGER DEFAULT TABLESPACE BOOKSYSTEM;
GRANT DBA TO BOOKMANAGER;
创建图书表:
create table BOOKS
(
bnumber NUMBER(10) not null,
bname VARCHAR2(20) not null,
bauthor VARCHAR2(20),
bpublish VARCHAR2(50),
bsum NUMBER(3),
bleft NUMBER(2),
btime DATE,
baddress VARCHAR2(50),
constraint PK_BOOKS primary key ( bnumber )
);
创建读者表:
create table READERS
(
rnumber NUMBER(10) not null,
rname VARCHAR2(20) not null,
password NUMBER(10) not null,
rsex CHAR(4),
rclass VARCHAR2(10),
rsum NUMBER(10),
constraint PK_READERS primary key ( rnumber )
);
创建借阅表:
create table BORROW
(
bnumber NUMBER(10) not null,
rnumber NUMBER(10) not null,
borrowtime DATE not null,
returntime DATE,
"comment" VARCHAR2(50),
constraint PK_BORROW primary key ( bnumber , rnumber , borrowtime )
);
alter table BORROW
add constraint FK_BORROW_BORROW_BOOKS foreign key ( bnumber )
references BOOKS ( bnumber );
alter table BORROW
add constraint FK_BORROW_BORROW_READERS foreign key ( rnumber )
references READERS ( rnumber );
创建序列:
create sequence seq_bnumber start with 1 increment by 1;
create sequence seq_rnumber start with 1101 increment by 1;
创建索引:
create index Borrow_index1 on BORROW ( bnumber ASC );
create index Borrow_index2 on BORROW ( rnumber ASC );
创建管理员表:
create table Users
(
id NUMBER(10) not null,
username VARCHAR2(20) not null,
pwd NUMBER(10) not null,
constraint PK_MANAGERS primary key ( id )
);
create sequence seq_usersId start with 202001 increment by 1;
数据库连接
package Jdbcutils;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class Jdbccon {
private static Connection conn;
private static String url="jdbc:oracle:thin:@localhost:1521:xe"; //连接字符串
private static String user="BOOKMANAGER"; //连接用户
private static String password="BOOKMANAGER";
//注册驱动
static{
try {
Class.forName("oracle.jdbc.OracleDriver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//建立连接
public static Connection getConnection(){
try {
conn=DriverManager.getConnection(url, user, password);
return conn;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
//关闭连接
public static void closeConnection(){
if(conn!=null) //关闭conn对象
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
以一个主要功能模块为例说明设计思想(MVC模式):
图书归还,续借功能模块中M层设计(DAO设计)
//针对图书Books表的CRUD操作,设计了doReturn方法:
//归还图书时,图书剩余数量+1:
public boolean doReturn(Books b){
con=Jdbccon.getConnection();
try {
String sql="update books set bleft=bleft+1 where bnumber=?";
pstmt=con.prepareStatement(sql);
pstmt.setInt(1,b.getBnumber());
int result=pstmt.executeUpdate();
return result>0;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}finally{
Jdbccon.closeConnection();
}
}
//针对读者Readers表的CRUD操作,设计了doReturn方法:
//归还图书时,读者在借图书数量-1:
public boolean doReturn(Readers r){
con=Jdbccon.getConnection();
try {
String sql="update readers set rsum=rsum-1 where rnumber=?";
pstmt=con.prepareStatement(sql);
pstmt.setInt(1,r.getRnumber());
int result=pstmt.executeUpdate();
return result>0;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}finally{
Jdbccon.closeConnection();
}
}
//针对借阅borrow表的CRUD操作,设计了doDelete和doLending方法:
//归还图书时,删除该图书被该读者借阅的记录:
public boolean doDelete(Borrow w){
con=Jdbccon.getConnection();
try {
String sql="delete from Borrow where bnumber=? and rnumber= ?";
pstmt=con.prepareStatement(sql);
pstmt.setInt(1,w.getBnumber());
pstmt.setInt(2, w.getRnumber());
int result=pstmt.executeUpdate();
return result>0;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}finally{
Jdbccon.closeConnection();
}
}
// 续借图书时,修改应还图书日期:
public boolean doLending(Borrow w){
con=Jdbccon.getConnection();
Calendar cal=Calendar.getInstance();
cal.setTime(w.getComments());//获取原本应还书日期
cal.add(Calendar.MONTH,2);//增加两个月
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
String time=sdf.format(cal.getTime());
try {
String sql="update Borrow set comments=? where bnumber=? and rnumber=?";
pstmt=con.prepareStatement(sql);
pstmt.setDate(1,java.sql.Date.valueOf(time));
pstmt.setInt(2,w.getBnumber());
pstmt.setInt(3, w.getRnumber());
int result=pstmt.executeUpdate();
return result>0;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}finally{
Jdbccon.closeConnection();
}
}
图书续借,归还功能模块中V层设计(界面设计):
setTitle("\u56FE\u4E66\u5F52\u8FD8 \u7EED\u501F");
setBounds(100, 100, 628, 453);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
setLocationRelativeTo(null); //窗体居中
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(15, 15, 576, 234);
contentPane.add(scrollPane);
BorrowDao wdao=new BorrowDao();
ReadersDao rdao=new ReadersDao();
Readers r=new Readers();
Vector columnNames=wdao.findColumnNames();
Vector rowData=new Vector();
String rname=ReaderLoginFrame.name;//获取使用者的用户名
r=rdao.findByname(rname);
int rnumber=r.getRnumber();//根据用户名获取学号
rowData=wdao.findByNumber(rnumber);//根据学号查找该用户的借阅记录
table = new JTable(rowData,columnNames);//读取借阅表
scrollPane.setViewportView(table);
JButton buttonReturn = new JButton("\u5F52\u8FD8");//归还
buttonReturn.setFont(new Font("宋体", Font.BOLD, 18));
buttonReturn.setBounds(109, 317, 123, 29);
contentPane.add(buttonReturn);
JButton buttonlend = new JButton("\u7EED\u501F");//续借
buttonlend.setFont(new Font("宋体", Font.BOLD, 18));
buttonlend.setBounds(381, 317, 123, 29);
contentPane.add(buttonlend);
图书续借,归还功能模块中C层设计(控制层设计):
实现buttonreturn的归还操作:
public void actionPerformed(ActionEvent e) {
BooksDao bdao=new BooksDao();
Books b=new Books();
Readers r=new Readers();
Borrow w=new Borrow();
int rowNum=table.getSelectedRow();
if(rowNum<0||rowNum>table.getRowCount()){
JOptionPane.showMessageDialog(null, "未选中", "提示",JOptionPane.PLAIN_MESSAGE);
}else{
int n = JOptionPane.showConfirmDialog(null, "确认归还吗?", "确认框", JOptionPane.YES_NO_OPTION);
if (n == JOptionPane.YES_OPTION)
{
int bNum=(int)table.getValueAt(rowNum, 0);
int RNum=(int)table.getValueAt(rowNum, 1);
r=rdao.findByname(rname);
//rdao.doReturn(r);
b=bdao.findBynumber(bNum);
//bdao.doReturn(b);
w=wdao.findBynumber(bNum,RNum);
if(wdao.doDelete(w)&&rdao.doReturn(r)&&bdao.doReturn(b)){
JOptionPane.showMessageDialog(null,"归还成功", "提示",JOptionPane.PLAIN_MESSAGE);
}else{
JOptionPane.showMessageDialog(null,"归还失败", "提示",JOptionPane.PLAIN_MESSAGE);
}
}
}
}
});
实现buttonlend的续借操作:
public void actionPerformed(ActionEvent e) {
ReadersDao rdao=new ReadersDao();
Readers r=new Readers();
BooksDao bdao=new BooksDao();
Books b=new Books();
Borrow w=new Borrow();
int rowNum=table.getSelectedRow();
if(rowNum<0||rowNum>table.getRowCount())
{
JOptionPane.showMessageDialog(null,"未选中", "提示",JOptionPane.PLAIN_MESSAGE);
} else {
int n = JOptionPane.showConfirmDialog(null, "确认续借吗?", "确认框", JOptionPane.YES_NO_OPTION);
if (n == JOptionPane.YES_OPTION)
{
int bNum=(int)table.getValueAt(rowNum, 0);
int rNum=(int)table.getValueAt(rowNum, 1);
Date btime=(Date) table.getValueAt(rowNum, 2);
Date comments=(Date) table.getValueAt(rowNum, 4);
r=rdao.findBynum(rNum);
b=bdao.findBynumber(bNum);
Calendar cal1=Calendar.getInstance();
Calendar cal2=Calendar.getInstance();
cal1.setTime(btime);
cal2.setTime(comments);
int bmonth=cal1.get(Calendar.MONTH)+1;
int cmonth=cal2.get(Calendar.MONTH)+1;
w=wdao.findBynumber(bNum, rNum);
if((cmonth-bmonth)==2){
if(wdao.doLending(w)){
JOptionPane.showMessageDialog(null,"续借成功", "提示",JOptionPane.PLAIN_MESSAGE);
}else{
JOptionPane.showMessageDialog(null,"续借失败", "提示",JOptionPane.PLAIN_MESSAGE);
}
}else{
JOptionPane.showMessageDialog(null,"您已续借过一次不可再续借", "提示",JOptionPane.PLAIN_MESSAGE);
}
}else if (n == JOptionPane.NO_OPTION)
{
return;
}
}
}
});
更多推荐
所有评论(0)