基于java+mysql的swing+mysql图书管理系统图书借阅(java+swing+mysql)

基于java+mysql的swing+mysql图书管理系统图书借阅(java+swing+mysql)

私信源码获取及调试交流

运行环境

Java≥8、MySQL≥5.7

开发工具

eclipse/idea/myeclipse/sts等均可配置运行

适用

课程设计,大作业,毕业设计,项目练习,学习演示等

功能说明

基于java的Swing+MySQL图书管理系统图书借阅(java+swing+mysql)

管理员:

admin 123456

用户:

Jerry 123456

Tom 123456

图书管理系统

图书管理系统进行了对图书的增删改查,对用户的增删改,分为用户和管理员两个界面,管理员比用户多了两个选项卡来进行账户和图书的管理

						if(flag == 1) {
							new Admin(name);
							f.dispose();
						}else {
							JOptionPane.showMessageDialog(null, "对不起,您还不是管理员", "警告", JOptionPane.WARNING_MESSAGE);
						}
					}
				}else if(flag == 0) {//密码输入错误
					JOptionPane.showMessageDialog(null, "密码输入有误", "抱歉", JOptionPane.PLAIN_MESSAGE);
				}else if (flag == 2) {
					JOptionPane.showMessageDialog(null, "用户名不存在","注意" , JOptionPane.PLAIN_MESSAGE);
				}
			}
		});
		
		//设置不可改变窗口大小
		f.setResizable(false);
		f.setVisible(true);
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	public int select(String username1,String password1) throws Exception {
		boolean flag = false;
		String sql = "select * from user where username=?";
		Connection con;
		PreparedStatement ps ;
		ResultSet rs;
		ConnectData cd = new ConnectData();
		con = cd.connect();
		ps = con.prepareStatement(sql);
		ps.setString(1, username1) ;
		rs = ps.executeQuery();
		String s1 = null;
		while(rs.next()) {
			flag = true;
			s1 = rs.getString(2);
		}
		if(!flag) {//用户名不存在
			con.close();
			return 2;
		}
		else if(s1.equals(password1)) {
			return 1;
		}else {
			return 0;
		}
	}
	public int ensure(String username) throws Exception{
		String sql = "select * from user where username=?";
		Connection con;
		PreparedStatement ps ;
		ResultSet rs;
		labelname.setFont(font);
		panel5.add(labelname);
		
		/*输入姓名文本框*/
		JTextField textname1 = new JTextField();
		textname1.setPreferredSize(dimension);
		panel5.add(textname1);
		panel5.setBounds(10, 270, 300, 70);
		f.add(panel5);
		
		
		/*添加注册按钮*/
		JButton button = new JButton("注册");
		Font font5 = new Font("宋体",Font.BOLD,25);
		button.setFont(font5);
		Dimension dimension2 = new Dimension(200,40);
		button.setPreferredSize(dimension2);
		button.setBackground(Color.pink);
		panel6.add(button);
		panel6.setBounds(100, 340, 200, 50);
		f.add(panel6);
		
		//把组件设置为透明
		textname.setOpaque(false);
		textpassword.setOpaque(false);
		textpassword1.setOpaque(false);
		textname1.setOpaque(false);
		panel1.setOpaque(false);
		panel2.setOpaque(false);
		panel3.setOpaque(false);
		panel4.setOpaque(false);
		panel5.setOpaque(false);
		panel6.setOpaque(false);
		

		/*返回登陆界面*/
		rebutton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				f.dispose();
				SignMenu sm = new SignMenu();
				sm.init();
			}
		});
		
		/*注册信息存入数据库*/
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
					username = textname.getText().trim();
					password = textpassword.getText().trim();
		panel2.setBounds(100, 90, 500, 50);
		
		/*作者标签*/
		JLabel labauthor = new JLabel("作者:");
		labauthor.setFont(font);
		panel3.add(labauthor);
		panel3.setBounds(50, 160, 200, 60);
		
		/*作者输入文本框*/
		JTextField textauthor = new JTextField(12);
		panel4.setLayout(null);
		textauthor.setSize(dimension);
		textauthor.setFont(font1);
		panel4.add(textauthor);
		panel4.setBounds(100, 220, 500, 50);
		
		
		/*添加按钮*/
		JButton button = new JButton("确定添加");
		button.setPreferredSize(dimension1);
		button.setBackground(Color.PINK);
		button.setFont(new Font("宋体",Font.BOLD,25));
		panel5.add(button);
		panel5.setBounds(150, 300, 200, 100);
		
		button.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				String book = textbook.getText().trim();
				String author = textauthor.getText().trim();
				String lend = "是";
				ManageBook mb = new ManageBook();
				try {
					mb.addBook(book, author,lend);
					JOptionPane.showMessageDialog(null, "添加成功", "添加", JOptionPane.PLAIN_MESSAGE);
					
					BookData bd = new BookData();
					try {
						model.setRowCount(0);
						bd.selectAll(model);
					} catch (Exception e1) {
						// TODO Auto-generated catch block
			
			/*面板3中的搜索框*/
			JTextField text3 = new JTextField();
			Dimension dimension1 = new Dimension(270,30);
			text3.setFont(font1);
			text3.setSize(dimension1);
			text3.setBackground(Color.pink);
			text3.setBounds(390,100, 270, 30);
			
			/*按钮*/
			JButton button = new JButton("搜索");
			button.setBackground(Color.pink);
			button.setBounds(670, 100, 120, 30);
			button.setFont(font2);
			
			model = new DefaultTableModel();
			model.addColumn("图书编号", new Vector<Integer>());
			model.addColumn("书名", new Vector<String>());
			model.addColumn("借阅人", new Vector<String>());
			model.addColumn("日期", new Vector<Date>());
			model.addColumn("借阅状态", new Vector<String>());
			JTable table = new JTable(model);
			
			jp = new JScrollPane(table);
			jp.setBounds(90, 160, 800, 500);
			
			/*显示所有结束记录*/
			ABorrowHistory abh = new ABorrowHistory();
			try {
				abh.allHistory(model);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			/*下拉框添加事件*/
			select = "按照书名查询";
			j.addItemListener(new ItemListener() {
				
				@Override
				public void itemStateChanged(ItemEvent e) {
					
					if(e.getStateChange()==ItemEvent.SELECTED) {
		panel2.setLayout(null);
		textname.setSize(dimension);
		textname.setFont(font1);
		panel2.add(textname);
		panel2.setBounds(100, 180, 500, 50);
		
		
		
		
		/*添加按钮*/
		JButton button = new JButton("确定删除");
		button.setPreferredSize(dimension);
		button.setBackground(Color.PINK);
		button.setFont(new Font("宋体",Font.BOLD,25));
		panel5.add(button);
		panel5.setBounds(100, 250, 300, 100);
		
		button.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				String name = textname.getText().trim();
				ManageUser mu = new ManageUser();
				try {
					mu.deleteUser(name);
					JOptionPane.showMessageDialog(null, "成功删除此用户", "删除", JOptionPane.PLAIN_MESSAGE);
				} catch (Exception e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});
		
		
		f.add(panel1);
		f.add(panel2);
		f.add(panel5);
		f.add(label);
		
		textname.setOpaque(false);
		panel1.setOpaque(false);
		panel2.setOpaque(false);
		panel5.setOpaque(false);
		

public class FixBook {

	public FixBook(DefaultTableModel model,int id) {
		JFrame f = new JFrame();
		f.setSize(500, 500);
		f.setLocationRelativeTo(null);
		f.setLayout(null);
		f.setTitle("修改图书");
		
		//改变窗口图标
		Toolkit tool = Toolkit.getDefaultToolkit();
		Image img = tool.getImage("picture\\sign.jpg");
		f.setIconImage(img);
		
		/*设置窗口背景图案*/
		Icon i = new ImageIcon("picture\\admin_background.jpg");
		JLabel label = new JLabel(i);
		label.setBounds(0, 0, 500, 500);
		
		
		
		JPanel panel1 = new JPanel();

public class UserManage extends JFrame{
	JPanel panel = new JPanel();	
	public UserManage() {
		
		panel.setLayout(null);
		
		//改变窗口图标
		Toolkit tool = Toolkit.getDefaultToolkit();
		Image img = tool.getImage("picture\\sign.jpg");
		this.setIconImage(img);
		
		/*设置窗口背景图案*/
		Icon i = new ImageIcon("picture\\background5.jpg");
		JLabel label = new JLabel(i);
		label.setBounds(0, 0,1000, 800);
		
		JLabel lab = new JLabel("管理操作");
		Font font1 = new Font("宋体",Font.BOLD,40);
		lab.setFont(font1);
		lab.setBounds(380, 50, 500, 100);
		panel.add(lab);
		
		Dimension d = new Dimension(200,50);
		Font font2 = new Font("宋体",Font.BOLD,25);
		
		JButton button1 = new JButton("添加用户");
		JButton button2 = new JButton("修改密码");
		JButton button3 = new JButton("修改姓名");
		JButton button4 = new JButton("删除用户");
		
		button1.setBackground(Color.pink);
		button2.setBackground(Color.pink);
		button3.setBackground(Color.pink);
		button4.setBackground(Color.pink);
		
		button1.setPreferredSize(d);
		button2.setPreferredSize(d);
		button3.setPreferredSize(d);
		button4.setPreferredSize(d);
		
		button1.setBounds(320, 170, 300, 50);
		button2.setBounds(320, 270, 300, 50);
		button3.setBounds(320, 370, 300, 50);
		button4.setBounds(320, 470, 300, 50);
		
		button1.setFont(font2);
				try {
					flag = ci.checkPass(borrower, password1);
					
				} catch (Exception e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}

				if(flag==1) {
					if((password2.length()<6||password2.length()>12)||password2.matches(regex)) {
						JOptionPane.showMessageDialog(null, "输入密码不符合要求", "警告", JOptionPane.WARNING_MESSAGE);
					}else {
						if(!(password2.equals(password3))){
							JOptionPane.showMessageDialog(null, "您输入的两次密码不相同", "警告",JOptionPane.ERROR_MESSAGE);
						}else {
							
							try {
								ci.savePass(password2, borrower);
							} catch (Exception e1) {
								// TODO Auto-generated catch block
								e1.printStackTrace();
							}
							
							try {
								ci.savePass2(borrowname, borrower);
							} catch (Exception e1) {
								// TODO Auto-generated catch block
								e1.printStackTrace();
							}
							JOptionPane.showMessageDialog(null, "信息更改成功", "恭喜",JOptionPane.PLAIN_MESSAGE);
						}
					}
					
				}else {
					JOptionPane.showMessageDialog(null, "对不起,您的旧密码有误", "请重新输入", JOptionPane.PLAIN_MESSAGE);
				}
			}
		});
		
		panel.add(label);
		
		panel.setOpaque(false);
		textprepass.setOpaque(false);
		textpass.setOpaque(false);
		textrepass.setOpaque(false);
		textname.setOpaque(false);
		
		
		this.add(panel);
	}
}
		f.add(panel2);
		f.add(panel3);
		f.add(panel4);
		f.add(panel5);
		f.add(label);
		
		textname.setOpaque(false);
		textpass.setOpaque(false);
		panel1.setOpaque(false);
		panel2.setOpaque(false);
		panel3.setOpaque(false);
		panel4.setOpaque(false);
		panel5.setOpaque(false);
		
		f.setVisible(true);
	}
}

public class ABorrowHistory {
	/*显示所有借书记录*/
	public void allHistory(DefaultTableModel model)throws Exception{
		String sql = "select * from borrowhistory";
		Connection con;
		ResultSet rs;
		PreparedStatement ps;
		ConnectData cd = new ConnectData();
		con = cd.connect();
		ps = con.prepareStatement(sql);
		rs = ps.executeQuery();
		while(rs.next()) {
			int BookNo = rs.getInt(1);
			String Borrower = rs.getString(2);
			String Book = rs.getString(3);
			Date Time = rs.getDate(4);
			String Lend = rs.getString(5);

public class SignMenu {
	private String username;
	private String password1;
	private String text;
	private int flag = 0;
	public JTextField id;
	private String name;
	private String select;
	
	public void init() {
			
		JFrame f = new JFrame("欢迎使用图书管理系统");
		
		//改变窗口图标
		Toolkit t = Toolkit.getDefaultToolkit();
		Image img = t.getImage("picture\\sign.jpg");
		f.setIconImage(img);
		
		//设置窗口大小
		f.setSize(600, 600);
		
		//使窗口居中
		f.setLocationRelativeTo(null);
		
		//设置布局为空
		f.setLayout(null);
		
		//设置窗口背景图案
			return 1;
		}
		con.close();
		return 0;
		
	}
}

public class SignMenu {
	private String username;
	private String password1;
	private String text;
	private int flag = 0;
	public JTextField id;
	private String name;
	private String select;
	
	public void init() {
			
		JFrame f = new JFrame("欢迎使用图书管理系统");
		
		//改变窗口图标
		Toolkit t = Toolkit.getDefaultToolkit();
		Image img = t.getImage("picture\\sign.jpg");
							
							abh.bookSelect(model, book);
						} catch (Exception e1) {
							
							e1.printStackTrace();
						}
						
					}else if(select.equals("按照借阅人查询")) {
						borrower = text3.getText().trim();
						try {
							
							abh.borrowerSelect(model, borrower);
						} catch (Exception e1) {
							
							e1.printStackTrace();
						}
						
					}else if(select.equals("按照编号查询")) {
						
						try {
							id = Integer.parseInt(text3.getText().trim());
							abh.idSelect(model, id);
						} catch (Exception e1) {
							
						}
					}
				}
			});
			
			
			
			JTableHeader head = table.getTableHeader();//创建表格表头对象
			head.setPreferredSize(new Dimension(head.getWidth(),35));//设置表头框框大小
			head.setFont(new Font("宋体",Font.BOLD,15));//设置表格字体
			table.setRowHeight(30);//设置表格行宽
			table.setFont(new Font("宋体",Font.BOLD,20));//设置表格行中字体大小
			
			
			laypane.add(label, new Integer(0),0);
			laypane.add(lab3,new Integer(150),1);
			laypane.add(j,new Integer(100),2);
			laypane.add(text3,new Integer(100),3);
			laypane.add(button, new Integer(100),4);
			public void mouseClicked(MouseEvent e) {
				selectrow = table.getSelectedRow();
				bookid =  (int) table.getValueAt(selectrow, 0);//获取所选书本编号
				bookname = (String) table.getValueAt(selectrow, 1);//获取所选书名
				lend= (String) table.getValueAt(selectrow, 3);//获取所选书本状态
				
			}
		});
		
		borroww = "借出";
		returnn = "退还";
		
		
		/*添加图书*/
		button1.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				new AddBook(model);
				
			}
		});
		
		/*修改图书*/
		button3.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				new FixBook(model,bookid);
				
			}
		});
		
		/*删除图书*/
		button2.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				new DeleteBook(model,bookid,bookname);
			}
			PreparedStatement ps;
			
			ConnectData cd = new ConnectData();
			con = cd.connect();		
			String sql = "insert into user values(?,?,?,0)";
			ps = con.prepareStatement(sql);
			ps.setString(1, username);
			ps.setString(2, password);
			ps.setString(3, name);
			int ok = ps.executeUpdate();
			con.close();
		}
	public int compare(String username) throws Exception{
		ResultSet rs ;
		Connection con;
		PreparedStatement ps;
		ConnectData cd = new ConnectData();
		con = cd.connect();		
		String sql = "select * from user where username = ?";
		ps = con.prepareStatement(sql);
		ps.setString(1, username);
		rs = ps.executeQuery();
		while(rs.next()) {
			return 1;
		}
		con.close();
		return 0;
		
	}
}

		/*点击注册跳转到注册界面*/
		button1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				f.dispose();
				Regest r = new Regest();
				try {
					r.show();
				} catch (Exception e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});
		
		/*登陆信息比对*/
		button2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				text = j.getToolTipText();
				username = id.getText().trim();
				password1 = password.getText().trim();
				
				try {
					flag = select(username,password1);
				} catch (Exception e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				if(flag == 1) {//密码输入正确
					name = id.getText().trim();
					
					if(select == "普通用户") {
						f.dispose();
						new User(name);
					}else if(select == "管理员用户") {
						flag = 0;
						try {
							flag = ensure(name);
						} catch (Exception e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
						if(flag == 1) {
							new Admin(name);
							f.dispose();
						}else {
							JOptionPane.showMessageDialog(null, "对不起,您还不是管理员", "警告", JOptionPane.WARNING_MESSAGE);
						}
					}
				}else if(flag == 0) {//密码输入错误
					JOptionPane.showMessageDialog(null, "密码输入有误", "抱歉", JOptionPane.PLAIN_MESSAGE);
				}else if (flag == 2) {
					JOptionPane.showMessageDialog(null, "用户名不存在","注意" , JOptionPane.PLAIN_MESSAGE);
				}
			}
					} catch (Exception e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					
				}else {
					bookname2 = "《" + bookname + "》";
					JOptionPane.showMessageDialog(null, bookname+"已被借阅", "抱歉", JOptionPane.PLAIN_MESSAGE);
				}
				
			}
		});
		
		
		
		label.setOpaque(false);
		laypane.setOpaque(false);
		
		text3.setOpaque(false);
	} 
	
}

		Image img = tool.getImage("picture\\sign.jpg");
		f.setIconImage(img);
		
		/*设置窗口背景图案*/
		Icon i = new ImageIcon("picture\\admin_background.jpg");
		JLabel label = new JLabel(i);
		label.setBounds(0, 0, 500, 500);
		
		
		
		JPanel panel1 = new JPanel();
		JPanel panel2 = new JPanel();
		JPanel panel3 = new JPanel();
		JPanel panel4 = new JPanel();
		JPanel panel5 = new JPanel();
		
		Font font = new Font("宋体",Font.BOLD,30);
		Font font1 = new Font("宋体",Font.PLAIN,20);
		Dimension dimension = new Dimension(300,40);
		Dimension dimension1 = new Dimension(200,40);
		
		/*书名标签*/
		JLabel labbook = new JLabel("书名:");
		labbook.setFont(font);
		panel1.add(labbook);
		panel1.setBounds(50, 30, 200, 60);
		
		/*书名输入文本框*/
		JTextField textbook = new JTextField(12);
		panel2.setLayout(null);
		textbook.setSize(dimension);
		textbook.setFont(font1);
		panel2.add(textbook);
		panel2.setBounds(100, 90, 500, 50);
		
		/*作者标签*/
		JLabel labauthor = new JLabel("作者:");
		labauthor.setFont(font);
		panel3.add(labauthor);
		panel3.setBounds(50, 160, 200, 60);
		
		/*作者输入文本框*/
		JTextField textauthor = new JTextField(12);
		panel4.setLayout(null);
		textauthor.setSize(dimension);
		textauthor.setFont(font1);
		panel4.add(textauthor);
			
			/*面板3中的搜索框*/
			JTextField text3 = new JTextField();
			Dimension dimension1 = new Dimension(270,30);
			text3.setFont(font1);
			text3.setSize(dimension1);
			text3.setBackground(Color.pink);
			text3.setBounds(390,100, 270, 30);
			
			/*按钮*/
			JButton button = new JButton("搜索");
			button.setBackground(Color.pink);
			button.setBounds(670, 100, 120, 30);
			button.setFont(font2);
			
			model = new DefaultTableModel();
			model.addColumn("图书编号", new Vector<Integer>());
			model.addColumn("书名", new Vector<String>());
			model.addColumn("借阅人", new Vector<String>());
			model.addColumn("日期", new Vector<Date>());
			model.addColumn("借阅状态", new Vector<String>());
			JTable table = new JTable(model);
			
			jp = new JScrollPane(table);
			jp.setBounds(90, 160, 800, 500);
			
			/*显示所有结束记录*/
			ABorrowHistory abh = new ABorrowHistory();
			try {
				abh.allHistory(model);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			/*下拉框添加事件*/
			select = "按照书名查询";
			j.addItemListener(new ItemListener() {
				
				@Override
				public void itemStateChanged(ItemEvent e) {
					
					if(e.getStateChange()==ItemEvent.SELECTED) {
						select =(String) e.getItem();
						
					}
				}
			});
			

请添加图片描述
请添加图片描述
请添加图片描述
请添加图片描述
请添加图片描述
请添加图片描述
请添加图片描述
请添加图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值